v0.1.4 优化登录接口,添加密码加密
parent
8b7c53671f
commit
e7931c8b93
|
|
@ -24,10 +24,8 @@ public class AuthController {
|
||||||
*/
|
*/
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public AjaxResult login(@RequestBody LoginRequest loginRequest) {
|
public AjaxResult login(@RequestBody LoginRequest loginRequest) {
|
||||||
System.out.println(loginRequest);
|
|
||||||
// 认证成功,返回 JWT 令牌
|
// 认证成功,返回 JWT 令牌
|
||||||
String token = authService.authenticate(loginRequest);
|
String token = authService.authenticate(loginRequest);
|
||||||
System.out.println(AjaxResult.success("登录成功", token).toString());
|
|
||||||
return AjaxResult.success("登录成功", token);
|
return AjaxResult.success("登录成功", token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@ public class GlobalExceptionHandler {
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(CustomException.class)
|
@ExceptionHandler(CustomException.class)
|
||||||
public AjaxResult handleCustomException(CustomException e) {
|
public AjaxResult handleCustomException(CustomException e) {
|
||||||
System.out.println(AjaxResult.error(e.getCode(), e.getMessage()));
|
|
||||||
return AjaxResult.error(e.getCode(), e.getMessage());
|
return AjaxResult.error(e.getCode(), e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -25,7 +24,6 @@ public class GlobalExceptionHandler {
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
public AjaxResult handleException(Exception e) {
|
public AjaxResult handleException(Exception e) {
|
||||||
System.out.println(AjaxResult.error(500, "服务器内部错误:" + e.getMessage()));
|
|
||||||
return AjaxResult.error(500, "服务器内部错误:" + e.getMessage());
|
return AjaxResult.error(500, "服务器内部错误:" + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import com.bruce.sams.exception.UserNotFoundException;
|
||||||
import com.bruce.sams.mapper.SysRoleMapper;
|
import com.bruce.sams.mapper.SysRoleMapper;
|
||||||
import com.bruce.sams.mapper.SysUserMapper;
|
import com.bruce.sams.mapper.SysUserMapper;
|
||||||
import com.bruce.sams.domain.sys.User;
|
import com.bruce.sams.domain.sys.User;
|
||||||
|
import com.bruce.sams.utils.PasswordUtil;
|
||||||
import com.bruce.sams.utils.TokenUtil;
|
import com.bruce.sams.utils.TokenUtil;
|
||||||
import com.bruce.sams.service.AuthService;
|
import com.bruce.sams.service.AuthService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
@ -48,7 +49,7 @@ public class AuthServiceImpl implements AuthService {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查密码是否匹配
|
// 检查密码是否匹配
|
||||||
if (!user.getPassword().equals(loginRequest.getPassword())) {
|
if (!PasswordUtil.matches(loginRequest.getPassword(), user.getPassword())) {
|
||||||
throw new PasswordIncorrectException();
|
throw new PasswordIncorrectException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.bruce.sams.utils;
|
||||||
|
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 密码加密工具类
|
||||||
|
*/
|
||||||
|
public class PasswordUtil {
|
||||||
|
private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密
|
||||||
|
* @param rawPassword 原始密码
|
||||||
|
* @return 加密后的密码
|
||||||
|
*/
|
||||||
|
public static String encode(String rawPassword) {
|
||||||
|
return encoder.encode(rawPassword);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证密码是否匹配
|
||||||
|
*
|
||||||
|
* @param rawPassword 待匹配密码
|
||||||
|
* @param encodedPassword 加密密码
|
||||||
|
* @return 是否匹配
|
||||||
|
*/
|
||||||
|
public static boolean matches(String rawPassword, String encodedPassword) {
|
||||||
|
return encoder.matches(rawPassword, encodedPassword);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package com.bruce.sams;
|
package com.bruce.sams;
|
||||||
|
|
||||||
|
import com.bruce.sams.utils.PasswordUtil;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
|
@ -8,6 +9,7 @@ class SamsApplicationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {
|
void contextLoads() {
|
||||||
|
System.out.println(PasswordUtil.encode("password123"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue