feat: 统一响应格式与消息国际化

This commit is contained in:
2025-11-29 11:08:31 +08:00
parent 26a587f20b
commit e9ee523147
21 changed files with 198 additions and 128 deletions

View File

@ -48,7 +48,7 @@ public class AuthenticationFilter extends OncePerRequestFilter {
SecurityContextHolder.setAuthentication(initialize);
filterChain.doFilter(request, response);
} catch (BusinessException e) {
response.setStatus(e.getCode());
response.setStatus(200);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding("UTF-8");
response.getWriter().write(new ObjectMapper().writeValueAsString(Result.failure(e)));

View File

@ -1,31 +1,38 @@
package day.gitlab.dolphin.common.security.annotation;
import day.gitlab.dolphin.common.core.i18n.MessagesHelper;
import day.gitlab.dolphin.common.security.Authentication;
import day.gitlab.dolphin.common.security.SecurityContextHolder;
import day.gitlab.dolphin.common.security.exception.NotAuthorityException;
import day.gitlab.dolphin.common.security.exception.NotLoginException;
import day.gitlab.dolphin.common.security.constants.Exceptions;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AuthorityCheckAspect {
private final MessagesHelper messagesHelper;
public AuthorityCheckAspect(MessagesHelper messagesHelper) {
this.messagesHelper = messagesHelper;
}
@Before("@annotation(authorityCheck)")
public void check(JoinPoint joinPoint, AuthorityCheck authorityCheck) {
Authentication authentication = SecurityContextHolder.getAuthentication();
if (authentication == null) {
throw new NotLoginException();
throw messagesHelper.newBusinessException(Exceptions.NOT_LOGIN);
}
// AuthorityCheck的type如果是AND输出1
if (authorityCheck.type() == AuthorityType.AND) {
if (!authentication.hasAllAuthorities(authorityCheck.value())) {
throw new NotAuthorityException(authorityCheck.value());
throw messagesHelper.newBusinessException(Exceptions.NOT_AUTHORITY);
}
} else if (authorityCheck.type() == AuthorityType.OR) {
if (!authentication.hasAnyAuthorities(authorityCheck.value())) {
throw new NotAuthorityException(authorityCheck.value());
throw messagesHelper.newBusinessException(Exceptions.NOT_AUTHORITY);
}
}
}

View File

@ -0,0 +1,12 @@
package day.gitlab.dolphin.common.security.constants;
public class Exceptions {
public static final String NOT_LOGIN = "00010001";
public static final String TOKEN_INVALID = "00010002";
public static final String TOKEN_EXPIRED = "00010003";
public static final String NOT_AUTHORITY = "00010004";
}

View File

@ -1,12 +0,0 @@
package day.gitlab.dolphin.common.security.exception;
import day.gitlab.dolphin.common.core.exception.BusinessException;
import lombok.Getter;
@Getter
public class AuthenticationException extends BusinessException {
public AuthenticationException(int code, String message) {
super(code, message);
}
}

View File

@ -1,14 +0,0 @@
package day.gitlab.dolphin.common.security.exception;
import lombok.Getter;
@Getter
public class InvalidTokenException extends AuthenticationException {
private final String token;
public InvalidTokenException(String token) {
super(401, "Unauthorized: Invalid token");
this.token = token;
}
}

View File

@ -1,14 +0,0 @@
package day.gitlab.dolphin.common.security.exception;
import lombok.Getter;
@Getter
public class NotAuthorityException extends AuthenticationException {
private final String[] userAuthorities;
public NotAuthorityException(String[] userAuthorities) {
super(403, "Forbidden: Insufficient permissions");
this.userAuthorities = userAuthorities;
}
}

View File

@ -1,8 +0,0 @@
package day.gitlab.dolphin.common.security.exception;
public class NotLoginException extends AuthenticationException{
public NotLoginException() {
super(401, "Unauthorized");
}
}

View File

@ -1,20 +0,0 @@
package day.gitlab.dolphin.common.security.exception;
import lombok.Getter;
import java.util.Date;
@Getter
public class TokenExpiredException extends AuthenticationException {
private final String token;
private final Date expiration;
public TokenExpiredException(String token, Date expiration) {
super(401, "Unauthorized: Token expired");
this.token = token;
this.expiration = expiration;
}
}

View File

@ -1,14 +1,13 @@
package day.gitlab.dolphin.common.security.jwt;
import com.fasterxml.jackson.databind.ObjectMapper;
import day.gitlab.dolphin.common.core.i18n.MessagesHelper;
import day.gitlab.dolphin.common.security.Authentication;
import day.gitlab.dolphin.common.security.AuthenticationInitialize;
import day.gitlab.dolphin.common.security.AuthenticationProvider;
import day.gitlab.dolphin.common.security.UserPrincipal;
import day.gitlab.dolphin.common.security.config.SecurityConfig;
import day.gitlab.dolphin.common.security.exception.InvalidTokenException;
import day.gitlab.dolphin.common.security.exception.NotLoginException;
import day.gitlab.dolphin.common.security.exception.TokenExpiredException;
import day.gitlab.dolphin.common.security.constants.Exceptions;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.data.redis.core.StringRedisTemplate;
@ -29,12 +28,16 @@ public class JwtAuthenticationInitialize implements AuthenticationInitialize {
private final AuthenticationProvider authenticationProvider;
private final MessagesHelper messagesHelper;
public JwtAuthenticationInitialize(SecurityConfig securityConfig,
StringRedisTemplate stringRedisTemplate,
AuthenticationProvider authenticationProvider) {
AuthenticationProvider authenticationProvider,
MessagesHelper messagesHelper) {
this.securityConfig = securityConfig;
this.stringRedisTemplate = stringRedisTemplate;
this.authenticationProvider = authenticationProvider;
this.messagesHelper = messagesHelper;
}
@Override
@ -44,7 +47,7 @@ public class JwtAuthenticationInitialize implements AuthenticationInitialize {
// 1、获取Token
String token = jwt.getTokenFromRequest(request);
if (token == null) {
throw new NotLoginException();
throw messagesHelper.newBusinessException(Exceptions.NOT_LOGIN);
}
// 2、解析Token获取用户ID
String userId;
@ -56,11 +59,11 @@ public class JwtAuthenticationInitialize implements AuthenticationInitialize {
Objects.requireNonNull(userId);
Objects.requireNonNull(expiration);
} catch (Exception e) {
throw new InvalidTokenException(token);
throw messagesHelper.newBusinessException(Exceptions.TOKEN_INVALID);
}
// 3、判断是否过期
if (expiration.before(new Date())) {
throw new TokenExpiredException(token, expiration);
throw messagesHelper.newBusinessException(Exceptions.TOKEN_EXPIRED);
}
// 4、从Redis或数据库中加载用户信息
UserPrincipal userPrincipal;
@ -83,7 +86,7 @@ public class JwtAuthenticationInitialize implements AuthenticationInitialize {
userAuthorities = list.stream().map(Object::toString).collect(Collectors.toList());
}
} catch (Exception e) {
throw new InvalidTokenException(token);
throw messagesHelper.newBusinessException(Exceptions.TOKEN_INVALID);
}
return new Authentication(userPrincipal, userAuthorities);