feat: 规范auth/login接口
This commit is contained in:
parent
8eae865aab
commit
aa04c49422
@ -21,6 +21,9 @@ public enum Status {
|
|||||||
/** 退出成功! */
|
/** 退出成功! */
|
||||||
LOGOUT(200, "退出成功!"),
|
LOGOUT(200, "退出成功!"),
|
||||||
|
|
||||||
|
/** 登录成功! */
|
||||||
|
LOGIN_SUCCESS(200, "登录成功!"),
|
||||||
|
|
||||||
/** 请先登录! */
|
/** 请先登录! */
|
||||||
UNAUTHORIZED(401, "请先登录!"),
|
UNAUTHORIZED(401, "请先登录!"),
|
||||||
|
|
||||||
|
|||||||
@ -2,8 +2,8 @@ package asia.yulinling.workflow.controller;
|
|||||||
|
|
||||||
import asia.yulinling.workflow.dto.request.LoginRequest;
|
import asia.yulinling.workflow.dto.request.LoginRequest;
|
||||||
import asia.yulinling.workflow.dto.request.RegisterRequest;
|
import asia.yulinling.workflow.dto.request.RegisterRequest;
|
||||||
import asia.yulinling.workflow.dto.response.JWTAuthResponse;
|
|
||||||
import asia.yulinling.workflow.model.ApiResponse;
|
import asia.yulinling.workflow.model.ApiResponse;
|
||||||
|
import asia.yulinling.workflow.model.vo.LoginVO;
|
||||||
import asia.yulinling.workflow.model.vo.RegisterVO;
|
import asia.yulinling.workflow.model.vo.RegisterVO;
|
||||||
import asia.yulinling.workflow.service.AuthService;
|
import asia.yulinling.workflow.service.AuthService;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
@ -29,10 +29,8 @@ public class AuthController {
|
|||||||
private final AuthService authService;
|
private final AuthService authService;
|
||||||
|
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public ApiResponse<JWTAuthResponse> login(@RequestBody LoginRequest loginRequest) {
|
public ApiResponse<LoginVO> login(@RequestBody LoginRequest loginRequest) {
|
||||||
String token = authService.login(loginRequest);
|
return authService.login(loginRequest);
|
||||||
JWTAuthResponse jwtAuthResponse = new JWTAuthResponse(token);
|
|
||||||
return ApiResponse.ofSuccess(jwtAuthResponse);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/logout")
|
@PostMapping("/logout")
|
||||||
|
|||||||
21
src/main/java/asia/yulinling/workflow/model/vo/LoginVO.java
Normal file
21
src/main/java/asia/yulinling/workflow/model/vo/LoginVO.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package asia.yulinling.workflow.model.vo;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 登录信息VO
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author YLL
|
||||||
|
* @since 2025/7/3
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class LoginVO {
|
||||||
|
private Long userId;
|
||||||
|
private String username;
|
||||||
|
private String accessToken;
|
||||||
|
private Long expiresIn;
|
||||||
|
}
|
||||||
@ -3,6 +3,7 @@ package asia.yulinling.workflow.service;
|
|||||||
import asia.yulinling.workflow.dto.request.LoginRequest;
|
import asia.yulinling.workflow.dto.request.LoginRequest;
|
||||||
import asia.yulinling.workflow.dto.request.RegisterRequest;
|
import asia.yulinling.workflow.dto.request.RegisterRequest;
|
||||||
import asia.yulinling.workflow.model.ApiResponse;
|
import asia.yulinling.workflow.model.ApiResponse;
|
||||||
|
import asia.yulinling.workflow.model.vo.LoginVO;
|
||||||
import asia.yulinling.workflow.model.vo.RegisterVO;
|
import asia.yulinling.workflow.model.vo.RegisterVO;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
@ -21,7 +22,7 @@ public interface AuthService {
|
|||||||
* @param loginRequest 登录请求
|
* @param loginRequest 登录请求
|
||||||
* @return token
|
* @return token
|
||||||
*/
|
*/
|
||||||
String login(LoginRequest loginRequest);
|
ApiResponse<LoginVO> login(LoginRequest loginRequest);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 退出登录
|
* 退出登录
|
||||||
|
|||||||
@ -6,7 +6,9 @@ import asia.yulinling.workflow.dto.request.RegisterRequest;
|
|||||||
import asia.yulinling.workflow.mapper.UserMapper;
|
import asia.yulinling.workflow.mapper.UserMapper;
|
||||||
import asia.yulinling.workflow.model.ApiResponse;
|
import asia.yulinling.workflow.model.ApiResponse;
|
||||||
import asia.yulinling.workflow.model.entity.User;
|
import asia.yulinling.workflow.model.entity.User;
|
||||||
|
import asia.yulinling.workflow.model.vo.LoginVO;
|
||||||
import asia.yulinling.workflow.model.vo.RegisterVO;
|
import asia.yulinling.workflow.model.vo.RegisterVO;
|
||||||
|
import asia.yulinling.workflow.model.vo.user.UserPrincipal;
|
||||||
import asia.yulinling.workflow.service.AuthService;
|
import asia.yulinling.workflow.service.AuthService;
|
||||||
import asia.yulinling.workflow.utils.JwtUtil;
|
import asia.yulinling.workflow.utils.JwtUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
@ -49,14 +51,26 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
* @return token
|
* @return token
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String login(LoginRequest loginRequest) {
|
public ApiResponse<LoginVO> login(LoginRequest loginRequest) {
|
||||||
|
|
||||||
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
|
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
|
||||||
loginRequest.getUsername(), loginRequest.getPassword()
|
loginRequest.getUsername(), loginRequest.getPassword()
|
||||||
));
|
));
|
||||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||||
String token = jwtUtil.generateToken(authentication, false);
|
|
||||||
log.info("generateToken: {}", token);
|
UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
|
||||||
return token;
|
|
||||||
|
String accessToken = jwtUtil.generateToken(authentication, false);
|
||||||
|
Long expiresIn = jwtUtil.getExpiresIn(loginRequest.getRememberMe());
|
||||||
|
|
||||||
|
LoginVO loginVO = LoginVO.builder()
|
||||||
|
.userId(userPrincipal.getId())
|
||||||
|
.username(userPrincipal.getUsername())
|
||||||
|
.accessToken(accessToken)
|
||||||
|
.expiresIn(expiresIn)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return ApiResponse.ofStatus(Status.LOGIN_SUCCESS, loginVO);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -239,6 +239,16 @@ public class JwtUtil {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据rememberMe返回Token过期时间
|
||||||
|
*
|
||||||
|
* @param rememberMe 记住我
|
||||||
|
* @return Token过期时间
|
||||||
|
*/
|
||||||
|
public Long getExpiresIn(boolean rememberMe) {
|
||||||
|
return rememberMe ? this.remember : this.ttl;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取签名密钥
|
* 获取签名密钥
|
||||||
*
|
*
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user