feat: 规范auth/login接口

This commit is contained in:
yulinling 2025-07-03 22:00:28 +08:00
parent 8eae865aab
commit aa04c49422
6 changed files with 57 additions and 10 deletions

View File

@ -21,6 +21,9 @@ public enum Status {
/** 退出成功! */
LOGOUT(200, "退出成功!"),
/** 登录成功! */
LOGIN_SUCCESS(200, "登录成功!"),
/** 请先登录! */
UNAUTHORIZED(401, "请先登录!"),

View File

@ -2,8 +2,8 @@ package asia.yulinling.workflow.controller;
import asia.yulinling.workflow.dto.request.LoginRequest;
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.vo.LoginVO;
import asia.yulinling.workflow.model.vo.RegisterVO;
import asia.yulinling.workflow.service.AuthService;
import jakarta.servlet.http.HttpServletRequest;
@ -29,10 +29,8 @@ public class AuthController {
private final AuthService authService;
@PostMapping("/login")
public ApiResponse<JWTAuthResponse> login(@RequestBody LoginRequest loginRequest) {
String token = authService.login(loginRequest);
JWTAuthResponse jwtAuthResponse = new JWTAuthResponse(token);
return ApiResponse.ofSuccess(jwtAuthResponse);
public ApiResponse<LoginVO> login(@RequestBody LoginRequest loginRequest) {
return authService.login(loginRequest);
}
@PostMapping("/logout")

View 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;
}

View File

@ -3,6 +3,7 @@ package asia.yulinling.workflow.service;
import asia.yulinling.workflow.dto.request.LoginRequest;
import asia.yulinling.workflow.dto.request.RegisterRequest;
import asia.yulinling.workflow.model.ApiResponse;
import asia.yulinling.workflow.model.vo.LoginVO;
import asia.yulinling.workflow.model.vo.RegisterVO;
import jakarta.servlet.http.HttpServletRequest;
@ -21,7 +22,7 @@ public interface AuthService {
* @param loginRequest 登录请求
* @return token
*/
String login(LoginRequest loginRequest);
ApiResponse<LoginVO> login(LoginRequest loginRequest);
/**
* 退出登录

View File

@ -6,7 +6,9 @@ import asia.yulinling.workflow.dto.request.RegisterRequest;
import asia.yulinling.workflow.mapper.UserMapper;
import asia.yulinling.workflow.model.ApiResponse;
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.user.UserPrincipal;
import asia.yulinling.workflow.service.AuthService;
import asia.yulinling.workflow.utils.JwtUtil;
import cn.hutool.core.util.StrUtil;
@ -49,14 +51,26 @@ public class AuthServiceImpl implements AuthService {
* @return token
*/
@Override
public String login(LoginRequest loginRequest) {
public ApiResponse<LoginVO> login(LoginRequest loginRequest) {
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
loginRequest.getUsername(), loginRequest.getPassword()
));
SecurityContextHolder.getContext().setAuthentication(authentication);
String token = jwtUtil.generateToken(authentication, false);
log.info("generateToken: {}", token);
return token;
UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
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);
}
/**

View File

@ -239,6 +239,16 @@ public class JwtUtil {
return null;
}
/**
* 根据rememberMe返回Token过期时间
*
* @param rememberMe 记住我
* @return Token过期时间
*/
public Long getExpiresIn(boolean rememberMe) {
return rememberMe ? this.remember : this.ttl;
}
/**
* 获取签名密钥
*