mirror of
https://github.com/FreeeBird/hotel.git
synced 2025-11-02 05:24:46 +08:00
1.update README
2.密码不用明文存储
This commit is contained in:
@@ -23,7 +23,7 @@ public class UserController {
|
||||
*/
|
||||
@RequestMapping(method = RequestMethod.POST,value = "/login")
|
||||
public int userLogin(String username,String password){
|
||||
int result = 0;
|
||||
int result;
|
||||
if (username == null | username == "" | password == null | password == ""){
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package cn.mafangui.hotel.service.impl;
|
||||
import cn.mafangui.hotel.entity.User;
|
||||
import cn.mafangui.hotel.mapper.UserMapper;
|
||||
import cn.mafangui.hotel.service.UserService;
|
||||
import cn.mafangui.hotel.utils.MD5Utils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -20,11 +21,13 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
@Override
|
||||
public int addUser(User user) {
|
||||
user.setPassword(MD5Utils.MD5Encode(user.getPassword()));
|
||||
return userMapper.insertSelective(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int insertUser(User user) {
|
||||
user.setPassword(MD5Utils.MD5Encode(user.getPassword()));
|
||||
return userMapper.insert(user);
|
||||
}
|
||||
|
||||
@@ -40,7 +43,8 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
@Override
|
||||
public User selectByUsernameAndPassword(String username, String password) {
|
||||
return userMapper.selectByUsernameAndPassword(username,password);
|
||||
String pass = MD5Utils.MD5Encode(password);
|
||||
return userMapper.selectByUsernameAndPassword(username,pass);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
60
src/main/java/cn/mafangui/hotel/utils/MD5Utils.java
Normal file
60
src/main/java/cn/mafangui/hotel/utils/MD5Utils.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package cn.mafangui.hotel.utils;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
|
||||
public class MD5Utils {
|
||||
|
||||
private static final String hexDigIts[] = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
|
||||
|
||||
/**
|
||||
* MD5加密
|
||||
* @param origin 字符
|
||||
* @param charsetname 编码
|
||||
* @return
|
||||
*/
|
||||
public static String MD5Encode(String origin, String charsetname){
|
||||
String resultString = null;
|
||||
try{
|
||||
resultString = new String(origin);
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
if(null == charsetname || "".equals(charsetname)){
|
||||
resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
|
||||
}else{
|
||||
resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
|
||||
}
|
||||
}catch (Exception e){
|
||||
}
|
||||
return resultString;
|
||||
}
|
||||
|
||||
public static String MD5Encode(String origin){
|
||||
String resultString = null;
|
||||
try{
|
||||
resultString = origin;
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
resultString = byteArrayToHexString(md.digest(resultString.getBytes("utf8")));
|
||||
}catch (Exception e){
|
||||
}
|
||||
return resultString;
|
||||
}
|
||||
|
||||
|
||||
public static String byteArrayToHexString(byte b[]){
|
||||
StringBuffer resultSb = new StringBuffer();
|
||||
for(int i = 0; i < b.length; i++){
|
||||
resultSb.append(byteToHexString(b[i]));
|
||||
}
|
||||
return resultSb.toString();
|
||||
}
|
||||
|
||||
public static String byteToHexString(byte b){
|
||||
int n = b;
|
||||
if(n < 0){
|
||||
n += 256;
|
||||
}
|
||||
int d1 = n / 16;
|
||||
int d2 = n % 16;
|
||||
return hexDigIts[d1] + hexDigIts[d2];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user