feat: 添加代码格式化

This commit is contained in:
lirui
2025-11-25 13:53:29 +08:00
parent 4304ec6e29
commit d89960f51c
72 changed files with 1403 additions and 1311 deletions

BIN
.idea/.cache/.easy-yapi/.api.cache.v1.1.db generated Normal file

Binary file not shown.

View File

View File

6
.idea/CoolRequestSetting.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CoolRequestSetting">
<option name="projectCachePath" value="project-e82c3cb9-7dfc-4fa6-b498-45789b6b3803" />
</component>
</project>

2
.idea/misc.xml generated
View File

@@ -15,7 +15,7 @@
</set> </set>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="zulu-17" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="corretto-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View File

@@ -7,7 +7,9 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients(basePackages = "cn.meowrain.aioj.backend.auth.clients") @EnableFeignClients(basePackages = "cn.meowrain.aioj.backend.auth.clients")
@SpringBootApplication @SpringBootApplication
public class AIOJAuthApplication { public class AIOJAuthApplication {
public static void main(String[] args) {
SpringApplication.run(AIOJAuthApplication.class, args); public static void main(String[] args) {
} SpringApplication.run(AIOJAuthApplication.class, args);
}
} }

View File

@@ -8,9 +8,11 @@ import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name = "user-service", path = "/api/v1/user") @FeignClient(name = "user-service", path = "/api/v1/user")
public interface UserClient { public interface UserClient {
@GetMapping("/inner/get-by-username")
Result<UserAuthRespDTO> getUserByUserName(@RequestParam("userAccount") String userAccount);
@GetMapping("/inner/get-by-userid") @GetMapping("/inner/get-by-username")
public Result<UserAuthRespDTO> getUserById(@RequestParam("userId") String userid); Result<UserAuthRespDTO> getUserByUserName(@RequestParam("userAccount") String userAccount);
@GetMapping("/inner/get-by-userid")
public Result<UserAuthRespDTO> getUserById(@RequestParam("userId") String userid);
} }

View File

@@ -1,5 +1,7 @@
package cn.meowrain.aioj.backend.auth.common.constants; package cn.meowrain.aioj.backend.auth.common.constants;
public class RedisKeyConstants { public class RedisKeyConstants {
public static String REFRESH_TOKEN_KEY_PREFIX = "refresh_token:%s";
public static String REFRESH_TOKEN_KEY_PREFIX = "refresh_token:%s";
} }

View File

@@ -5,16 +5,18 @@ import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor @RequiredArgsConstructor
public enum ChainMarkEnums { public enum ChainMarkEnums {
/**
* 用户登录请求验证
*/
USER_LOGIN_REQ_PARAM_VERIFY("USER_LOGIN_REQ_PARAM_VERIFY");
@Getter /**
private final String markName; * 用户登录请求验证
*/
USER_LOGIN_REQ_PARAM_VERIFY("USER_LOGIN_REQ_PARAM_VERIFY");
@Getter
private final String markName;
@Override
public String toString() {
return markName;
}
@Override
public String toString() {
return markName;
}
} }

View File

@@ -15,33 +15,27 @@ import org.springframework.security.web.SecurityFilterChain;
@EnableWebSecurity @EnableWebSecurity
public class SecurityConfiguration { public class SecurityConfiguration {
@Bean @Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http http.csrf(csrf -> csrf.disable())
.csrf(csrf -> csrf.disable()) .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .authorizeHttpRequests(auth -> auth
.authorizeHttpRequests(auth -> auth .requestMatchers("/v1/auth/**", "/doc.html", "/swagger-ui/**", "/swagger-resources/**", "/webjars/**",
.requestMatchers( "/v3/api-docs/**", "/favicon.ico")
"/v1/auth/**", .permitAll()
"/doc.html", .anyRequest()
"/swagger-ui/**", .authenticated());
"/swagger-resources/**", return http.build();
"/webjars/**", }
"/v3/api-docs/**",
"/favicon.ico"
)
.permitAll()
.anyRequest().authenticated());
return http.build();
}
@Bean @Bean
public PasswordEncoder passwordEncoder() { public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(); return new BCryptPasswordEncoder();
} }
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception {
return configuration.getAuthenticationManager();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception {
return configuration.getAuthenticationManager();
}
} }

View File

@@ -16,23 +16,25 @@ import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
@EnableKnife4j @EnableKnife4j
public class SwaggerConfiguration implements ApplicationRunner { public class SwaggerConfiguration implements ApplicationRunner {
@Value("${server.port:8080}")
private String serverPort;
@Value("${server.servlet.context-path:}")
private String contextPath;
@Bean @Value("${server.port:8080}")
public OpenAPI customerOpenAPI() { private String serverPort;
return new OpenAPI()
.info(new Info() @Value("${server.servlet.context-path:}")
.title("AIOJ-renz微服务✨") private String contextPath;
.description("用户认证功能")
.version("v1.0.0") @Bean
.contact(new Contact().name("meowrain").email("meowrain@126.com")) public OpenAPI customerOpenAPI() {
.license(new License().name("MeowRain").url("https://meowrain.cn"))); return new OpenAPI().info(new Info().title("AIOJ-renz微服务✨")
} .description("用户认证功能")
@Override .version("v1.0.0")
public void run(ApplicationArguments args) throws Exception { .contact(new Contact().name("meowrain").email("meowrain@126.com"))
log.info("✨API Document: http://127.0.0.1:{}{}/doc.html", serverPort, contextPath); .license(new License().name("MeowRain").url("https://meowrain.cn")));
} }
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("✨API Document: http://127.0.0.1:{}{}/doc.html", serverPort, contextPath);
}
} }

View File

@@ -8,18 +8,22 @@ import org.springframework.stereotype.Component;
@Data @Data
@ConfigurationProperties(value = JwtPropertiesConfiguration.PREFIX) @ConfigurationProperties(value = JwtPropertiesConfiguration.PREFIX)
public class JwtPropertiesConfiguration { public class JwtPropertiesConfiguration {
public static final String PREFIX = "jwt";
/**
* JWT 密钥(必须 32 字节以上)
*/
private String secret;
/** public static final String PREFIX = "jwt";
* 过期时间(单位:毫秒)
*/ /**
private long accessExpire; // access token TTL * JWT 密钥(必须 32 字节以上)
/** */
* 刷新令牌时间 private String secret;
*/
private long refreshExpire; // refresh token TTL /**
* 过期时间(单位:毫秒)
*/
private long accessExpire; // access token TTL
/**
* 刷新令牌时间
*/
private long refreshExpire; // refresh token TTL
} }

View File

@@ -13,25 +13,24 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/v1/auth") @RequestMapping("/v1/auth")
public class AuthController { public class AuthController {
private final AuthService authService; private final AuthService authService;
@PostMapping("/login") @PostMapping("/login")
public Result<UserLoginResponseDTO> login(@RequestBody UserLoginRequestDTO userLoginRequest) { public Result<UserLoginResponseDTO> login(@RequestBody UserLoginRequestDTO userLoginRequest) {
UserLoginResponseDTO userLoginResponse = authService.userLogin(userLoginRequest); UserLoginResponseDTO userLoginResponse = authService.userLogin(userLoginRequest);
return Results.success(userLoginResponse); return Results.success(userLoginResponse);
} }
@PostMapping("/refresh") @PostMapping("/refresh")
public Result<UserLoginResponseDTO> refresh(@RequestParam String refreshToken) { public Result<UserLoginResponseDTO> refresh(@RequestParam String refreshToken) {
return Results.success(authService.refreshToken(refreshToken)); return Results.success(authService.refreshToken(refreshToken));
} }
@PostMapping("/auth")
public Result<String> auth(@RequestBody UserLoginRequestDTO userLoginRequest) {
UserLoginResponseDTO userLoginResponseDTO = authService.userLogin(userLoginRequest);
return Results.success(userLoginResponseDTO.getAccessToken());
}
@PostMapping("/auth")
public Result<String> auth(@RequestBody UserLoginRequestDTO userLoginRequest) {
UserLoginResponseDTO userLoginResponseDTO = authService.userLogin(userLoginRequest);
return Results.success(userLoginResponseDTO.getAccessToken());
}
} }

View File

@@ -12,26 +12,28 @@ import org.springframework.stereotype.Component;
@Component @Component
@Slf4j @Slf4j
public class UserLoginRequestParamVerifyChain implements AbstractChianHandler<UserLoginRequestDTO> { public class UserLoginRequestParamVerifyChain implements AbstractChianHandler<UserLoginRequestDTO> {
@Override
public void handle(UserLoginRequestDTO requestParam) {
if (StringUtils.isAnyBlank(requestParam.getUserAccount(), requestParam.getUserPassword())) {
throw new ClientException("参数为空", ErrorCode.PARAMS_ERROR);
}
if (requestParam.getUserAccount().length() < 4) {
throw new ClientException("账号长度不小于4位", ErrorCode.PARAMS_ERROR);
}
if (requestParam.getUserPassword().length() < 8) {
throw new ClientException("密码长度不小于8位", ErrorCode.PARAMS_ERROR);
}
}
@Override @Override
public String mark() { public void handle(UserLoginRequestDTO requestParam) {
return ChainMarkEnums.USER_LOGIN_REQ_PARAM_VERIFY.getMarkName(); if (StringUtils.isAnyBlank(requestParam.getUserAccount(), requestParam.getUserPassword())) {
} throw new ClientException("参数为空", ErrorCode.PARAMS_ERROR);
}
if (requestParam.getUserAccount().length() < 4) {
throw new ClientException("账号长度不小于4位", ErrorCode.PARAMS_ERROR);
}
if (requestParam.getUserPassword().length() < 8) {
throw new ClientException("密码长度不小于8位", ErrorCode.PARAMS_ERROR);
}
}
@Override
public String mark() {
return ChainMarkEnums.USER_LOGIN_REQ_PARAM_VERIFY.getMarkName();
}
@Override
public int getOrder() {
return 10;
}
@Override
public int getOrder() {
return 10;
}
} }

View File

@@ -6,4 +6,5 @@ import org.springframework.stereotype.Component;
@Component @Component
public class UserLoginRequestParamVerifyContext extends CommonChainContext<UserLoginRequestDTO> { public class UserLoginRequestParamVerifyContext extends CommonChainContext<UserLoginRequestDTO> {
} }

View File

@@ -4,6 +4,9 @@ import lombok.Data;
@Data @Data
public class UserLoginRequestDTO { public class UserLoginRequestDTO {
private String userAccount;
private String userPassword; private String userAccount;
private String userPassword;
} }

View File

@@ -10,59 +10,59 @@ import java.util.Date;
@Data @Data
public class UserAuthRespDTO { public class UserAuthRespDTO {
/** /**
* id * id
*/ */
private Long id; private Long id;
/** /**
* 用户账号 * 用户账号
*/ */
private String userAccount; private String userAccount;
/**
* 用户密码
*/
private String userPassword;
/** /**
* 开放平台id * 用户密码
*/ */
private String unionId; private String userPassword;
/** /**
* 公众号openId * 开放平台id
*/ */
private String mpOpenId; private String unionId;
/** /**
* 用户昵称 * 公众号openId
*/ */
private String userName; private String mpOpenId;
/** /**
* 用户头像 * 用户昵称
*/ */
private String userAvatar; private String userName;
/** /**
* 用户简介 * 用户头像
*/ */
private String userProfile; private String userAvatar;
/** /**
* 用户角色user/admin/ban * 用户简介
*/ */
private String userRole; private String userProfile;
/** /**
* 创建时间 * 用户角色user/admin/ban
*/ */
private Date createTime; private String userRole;
/** /**
* 更新时间 * 创建时间
*/ */
private Date updateTime; private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
} }

View File

@@ -7,24 +7,28 @@ import java.util.Date;
@Data @Data
public class UserLoginResponseDTO implements Serializable { public class UserLoginResponseDTO implements Serializable {
/**
* id
*/
private Long id;
/** /**
* 用户账号 * id
*/ */
private String userAccount; private Long id;
/** /**
* 开放平台id * 用户账号
*/ */
private String unionId; private String userAccount;
private String accessToken; /**
private String refreshToken; * 开放平台id
private Long expire; */
private String unionId;
private String accessToken;
private String refreshToken;
private Long expire;
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
} }

View File

@@ -7,4 +7,5 @@ import org.springframework.stereotype.Component;
*/ */
@Component @Component
public class JwtAuthenticationFilter { public class JwtAuthenticationFilter {
} }

View File

@@ -4,17 +4,19 @@ import cn.meowrain.aioj.backend.auth.dto.req.UserLoginRequestDTO;
import cn.meowrain.aioj.backend.auth.dto.resp.UserLoginResponseDTO; import cn.meowrain.aioj.backend.auth.dto.resp.UserLoginResponseDTO;
public interface AuthService { public interface AuthService {
/**
* 用户登录
* @param request {@link UserLoginRequestDTO}
* @return {@link UserLoginResponseDTO}
*/
UserLoginResponseDTO userLogin(UserLoginRequestDTO request);
/** /**
* 刷新token * 用户登录
* @param refreshToken * @param request {@link UserLoginRequestDTO}
* @return * @return {@link UserLoginResponseDTO}
*/ */
UserLoginResponseDTO refreshToken(String refreshToken); UserLoginResponseDTO userLogin(UserLoginRequestDTO request);
/**
* 刷新token
* @param refreshToken
* @return
*/
UserLoginResponseDTO refreshToken(String refreshToken);
} }

View File

@@ -26,81 +26,84 @@ import java.util.concurrent.TimeUnit;
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j @Slf4j
public class AuthServiceImpl implements AuthService { public class AuthServiceImpl implements AuthService {
private final JwtUtil jwtUtil;
private final UserLoginRequestParamVerifyContext userLoginRequestParamVerifyContext;
private final UserClient userClient;
private final StringRedisTemplate stringRedisTemplate;
private final JwtPropertiesConfiguration jwtPropertiesConfiguration;
@Override private final JwtUtil jwtUtil;
public UserLoginResponseDTO userLogin(UserLoginRequestDTO requestParam) {
// 1.校验
userLoginRequestParamVerifyContext.handler(ChainMarkEnums.USER_LOGIN_REQ_PARAM_VERIFY.getMarkName(),
requestParam);
// 如果调用user-service失败那么就说明是系统内部错误
Result<UserAuthRespDTO> userResp = userClient.getUserByUserName(requestParam.getUserAccount());
if (userResp.isFail()) {
log.error("调用user-service返回失败{}", userResp.getMessage());
throw new ServiceException(ErrorCode.SYSTEM_ERROR);
}
UserAuthRespDTO user = userResp.getData();
if (ObjectUtil.isNull(user) private final UserLoginRequestParamVerifyContext userLoginRequestParamVerifyContext;
|| !BCrypt.checkpw(requestParam.getUserPassword(), user.getUserPassword())) {
throw new ServiceException("用户不存在或者密码错误", ErrorCode.NOT_LOGIN_ERROR);
}
// 生成 JWT
String accessToken = jwtUtil.generateAccessToken(user);
String refreshToken = jwtUtil.generateRefreshToken(user.getId());
UserLoginResponseDTO resp = new UserLoginResponseDTO();
resp.setId(user.getId());
resp.setUserAccount(user.getUserAccount());
resp.setAccessToken(accessToken);
resp.setRefreshToken(refreshToken);
// refresh token存入到REDIS里面 private final UserClient userClient;
stringRedisTemplate.opsForValue().set(
String.format(RedisKeyConstants.REFRESH_TOKEN_KEY_PREFIX, user.getId()),
refreshToken,
jwtPropertiesConfiguration.getRefreshExpire(),
TimeUnit.MILLISECONDS);
return resp;
}
/** private final StringRedisTemplate stringRedisTemplate;
* 更新access token使用refresh token
* @param refreshToken
* @return
*/
@Override
public UserLoginResponseDTO refreshToken(String refreshToken) {
UserLoginResponseDTO userLoginResponseDTO = new UserLoginResponseDTO();
if (!jwtUtil.isTokenValid(refreshToken)) {
throw new RuntimeException("Refresh Token 已过期");
}
Long userId = Long.valueOf(jwtUtil.parseClaims(refreshToken).getSubject()); private final JwtPropertiesConfiguration jwtPropertiesConfiguration;
String cacheKey = String.format(RedisKeyConstants.REFRESH_TOKEN_KEY_PREFIX, userId); @Override
String cacheValue = stringRedisTemplate.opsForValue().get(cacheKey); public UserLoginResponseDTO userLogin(UserLoginRequestDTO requestParam) {
// 1.校验
userLoginRequestParamVerifyContext.handler(ChainMarkEnums.USER_LOGIN_REQ_PARAM_VERIFY.getMarkName(),
requestParam);
// 如果调用user-service失败那么就说明是系统内部错误
Result<UserAuthRespDTO> userResp = userClient.getUserByUserName(requestParam.getUserAccount());
if (userResp.isFail()) {
log.error("调用user-service返回失败{}", userResp.getMessage());
throw new ServiceException(ErrorCode.SYSTEM_ERROR);
}
UserAuthRespDTO user = userResp.getData();
if (cacheValue == null || !cacheValue.equals(refreshToken)) { if (ObjectUtil.isNull(user) || !BCrypt.checkpw(requestParam.getUserPassword(), user.getUserPassword())) {
throw new RuntimeException("Refresh Token 已失效"); throw new ServiceException("用户不存在或者密码错误", ErrorCode.NOT_LOGIN_ERROR);
} }
// 生成 JWT
String accessToken = jwtUtil.generateAccessToken(user);
String refreshToken = jwtUtil.generateRefreshToken(user.getId());
UserLoginResponseDTO resp = new UserLoginResponseDTO();
resp.setId(user.getId());
resp.setUserAccount(user.getUserAccount());
resp.setAccessToken(accessToken);
resp.setRefreshToken(refreshToken);
// 再次签发新的 Access Token // refresh token存入到REDIS里面
// 此处你需要查用户,拿 userName, role stringRedisTemplate.opsForValue()
Result<UserAuthRespDTO> userResult = userClient.getUserById(String.valueOf(userId)); .set(String.format(RedisKeyConstants.REFRESH_TOKEN_KEY_PREFIX, user.getId()), refreshToken,
if (userResult.isFail()) { jwtPropertiesConfiguration.getRefreshExpire(), TimeUnit.MILLISECONDS);
log.error("通过id查找用户失败:{}", userResult.getMessage()); return resp;
throw new ServiceException(ErrorCode.SYSTEM_ERROR); }
}
UserAuthRespDTO user = userResult.getData(); /**
String newAccessToken = jwtUtil.generateAccessToken(user); * 更新access token使用refresh token
* @param refreshToken
* @return
*/
@Override
public UserLoginResponseDTO refreshToken(String refreshToken) {
UserLoginResponseDTO userLoginResponseDTO = new UserLoginResponseDTO();
if (!jwtUtil.isTokenValid(refreshToken)) {
throw new RuntimeException("Refresh Token 已过期");
}
Long userId = Long.valueOf(jwtUtil.parseClaims(refreshToken).getSubject());
String cacheKey = String.format(RedisKeyConstants.REFRESH_TOKEN_KEY_PREFIX, userId);
String cacheValue = stringRedisTemplate.opsForValue().get(cacheKey);
if (cacheValue == null || !cacheValue.equals(refreshToken)) {
throw new RuntimeException("Refresh Token 已失效");
}
// 再次签发新的 Access Token
// 此处你需要查用户,拿 userName, role
Result<UserAuthRespDTO> userResult = userClient.getUserById(String.valueOf(userId));
if (userResult.isFail()) {
log.error("通过id查找用户失败:{}", userResult.getMessage());
throw new ServiceException(ErrorCode.SYSTEM_ERROR);
}
UserAuthRespDTO user = userResult.getData();
String newAccessToken = jwtUtil.generateAccessToken(user);
// 设置refresh token和access token
userLoginResponseDTO.setRefreshToken(refreshToken);
userLoginResponseDTO.setAccessToken(newAccessToken);
return userLoginResponseDTO;
}
//设置refresh token和access token
userLoginResponseDTO.setRefreshToken(refreshToken);
userLoginResponseDTO.setAccessToken(newAccessToken);
return userLoginResponseDTO;
}
} }

View File

@@ -17,58 +17,56 @@ import java.util.Map;
@Component @Component
public class JwtUtil { public class JwtUtil {
private final JwtPropertiesConfiguration jwtConfig; private final JwtPropertiesConfiguration jwtConfig;
private SecretKey getSigningKey() { private SecretKey getSigningKey() {
return Keys.hmacShaKeyFor(jwtConfig.getSecret().getBytes()); return Keys.hmacShaKeyFor(jwtConfig.getSecret().getBytes());
} }
/** 生成 Access Token */ /** 生成 Access Token */
public String generateAccessToken(UserAuthRespDTO user) { public String generateAccessToken(UserAuthRespDTO user) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
Map<String, Object> claims = new HashMap<>(); Map<String, Object> claims = new HashMap<>();
claims.put("userId", user.getId()); claims.put("userId", user.getId());
claims.put("userName", user.getUserName()); claims.put("userName", user.getUserName());
claims.put("role", user.getUserRole()); claims.put("role", user.getUserRole());
return Jwts.builder() return Jwts.builder()
.subject(user.getUserAccount()) .subject(user.getUserAccount())
.issuedAt(new Date(now)) .issuedAt(new Date(now))
.expiration(new Date(now + jwtConfig.getAccessExpire())) .expiration(new Date(now + jwtConfig.getAccessExpire()))
.claims(claims) .claims(claims)
.signWith(getSigningKey(), Jwts.SIG.HS256) .signWith(getSigningKey(), Jwts.SIG.HS256)
.compact(); .compact();
} }
/** 生成 Refresh Token只含 userId */ /** 生成 Refresh Token只含 userId */
public String generateRefreshToken(Long userId) { public String generateRefreshToken(Long userId) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
return Jwts.builder() return Jwts.builder()
.subject(String.valueOf(userId)) .subject(String.valueOf(userId))
.issuedAt(new Date(now)) .issuedAt(new Date(now))
.expiration(new Date(now + jwtConfig.getRefreshExpire())) .expiration(new Date(now + jwtConfig.getRefreshExpire()))
.signWith(getSigningKey(), Jwts.SIG.HS256) .signWith(getSigningKey(), Jwts.SIG.HS256)
.compact(); .compact();
} }
/** 解析 Token */ /** 解析 Token */
public Claims parseClaims(String token) { public Claims parseClaims(String token) {
return Jwts.parser() return Jwts.parser().verifyWith(getSigningKey()).build().parseSignedClaims(token).getPayload();
.verifyWith(getSigningKey()) }
.build()
.parseSignedClaims(token) /** 校验 Token 是否过期 */
.getPayload(); public boolean isTokenValid(String token) {
} try {
Claims claims = parseClaims(token);
return claims.getExpiration().after(new Date());
}
catch (Exception ignored) {
return false;
}
}
/** 校验 Token 是否过期 */
public boolean isTokenValid(String token) {
try {
Claims claims = parseClaims(token);
return claims.getExpiration().after(new Date());
} catch (Exception ignored) {
return false;
}
}
} }

View File

@@ -15,56 +15,47 @@ import java.time.format.DateTimeFormatter;
@Component @Component
public class EnvironmentBanner implements ApplicationListener<ApplicationReadyEvent> { public class EnvironmentBanner implements ApplicationListener<ApplicationReadyEvent> {
@Value("${spring.application.name:unknown}") @Value("${spring.application.name:unknown}")
private String appName; private String appName;
@Override @Override
public void onApplicationEvent(ApplicationReadyEvent event) { public void onApplicationEvent(ApplicationReadyEvent event) {
Environment env = event.getApplicationContext().getEnvironment(); Environment env = event.getApplicationContext().getEnvironment();
// Active profiles // Active profiles
String profiles = String.join(",", env.getActiveProfiles()); String profiles = String.join(",", env.getActiveProfiles());
if (profiles.isEmpty()) { if (profiles.isEmpty()) {
profiles = "default"; profiles = "default";
} }
// Port // Port
String port = env.getProperty("server.port", "unknown"); String port = env.getProperty("server.port", "unknown");
// JVM info // JVM info
String jvm = System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")"; String jvm = System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")";
// PID // PID
String pid = ManagementFactory.getRuntimeMXBean().getPid() + ""; String pid = ManagementFactory.getRuntimeMXBean().getPid() + "";
// Time // Time
String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// Git commit id (如果没有 git.properties不会报错) // Git commit id (如果没有 git.properties不会报错)
String gitCommit = env.getProperty("git.commit.id.abbrev", "N/A"); String gitCommit = env.getProperty("git.commit.id.abbrev", "N/A");
printBanner(appName, profiles, port, jvm, pid, time, gitCommit); printBanner(appName, profiles, port, jvm, pid, time, gitCommit);
} }
private void printBanner(String appName, private void printBanner(String appName, String profiles, String port, String jvm, String pid, String time,
String profiles, String gitCommit) {
String port,
String jvm, String banner = "\n" + "------------------------------------------------------------\n"
String pid, + " ✨AI Online Judge✨ - " + appName + "\n" + " Environment : " + profiles + "\n" + " Port : "
String time, + port + "\n" + " Git Commit : " + gitCommit + "\n" + " JVM : " + jvm + "\n"
String gitCommit) { + " PID : " + pid + "\n" + " Started At : " + time + "\n"
+ "------------------------------------------------------------\n";
System.out.println(banner);
}
String banner = "\n" +
"------------------------------------------------------------\n" +
" ✨AI Online Judge✨ - " + appName + "\n" +
" Environment : " + profiles + "\n" +
" Port : " + port + "\n" +
" Git Commit : " + gitCommit + "\n" +
" JVM : " + jvm + "\n" +
" PID : " + pid + "\n" +
" Started At : " + time + "\n" +
"------------------------------------------------------------\n";
System.out.println(banner);
}
} }

View File

@@ -1,6 +1,5 @@
package cn.meowrain.aioj.backend.framework.core.config; package cn.meowrain.aioj.backend.framework.core.config;
import cn.meowrain.aioj.backend.framework.core.exception.handler.GlobalExceptionHandler; import cn.meowrain.aioj.backend.framework.core.exception.handler.GlobalExceptionHandler;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@@ -8,8 +7,10 @@ import org.springframework.context.annotation.Bean;
* 注册为bean,全局异常拦截器 * 注册为bean,全局异常拦截器
*/ */
public class WebAutoConfiguration { public class WebAutoConfiguration {
@Bean
public GlobalExceptionHandler globalExceptionHandler() { @Bean
return new GlobalExceptionHandler(); public GlobalExceptionHandler globalExceptionHandler() {
} return new GlobalExceptionHandler();
}
} }

View File

@@ -3,17 +3,17 @@ package cn.meowrain.aioj.backend.framework.core.designpattern.chains;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
public interface AbstractChianHandler<T> extends Ordered { public interface AbstractChianHandler<T> extends Ordered {
/**
* 执行责任链逻辑
*
* @param requestParam 责任链执行入参
*/
void handle(T requestParam);
/** /**
* 责任链组件标识 * 执行责任链逻辑
* * @param requestParam 责任链执行入参
* @return String */
*/ void handle(T requestParam);
String mark();
/**
* 责任链组件标识
* @return String
*/
String mark();
} }

View File

@@ -19,53 +19,50 @@ import java.util.stream.Collectors;
@Component @Component
@Slf4j @Slf4j
public class CommonChainContext<T> implements ApplicationContextAware, CommandLineRunner { public class CommonChainContext<T> implements ApplicationContextAware, CommandLineRunner {
private ApplicationContext applicationContext;
@Override private ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
private final Map<String, List<AbstractChianHandler<T>>> abstractChainHandlerMap = new HashMap<>(); @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public void handler(String mark, T requestParam) { private final Map<String, List<AbstractChianHandler<T>>> abstractChainHandlerMap = new HashMap<>();
List<AbstractChianHandler<T>> merchantAdminAbstractChainHandlers = abstractChainHandlerMap.get(mark);
if (merchantAdminAbstractChainHandlers == null || merchantAdminAbstractChainHandlers.isEmpty()) {
throw new RuntimeException(String.format("[%s] Chain of Responsibility ID is undefined.", mark));
}
merchantAdminAbstractChainHandlers.forEach(h -> {
h.handle(requestParam);
});
}
@Override public void handler(String mark, T requestParam) {
public void run(String... args) throws Exception { List<AbstractChianHandler<T>> merchantAdminAbstractChainHandlers = abstractChainHandlerMap.get(mark);
log.info("【责任链路初始化】开始加载并分组所有处理器..."); if (merchantAdminAbstractChainHandlers == null || merchantAdminAbstractChainHandlers.isEmpty()) {
applicationContext.getBeansOfType(AbstractChianHandler.class) throw new RuntimeException(String.format("[%s] Chain of Responsibility ID is undefined.", mark));
.values() }
.forEach(handler -> { merchantAdminAbstractChainHandlers.forEach(h -> {
// 打印当前处理器的类名和它所属的 ChainMark h.handle(requestParam);
String handlerName = handler.getClass().getSimpleName(); });
String mark = handler.mark(); }
log.info(" -> 发现处理器:{},归属链路:{}", handlerName, mark);
abstractChainHandlerMap
.computeIfAbsent(handler.mark(), k -> new ArrayList<>())
.add(handler);
});
// 步骤 2: 对每个链路中的处理器进行排序 (Sort 阶段) @Override
abstractChainHandlerMap.forEach((mark, handlers) -> { public void run(String... args) throws Exception {
handlers.sort(Comparator.comparing(Ordered::getOrder)); log.info("【责任链路初始化】开始加载并分组所有处理器...");
applicationContext.getBeansOfType(AbstractChianHandler.class).values().forEach(handler -> {
// 打印当前处理器的类名和它所属的 ChainMark
String handlerName = handler.getClass().getSimpleName();
String mark = handler.mark();
log.info(" -> 发现处理器:{},归属链路:{}", handlerName, mark);
abstractChainHandlerMap.computeIfAbsent(handler.mark(), k -> new ArrayList<>()).add(handler);
});
// 打印排序后的 Bean 列表 // 步骤 2: 对每个链路中的处理器进行排序 (Sort 阶段)
String sortedList = handlers.stream() abstractChainHandlerMap.forEach((mark, handlers) -> {
.map(h -> String.format("%s (Order:%d)", h.getClass().getSimpleName(), h.getOrder())) handlers.sort(Comparator.comparing(Ordered::getOrder));
.collect(Collectors.joining(" -> "));
log.info(" ✅ 链路 {} 排序完成:{}", mark, sortedList); // 打印排序后的 Bean 列表
}); String sortedList = handlers.stream()
.map(h -> String.format("%s (Order:%d)", h.getClass().getSimpleName(), h.getOrder()))
.collect(Collectors.joining(" -> "));
log.info("【责任链路初始化】所有处理器链已完全就绪。"); log.info(" ✅ 链路 {} 排序完成:{}", mark, sortedList);
} });
log.info("【责任链路初始化】所有处理器链已完全就绪。");
}
} }

View File

@@ -4,14 +4,17 @@ package cn.meowrain.aioj.backend.framework.core.enums;
* 删除枚举 * 删除枚举
*/ */
public enum DelStatusEnum { public enum DelStatusEnum {
STATUS_NORMAL("0"),
STATUS_DELETE("1");
private final String code; STATUS_NORMAL("0"), STATUS_DELETE("1");
DelStatusEnum(String code) {
this.code = code; private final String code;
}
public String code() { DelStatusEnum(String code) {
return this.code; this.code = code;
} }
public String code() {
return this.code;
}
} }

View File

@@ -1,38 +1,35 @@
package cn.meowrain.aioj.backend.framework.core.errorcode; package cn.meowrain.aioj.backend.framework.core.errorcode;
public enum ErrorCode implements IErrorCode { public enum ErrorCode implements IErrorCode {
SUCCESS("0", "ok"),
PARAMS_ERROR("40000", "请求参数错误"),
NOT_LOGIN_ERROR("40100", "未登录"),
NO_AUTH_ERROR("40101", "无权限"),
NOT_FOUND_ERROR("40400", "请求数据不存在"),
FORBIDDEN_ERROR("40300", "禁止访问"),
SYSTEM_ERROR("50000", "系统内部异常"),
OPERATION_ERROR("50001", "操作失败"),
API_REQUEST_ERROR("50010", "接口调用失败");
/**
* 状态码
*/
private final String code; SUCCESS("0", "ok"), PARAMS_ERROR("40000", "请求参数错误"), NOT_LOGIN_ERROR("40100", "未登录"), NO_AUTH_ERROR("40101", "无权限"),
NOT_FOUND_ERROR("40400", "请求数据不存在"), FORBIDDEN_ERROR("40300", "禁止访问"), SYSTEM_ERROR("50000", "系统内部异常"),
OPERATION_ERROR("50001", "操作失败"), API_REQUEST_ERROR("50010", "接口调用失败");
/** /**
* 信息 * 状态码
*/ */
private final String message;
ErrorCode(String code, String message) { private final String code;
this.code = code;
this.message = message;
}
@Override /**
public String code() { * 信息
return code; */
} private final String message;
ErrorCode(String code, String message) {
this.code = code;
this.message = message;
}
@Override
public String code() {
return code;
}
@Override
public String message() {
return message;
}
@Override
public String message() {
return message;
}
} }

View File

@@ -1,6 +1,9 @@
package cn.meowrain.aioj.backend.framework.core.errorcode; package cn.meowrain.aioj.backend.framework.core.errorcode;
public interface IErrorCode { public interface IErrorCode {
String code();
String message(); String code();
String message();
} }

View File

@@ -12,13 +12,16 @@ import java.util.Optional;
@Getter @Getter
public class AbstractException extends RuntimeException { public class AbstractException extends RuntimeException {
public final String errorCode;
public final String errorMessage;
public AbstractException(String message, Throwable throwable, IErrorCode errorCode) { public final String errorCode;
super(message);
this.errorCode = errorCode.code(); public final String errorMessage;
this.errorMessage = Optional.ofNullable(StringUtils.hasLength(message) ? message : null)
.orElse(errorCode.message()); public AbstractException(String message, Throwable throwable, IErrorCode errorCode) {
} super(message);
this.errorCode = errorCode.code();
this.errorMessage = Optional.ofNullable(StringUtils.hasLength(message) ? message : null)
.orElse(errorCode.message());
}
} }

View File

@@ -8,21 +8,22 @@ import lombok.ToString;
* 客户端异常 * 客户端异常
*/ */
@ToString @ToString
public class ClientException extends AbstractException{ public class ClientException extends AbstractException {
public ClientException(String message, Throwable throwable, IErrorCode errorCode) {
super(message, throwable, errorCode);
}
public ClientException(IErrorCode errorCode) { public ClientException(String message, Throwable throwable, IErrorCode errorCode) {
this(null, null, errorCode); super(message, throwable, errorCode);
} }
public ClientException(String message, IErrorCode errorCode) { public ClientException(IErrorCode errorCode) {
this(message, null, errorCode); this(null, null, errorCode);
} }
public ClientException(String message) { public ClientException(String message, IErrorCode errorCode) {
this(message, null, ErrorCode.PARAMS_ERROR); this(message, null, errorCode);
} }
public ClientException(String message) {
this(message, null, ErrorCode.PARAMS_ERROR);
}
} }

View File

@@ -8,20 +8,22 @@ import lombok.ToString;
* 调用第三方服务异常 * 调用第三方服务异常
*/ */
@ToString @ToString
public class RemoteException extends AbstractException{ public class RemoteException extends AbstractException {
public RemoteException(IErrorCode errorCode) {
this(null, null, errorCode);
}
public RemoteException(String message, IErrorCode errorCode) { public RemoteException(IErrorCode errorCode) {
this(message, null, errorCode); this(null, null, errorCode);
} }
public RemoteException(String message, Throwable throwable, IErrorCode errorCode) { public RemoteException(String message, IErrorCode errorCode) {
super(message, throwable, errorCode); this(message, null, errorCode);
} }
public RemoteException(String message) {
this(message, null, ErrorCode.API_REQUEST_ERROR); public RemoteException(String message, Throwable throwable, IErrorCode errorCode) {
} super(message, throwable, errorCode);
}
public RemoteException(String message) {
this(message, null, ErrorCode.API_REQUEST_ERROR);
}
} }

View File

@@ -9,20 +9,21 @@ import lombok.ToString;
*/ */
@ToString @ToString
public class ServiceException extends AbstractException { public class ServiceException extends AbstractException {
public ServiceException(String message, IErrorCode errorCode) {
this(message, null, errorCode);
}
public ServiceException(String message) { public ServiceException(String message, IErrorCode errorCode) {
this(message, null, ErrorCode.SYSTEM_ERROR); this(message, null, errorCode);
} }
public ServiceException(IErrorCode errorCode) { public ServiceException(String message) {
this(null, null, errorCode); this(message, null, ErrorCode.SYSTEM_ERROR);
} }
public ServiceException(String message, Throwable throwable, IErrorCode errorCode) { public ServiceException(IErrorCode errorCode) {
super(message, throwable, errorCode); this(null, null, errorCode);
} }
public ServiceException(String message, Throwable throwable, IErrorCode errorCode) {
super(message, throwable, errorCode);
}
} }

View File

@@ -20,64 +20,68 @@ import java.util.List;
@Slf4j @Slf4j
@RestControllerAdvice @RestControllerAdvice
public class GlobalExceptionHandler { public class GlobalExceptionHandler {
/**
* 捕获所有参数错误,然后统一捕获并且抛出
* @param request {@link HttpServletRequest}
* @param ex {@link org.springframework.validation.method.MethodValidationException}
* @return {@link Result<Void>}
*/
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public Result<Void> validExceptionHandler(HttpServletRequest request, MethodArgumentNotValidException ex) {
BindingResult bindingResult = ex.getBindingResult();
// 收集所有错误字段
List<String> errorMessages = bindingResult.getFieldErrors().stream()
.map(FieldError::getDefaultMessage).toList();
String exceptionMessage = String.join(",", errorMessages);
log.error("[{}] {} [ex] {}", request.getMethod(), getUrl(request), exceptionMessage);
return Results.paramsValidFailure();
}
/** /**
* 抽象异常捕获其 * 捕获所有参数错误,然后统一捕获并且抛出
* @param request {@link HttpServletRequest} * @param request {@link HttpServletRequest}
* @param ex {@link AbstractException} * @param ex {@link org.springframework.validation.method.MethodValidationException}
* @return {@link Result<Void>} * @return {@link Result<Void>}
*/ */
@ExceptionHandler(value = {AbstractException.class}) @ExceptionHandler(value = MethodArgumentNotValidException.class)
public Result<Void> abstractExceptionHandler(HttpServletRequest request,AbstractException ex ) { public Result<Void> validExceptionHandler(HttpServletRequest request, MethodArgumentNotValidException ex) {
if (ex.getCause() != null) { BindingResult bindingResult = ex.getBindingResult();
log.error("[{}] {} [ex] {}", request.getMethod(), request.getRequestURL().toString(), ex, ex.getCause()); // 收集所有错误字段
return Results.failure(ex); List<String> errorMessages = bindingResult.getFieldErrors()
} .stream()
StringBuilder stackTraceBuilder = new StringBuilder(); .map(FieldError::getDefaultMessage)
stackTraceBuilder.append(ex.getClass().getName()).append(": ").append(ex.getErrorMessage()).append("\n"); .toList();
StackTraceElement[] stackTrace = ex.getStackTrace(); String exceptionMessage = String.join(",", errorMessages);
for (int i = 0; i < Math.min(5, stackTrace.length); i++) { log.error("[{}] {} [ex] {}", request.getMethod(), getUrl(request), exceptionMessage);
stackTraceBuilder.append("\tat ").append(stackTrace[i]).append("\n"); return Results.paramsValidFailure();
} }
log.error("[{}] {} [ex] {} \n\n{}", request.getMethod(), request.getRequestURL().toString(), ex,
stackTraceBuilder);
return Results.failure(ex);
}
/** /**
* 拦截未捕获异常 * 抽象异常捕获其
*/ * @param request {@link HttpServletRequest}
@ExceptionHandler(value = Throwable.class) * @param ex {@link AbstractException}
public Result<Void> defaultErrorHandler(HttpServletRequest request, Throwable throwable) { * @return {@link Result<Void>}
log.error("[{}] {} ", request.getMethod(), getUrl(request), throwable); */
return Results.failure(); @ExceptionHandler(value = { AbstractException.class })
} public Result<Void> abstractExceptionHandler(HttpServletRequest request, AbstractException ex) {
if (ex.getCause() != null) {
log.error("[{}] {} [ex] {}", request.getMethod(), request.getRequestURL().toString(), ex, ex.getCause());
return Results.failure(ex);
}
StringBuilder stackTraceBuilder = new StringBuilder();
stackTraceBuilder.append(ex.getClass().getName()).append(": ").append(ex.getErrorMessage()).append("\n");
StackTraceElement[] stackTrace = ex.getStackTrace();
for (int i = 0; i < Math.min(5, stackTrace.length); i++) {
stackTraceBuilder.append("\tat ").append(stackTrace[i]).append("\n");
}
log.error("[{}] {} [ex] {} \n\n{}", request.getMethod(), request.getRequestURL().toString(), ex,
stackTraceBuilder);
return Results.failure(ex);
}
/**
* 拦截未捕获异常
*/
@ExceptionHandler(value = Throwable.class)
public Result<Void> defaultErrorHandler(HttpServletRequest request, Throwable throwable) {
log.error("[{}] {} ", request.getMethod(), getUrl(request), throwable);
return Results.failure();
}
/**
* 获取请求URL
* @param request {@link HttpServletRequest}
* @return String
*/
private String getUrl(HttpServletRequest request) {
if (!StringUtils.hasText(request.getQueryString())) {
return request.getRequestURI();
}
return request.getRequestURL() + "?" + request.getQueryString();
}
/**
* 获取请求URL
* @param request {@link HttpServletRequest}
* @return String
*/
private String getUrl(HttpServletRequest request) {
if (!StringUtils.hasText(request.getQueryString())) {
return request.getRequestURI();
}
return request.getRequestURL() + "?" + request.getQueryString();
}
} }

View File

@@ -12,39 +12,44 @@ import java.io.Serializable;
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class Result<T> implements Serializable { public class Result<T> implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* 正确返回码
* */
public static final String SUCCESS_CODE = "0";
/**
* 响应码
*/
private String code;
/**
* 响应数据
*/
private T data;
/**
* 响应信息
*/
private String message;
/** @Serial
* 返回是否是正确响应 private static final long serialVersionUID = 1L;
*
* @return boolean /**
*/ * 正确返回码
public boolean isSuccess() { */
return SUCCESS_CODE.equals(code); public static final String SUCCESS_CODE = "0";
}
/**
* 响应码
*/
private String code;
/**
* 响应数据
*/
private T data;
/**
* 响应信息
*/
private String message;
/**
* 返回是否是正确响应
* @return boolean
*/
public boolean isSuccess() {
return SUCCESS_CODE.equals(code);
}
/**
* 返回是否是错误响应
* @return boolean
*/
public boolean isFail() {
return !isSuccess();
}
/**
* 返回是否是错误响应
* @return boolean
*/
public boolean isFail() {
return !isSuccess();
}
} }

View File

@@ -1,6 +1,5 @@
package cn.meowrain.aioj.backend.framework.core.web; package cn.meowrain.aioj.backend.framework.core.web;
import cn.meowrain.aioj.backend.framework.core.errorcode.ErrorCode; import cn.meowrain.aioj.backend.framework.core.errorcode.ErrorCode;
import cn.meowrain.aioj.backend.framework.core.exception.AbstractException; import cn.meowrain.aioj.backend.framework.core.exception.AbstractException;
@@ -10,70 +9,61 @@ import java.util.Optional;
* 构建响应的工具类 * 构建响应的工具类
*/ */
public final class Results { public final class Results {
/**
* 成功响应,不返回任何信息
*
* @return {@link Result<Void>}
*/
public static Result<Void> success() {
return new Result<Void>().setCode(Result.SUCCESS_CODE);
}
/** /**
* 成功响应 返回数据 * 成功响应,不返回任何信息
* * @return {@link Result<Void>}
* @param data 返回的响应体信息 */
* @param <T> 泛型 public static Result<Void> success() {
* @return {@link Result<T>} return new Result<Void>().setCode(Result.SUCCESS_CODE);
*/ }
public static <T> Result<T> success(T data) {
return new Result<T>().setCode(Result.SUCCESS_CODE)
.setData(data);
}
/** /**
* 客户端请求参数错误 * 成功响应 返回数据
* @return {@link Result<Void>} * @param data 返回的响应体信息
*/ * @param <T> 泛型
public static Result<Void> paramsValidFailure() { * @return {@link Result<T>}
return failure(ErrorCode.PARAMS_ERROR.code(), ErrorCode.PARAMS_ERROR.message()); */
} public static <T> Result<T> success(T data) {
return new Result<T>().setCode(Result.SUCCESS_CODE).setData(data);
}
/** /**
* 服务端错误默认响应 -- <b>内部错误</b> * 客户端请求参数错误
* * @return {@link Result<Void>}
* @return {@link Result<Void>} */
*/ public static Result<Void> paramsValidFailure() {
public static Result<Void> failure() { return failure(ErrorCode.PARAMS_ERROR.code(), ErrorCode.PARAMS_ERROR.message());
return new Result<Void>().setCode(ErrorCode.SYSTEM_ERROR.code()) }
.setMessage(ErrorCode.SYSTEM_ERROR.message());
}
/** /**
* 服务端错误响应 - 接收一个AbstractException里面定义了错误码和错误信息 * 服务端错误默认响应 -- <b>内部错误</b>
* * @return {@link Result<Void>}
* @param exception {@link AbstractException} */
* @return {@link Result<Void>} public static Result<Void> failure() {
*/ return new Result<Void>().setCode(ErrorCode.SYSTEM_ERROR.code()).setMessage(ErrorCode.SYSTEM_ERROR.message());
public static Result<Void> failure(AbstractException exception) { }
String errorCode = Optional.ofNullable(exception.getErrorCode()).orElse(ErrorCode.SYSTEM_ERROR.code());
String errorMessage = Optional.ofNullable(exception.getMessage()).orElse(ErrorCode.SYSTEM_ERROR.message());
return new Result<Void>() /**
.setCode(errorCode) * 服务端错误响应 - 接收一个AbstractException里面定义了错误码和错误信息
.setMessage(errorMessage); * @param exception {@link AbstractException}
} * @return {@link Result<Void>}
*/
public static Result<Void> failure(AbstractException exception) {
String errorCode = Optional.ofNullable(exception.getErrorCode()).orElse(ErrorCode.SYSTEM_ERROR.code());
String errorMessage = Optional.ofNullable(exception.getMessage()).orElse(ErrorCode.SYSTEM_ERROR.message());
return new Result<Void>().setCode(errorCode).setMessage(errorMessage);
}
/**
* 服务端错误响应,自定义错误码和错误信息
* @param errorCode {@link String}
* @param errorMessage {@link String}
* @return {@link Result<Void>}
*/
public static Result<Void> failure(String errorCode, String errorMessage) {
return new Result<Void>().setCode(errorCode).setMessage(errorMessage);
}
/**
* 服务端错误响应,自定义错误码和错误信息
*
* @param errorCode {@link String}
* @param errorMessage {@link String}
* @return {@link Result<Void>}
*/
public static Result<Void> failure(String errorCode, String errorMessage) {
return new Result<Void>()
.setCode(errorCode)
.setMessage(errorMessage);
}
} }

View File

@@ -14,19 +14,23 @@ import org.springframework.scheduling.annotation.EnableAsync;
@EnableConfigurationProperties(AIOJLogPropertiesConfiguration.class) @EnableConfigurationProperties(AIOJLogPropertiesConfiguration.class)
@ConditionalOnProperty(value = "aioj.log.enabled", matchIfMissing = true) @ConditionalOnProperty(value = "aioj.log.enabled", matchIfMissing = true)
public class LogAutoConfiguration { public class LogAutoConfiguration {
/**
* 创建并返回SysLogListener的Bean实例 /**
*/ * 创建并返回SysLogListener的Bean实例
@Bean */
public SysLogListener sysLogListener(AIOJLogPropertiesConfiguration logProperties, RemoteLogService remoteLogService) { @Bean
return new SysLogListener(remoteLogService, logProperties); public SysLogListener sysLogListener(AIOJLogPropertiesConfiguration logProperties,
} RemoteLogService remoteLogService) {
/** return new SysLogListener(remoteLogService, logProperties);
* 返回切面类实例 }
* @return {@link SysLogAspect}
*/ /**
@Bean * 返回切面类实例
public SysLogAspect sysLogAspect() { * @return {@link SysLogAspect}
return new SysLogAspect(); */
} @Bean
public SysLogAspect sysLogAspect() {
return new SysLogAspect();
}
} }

View File

@@ -9,15 +9,17 @@ import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented @Documented
public @interface SysLog { public @interface SysLog {
/**
* 描述
* @return {@link String}
*/
String value() default "";
/** /**
* Spel表达式 * 描述
* @return 日志描述 * @return {@link String}
*/ */
String expression() default ""; String value() default "";
/**
* Spel表达式
* @return 日志描述
*/
String expression() default "";
} }

View File

@@ -15,17 +15,18 @@ import org.springframework.expression.EvaluationContext;
@RequiredArgsConstructor @RequiredArgsConstructor
public class SysLogAspect { public class SysLogAspect {
@Around("@annotation(sysLog)") @Around("@annotation(sysLog)")
public Object around(ProceedingJoinPoint joinPoint,SysLog sysLog) throws Throwable { public Object around(ProceedingJoinPoint joinPoint, SysLog sysLog) throws Throwable {
String strClassName = joinPoint.getTarget().getClass().getName(); String strClassName = joinPoint.getTarget().getClass().getName();
String strMethodName = joinPoint.getSignature().getName(); String strMethodName = joinPoint.getSignature().getName();
log.debug("[类名]:{},[方法]:{}", strClassName, strMethodName); log.debug("[类名]:{},[方法]:{}", strClassName, strMethodName);
String value = sysLog.value(); String value = sysLog.value();
String expression = sysLog.expression(); String expression = sysLog.expression();
if(StrUtil.isNotBlank(expression)) { if (StrUtil.isNotBlank(expression)) {
// 解析SPEL // 解析SPEL
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); MethodSignature signature = (MethodSignature) joinPoint.getSignature();
EvaluationContext context = SysLogUtils.getContext(point.getArgs(), signature.getMethod()); EvaluationContext context = SysLogUtils.getContext(point.getArgs(), signature.getMethod());
} }
} }
} }

View File

@@ -8,14 +8,17 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@Setter @Setter
@ConfigurationProperties(AIOJLogPropertiesConfiguration.PREFIX) @ConfigurationProperties(AIOJLogPropertiesConfiguration.PREFIX)
public class AIOJLogPropertiesConfiguration { public class AIOJLogPropertiesConfiguration {
public static final String PREFIX = "aioj.log";
/**
* 开启日志记录
*/
private boolean enabled = true;
/** public static final String PREFIX = "aioj.log";
* 请求报文最大存储长度
*/ /**
private Integer maxLength = 20000; * 开启日志记录
*/
private boolean enabled = true;
/**
* 请求报文最大存储长度
*/
private Integer maxLength = 20000;
} }

View File

@@ -12,81 +12,82 @@ import java.time.LocalDateTime;
@Data @Data
public class SysLog implements Serializable { public class SysLog implements Serializable {
@Serial @Serial
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** /**
* 编号 * 编号
*/ */
private Long id; private Long id;
/** /**
* 日志类型 * 日志类型
*/ */
private String logType; private String logType;
/** /**
* 日志标题 * 日志标题
*/ */
private String title; private String title;
/** /**
* 创建者 * 创建者
*/ */
private String createBy; private String createBy;
/** /**
* 创建时间 * 创建时间
*/ */
private LocalDateTime createTime; private LocalDateTime createTime;
/** /**
* 更新时间 * 更新时间
*/ */
private LocalDateTime updateTime; private LocalDateTime updateTime;
/** /**
* 操作IP地址 * 操作IP地址
*/ */
private String remoteAddr; private String remoteAddr;
/** /**
* 用户代理 * 用户代理
*/ */
private String userAgent; private String userAgent;
/** /**
* 请求URI * 请求URI
*/ */
private String requestUri; private String requestUri;
/** /**
* 操作方式 * 操作方式
*/ */
private String method; private String method;
/** /**
* 操作提交的数据 * 操作提交的数据
*/ */
private String params; private String params;
/** /**
* 执行时间 * 执行时间
*/ */
private Long time; private Long time;
/** /**
* 异常信息 * 异常信息
*/ */
private String exception; private String exception;
/** /**
* 服务ID * 服务ID
*/ */
private String serviceId; private String serviceId;
/**
* 删除标记
*/
private String delFlag;
/**
* 删除标记
*/
private String delFlag;
} }

View File

@@ -9,12 +9,15 @@ import java.io.Serial;
* 系统日志事件类 * 系统日志事件类
*/ */
public class SysLogEvent extends ApplicationEvent { public class SysLogEvent extends ApplicationEvent {
@Serial
private static final long serialVersionUID = 1L; @Serial
/** private static final long serialVersionUID = 1L;
* 构造方法根据源SysLog对象创建SysLogEvent
*/ /**
public SysLogEvent(SysLog source) { * 构造方法根据源SysLog对象创建SysLogEvent
super(source); */
} public SysLogEvent(SysLog source) {
super(source);
}
} }

View File

@@ -13,12 +13,12 @@ import java.io.Serializable;
@EqualsAndHashCode(callSuper = false) @EqualsAndHashCode(callSuper = false)
public class SysLogEventSource extends SysLogEvent implements Serializable { public class SysLogEventSource extends SysLogEvent implements Serializable {
@Serial @Serial
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** /**
* 参数重写成object * 参数重写成object
*/ */
private Object body; private Object body;
} }

View File

@@ -13,24 +13,27 @@ import org.springframework.scheduling.annotation.Async;
@RequiredArgsConstructor @RequiredArgsConstructor
public class SysLogListener implements InitializingBean { public class SysLogListener implements InitializingBean {
private final static ObjectMapper objectMapper = new ObjectMapper();
private final RemoteLogService remoteLogService; private final static ObjectMapper objectMapper = new ObjectMapper();
private final AIOJLogPropertiesConfiguration logProperties; private final RemoteLogService remoteLogService;
@SneakyThrows private final AIOJLogPropertiesConfiguration logProperties;
@Async
@Order
@EventListener(SysLogEvent.class)
public void saveLog(SysLogEvent sysLogEvent){
SysLogEventSource source = (SysLogEventSource) sysLogEvent.getSource();
SysLog sysLog = new SysLog();
BeanUtils.copyProperties(source, sysLog);
} @SneakyThrows
@Override @Async
public void afterPropertiesSet() throws Exception { @Order
@EventListener(SysLogEvent.class)
public void saveLog(SysLogEvent sysLogEvent) {
SysLogEventSource source = (SysLogEventSource) sysLogEvent.getSource();
SysLog sysLog = new SysLog();
BeanUtils.copyProperties(source, sysLog);
}
@Override
public void afterPropertiesSet() throws Exception {
}
}
} }

View File

@@ -6,13 +6,15 @@ import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
public class ApplicationLoggerInitializer implements EnvironmentPostProcessor, Ordered { public class ApplicationLoggerInitializer implements EnvironmentPostProcessor, Ordered {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
} @Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
}
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
} }

View File

@@ -3,7 +3,9 @@ package cn.meowrain.aioj.backend.framework.log.utils;
import cn.meowrain.aioj.backend.framework.log.event.SysLogEventSource; import cn.meowrain.aioj.backend.framework.log.event.SysLogEventSource;
public final class SysLogUtils { public final class SysLogUtils {
public static SysLogEventSource getSysLog() {
} public static SysLogEventSource getSysLog() {
}
} }

View File

@@ -9,19 +9,19 @@ import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
public class MybatisPlusAutoConfiguration { public class MybatisPlusAutoConfiguration {
@Bean @Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() { public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInterceptor()); interceptor.addInnerInterceptor(new PaginationInterceptor());
return interceptor; return interceptor;
} }
/** /**
* 创建并返回MybatisPlusMetaObjectHandler实例用于审计字段自动填充 * 创建并返回MybatisPlusMetaObjectHandler实例用于审计字段自动填充
*/ */
@Bean @Bean
public MybatisPlusMetaObjectHandler mybatisPlusMetaObjectHandler() { public MybatisPlusMetaObjectHandler mybatisPlusMetaObjectHandler() {
return new MybatisPlusMetaObjectHandler(); return new MybatisPlusMetaObjectHandler();
} }
} }

View File

@@ -11,38 +11,41 @@ import java.io.Serializable;
import java.time.LocalDateTime; import java.time.LocalDateTime;
/** /**
* 基础实体抽象类,包含通用实体字段 * 基础实体抽象类,包含通用实体字段
*/ */
@Getter @Getter
@Setter @Setter
public class BaseEntity implements Serializable { public class BaseEntity implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* 创建者
*/
@Schema(description = "创建人")
@TableField(fill = FieldFill.INSERT)
private String createBy;
/** @Serial
* 创建时间 private static final long serialVersionUID = 1L;
*/
@Schema(description = "创建时间")
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/** /**
* 更新 * 创建
*/ */
@Schema(description = "更新") @Schema(description = "创建")
@TableField(fill = FieldFill.INSERT_UPDATE) @TableField(fill = FieldFill.INSERT)
private String updateBy; private String createBy;
/**
* 创建时间
*/
@Schema(description = "创建时间")
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 更新者
*/
@Schema(description = "更新人")
@TableField(fill = FieldFill.INSERT_UPDATE)
private String updateBy;
/**
* 更新时间
*/
@Schema(description = "更新时间")
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
/**
* 更新时间
*/
@Schema(description = "更新时间")
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
} }

View File

@@ -17,54 +17,56 @@ import java.util.Optional;
*/ */
@Slf4j @Slf4j
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler { public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
log.debug("mybatis plus start insert fill ....");
LocalDateTime now = LocalDateTime.now();
fillValIfNullByName("createTime", now, metaObject, true); @Override
fillValIfNullByName("updateTime", now, metaObject, true); public void insertFill(MetaObject metaObject) {
fillValIfNullByName("createBy", getUserName(), metaObject, true); log.debug("mybatis plus start insert fill ....");
fillValIfNullByName("updateBy", getUserName(), metaObject, true); LocalDateTime now = LocalDateTime.now();
// 删除标记自动填充 fillValIfNullByName("createTime", now, metaObject, true);
fillValIfNullByName("delFlag", DelStatusEnum.STATUS_NORMAL.code(), metaObject, true); fillValIfNullByName("updateTime", now, metaObject, true);
} fillValIfNullByName("createBy", getUserName(), metaObject, true);
fillValIfNullByName("updateBy", getUserName(), metaObject, true);
@Override // 删除标记自动填充
public void updateFill(MetaObject metaObject) { fillValIfNullByName("delFlag", DelStatusEnum.STATUS_NORMAL.code(), metaObject, true);
log.debug("mybatis plus start update fill ...."); }
fillValIfNullByName("updateTime", LocalDateTime.now(), metaObject, true);
fillValIfNullByName("updateBy", getUserName(), metaObject, true);
}
private void fillValIfNullByName(String fieldName, Object fieldVal, MetaObject metaObject, boolean isCover) { @Override
//如果填充值为空 public void updateFill(MetaObject metaObject) {
if (fieldVal == null) { log.debug("mybatis plus start update fill ....");
return; fillValIfNullByName("updateTime", LocalDateTime.now(), metaObject, true);
} fillValIfNullByName("updateBy", getUserName(), metaObject, true);
// 没有 set 方法 }
if (!metaObject.hasSetter(fieldName)) {
return;
}
// field 类型相同时设置
Class<?> getterType = metaObject.getGetterType(fieldName);
if (ClassUtils.isAssignableValue(getterType, fieldVal)) {
metaObject.setValue(fieldName, fieldVal);
}
}
private Object getUserName() { private void fillValIfNullByName(String fieldName, Object fieldVal, MetaObject metaObject, boolean isCover) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); // 如果填充值为空
// 匿名接口直接返回 if (fieldVal == null) {
if (authentication instanceof AnonymousAuthenticationToken) { return;
return null; }
} // 没有 set 方法
if (!metaObject.hasSetter(fieldName)) {
return;
}
// field 类型相同时设置
Class<?> getterType = metaObject.getGetterType(fieldName);
if (ClassUtils.isAssignableValue(getterType, fieldVal)) {
metaObject.setValue(fieldName, fieldVal);
}
}
if (Optional.ofNullable(authentication).isPresent()) { private Object getUserName() {
return authentication.getName(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
} // 匿名接口直接返回
if (authentication instanceof AnonymousAuthenticationToken) {
return null;
}
if (Optional.ofNullable(authentication).isPresent()) {
return authentication.getName();
}
return null;
}
return null;
}
} }

View File

@@ -1,6 +1,5 @@
package cn.meowrain.backend.common.mybaits.plugins; package cn.meowrain.backend.common.mybaits.plugins;
import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ParameterUtils; import com.baomidou.mybatisplus.core.toolkit.ParameterUtils;
@@ -16,47 +15,49 @@ import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.session.RowBounds;
/** /**
* * 分页拦截器实现类,用于处理分页查询逻辑 * * 分页拦截器实现类,用于处理分页查询逻辑 *
* * <p> * <p>
* * 当分页大小小于0时自动设置为0防止全表查询 * * 当分页大小小于0时自动设置为0防止全表查询
*/ */
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@EqualsAndHashCode(callSuper = false) @EqualsAndHashCode(callSuper = false)
public class PaginationInterceptor extends PaginationInnerInterceptor { public class PaginationInterceptor extends PaginationInnerInterceptor {
/**
* 数据库类型
* <p>
* 查看 {@link #findIDialect(Executor)} 逻辑
*/
private DbType dbType;
/** /**
* 方言实现类 * 数据库类型
* <p> * <p>
* 查看 {@link #findIDialect(Executor)} 逻辑 * 查看 {@link #findIDialect(Executor)} 逻辑
*/ */
private IDialect dialect; private DbType dbType;
/**
* 方言实现类
* <p>
* 查看 {@link #findIDialect(Executor)} 逻辑
*/
private IDialect dialect;
public PaginationInterceptor(DbType dbType) { public PaginationInterceptor(DbType dbType) {
this.dbType = dbType; this.dbType = dbType;
} }
public PaginationInterceptor(IDialect dialect) { public PaginationInterceptor(IDialect dialect) {
this.dialect = dialect; this.dialect = dialect;
} }
/**
* 在执行查询前处理分页参数
*/
@Override
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds,
ResultHandler resultHandler, BoundSql boundSql) {
IPage<?> page = ParameterUtils.findPage(parameter).orElse(null);
// size 小于 0 直接设置为 0 , 即不查询任何数据
if (null != page && page.getSize() < 0) {
page.setSize(0);
}
super.beforeQuery(executor, ms, page, rowBounds, resultHandler, boundSql);
}
/**
* 在执行查询前处理分页参数
*/
@Override
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
IPage<?> page = ParameterUtils.findPage(parameter).orElse(null);
// size 小于 0 直接设置为 0 , 即不查询任何数据
if (null != page && page.getSize() < 0) {
page.setSize(0);
}
super.beforeQuery(executor, ms, page, rowBounds, resultHandler, boundSql);
}
} }

View File

@@ -5,10 +5,12 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
@EnableConfigurationProperties(value = {GatewayPropertiesConfiguration.class}) @EnableConfigurationProperties(value = { GatewayPropertiesConfiguration.class })
@SpringBootApplication @SpringBootApplication
public class AIOJGatewayApplication { public class AIOJGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(AIOJGatewayApplication.class, args); public static void main(String[] args) {
} SpringApplication.run(AIOJGatewayApplication.class, args);
}
} }

View File

@@ -12,57 +12,49 @@ import java.util.List;
/** /**
* 全局 CORS 配置WebFlux 环境使用 CorsWebFilter * 全局 CORS 配置WebFlux 环境使用 CorsWebFilter
* *
* WebFlux 不使用 Spring MVC 的 CorsFilter * WebFlux 不使用 Spring MVC 的 CorsFilter 而是使用专门的 CorsWebFilter 处理跨域。
* 而是使用专门的 CorsWebFilter 处理跨域。
* *
* 此配置实现了: * 此配置实现了: - 允许任意域名访问AllowedOriginPatterns = "*" - 允许所有请求方法GET、POST、PUT... - 允许所有请求头 -
* - 允许任意域名访问AllowedOriginPatterns = "*" * 允许跨域携带 CookieAllowCredentials - 对所有路径生效(/**
* - 允许所有请求方法GET、POST、PUT...
* - 允许所有请求头
* - 允许跨域携带 CookieAllowCredentials
* - 对所有路径生效(/**
*/ */
@Configuration @Configuration
public class CorsConfig { public class CorsConfig {
/** /**
* 注册全局 CORS 过滤器 * 注册全局 CORS 过滤器
* * @return CorsWebFilter 跨域过滤器
* @return CorsWebFilter 跨域过滤器 */
*/ @Bean
@Bean public CorsWebFilter corsWebFilter() {
public CorsWebFilter corsWebFilter() {
// 创建跨域配置对象 // 创建跨域配置对象
CorsConfiguration corsConfiguration = new CorsConfiguration(); CorsConfiguration corsConfiguration = new CorsConfiguration();
// 允许所有请求方式GET、POST、PUT、DELETE、OPTIONS... // 允许所有请求方式GET、POST、PUT、DELETE、OPTIONS...
corsConfiguration.addAllowedMethod("*"); corsConfiguration.addAllowedMethod("*");
// 是否允许携带 Cookie 信息。跨域默认不允许,需要显式开启 // 是否允许携带 Cookie 信息。跨域默认不允许,需要显式开启
corsConfiguration.setAllowCredentials(true); corsConfiguration.setAllowCredentials(true);
/** /**
* 允许跨域的来源域名 * 允许跨域的来源域名 使用 setAllowedOriginPatterns("*") 是 WebFlux 推荐方式, 因为
* 使用 setAllowedOriginPatterns("*") 是 WebFlux 推荐方式, * setAllowedOrigins("*") 在 allowCredentials=true 时会被拦截。
* 因为 setAllowedOrigins("*") 在 allowCredentials=true 时会被拦截。 */
*/ corsConfiguration.setAllowedOriginPatterns(List.of("*"));
corsConfiguration.setAllowedOriginPatterns(List.of("*"));
// 允许所有请求头,例如 Authorization、Content-Type 等 // 允许所有请求头,例如 Authorization、Content-Type 等
corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedHeader("*");
/** /**
* 基于 URL 的跨域配置源, * 基于 URL 的跨域配置源, PathPatternParser 用于解析路径模式(更高性能)
* PathPatternParser 用于解析路径模式(更高性能) */
*/ UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
UrlBasedCorsConfigurationSource source =
new UrlBasedCorsConfigurationSource(new PathPatternParser());
// 对所有路径应用跨域设置 // 对所有路径应用跨域设置
source.registerCorsConfiguration("/**", corsConfiguration); source.registerCorsConfiguration("/**", corsConfiguration);
// 创建并返回 WebFlux 专用的 CORS 过滤器
return new CorsWebFilter(source);
}
// 创建并返回 WebFlux 专用的 CORS 过滤器
return new CorsWebFilter(source);
}
} }

View File

@@ -6,9 +6,12 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = GatewayPropertiesConfiguration.PREFIX) @ConfigurationProperties(prefix = GatewayPropertiesConfiguration.PREFIX)
@Data @Data
public class GatewayPropertiesConfiguration { public class GatewayPropertiesConfiguration {
public static final String PREFIX = "aioj-backend-gateway";
/* public static final String PREFIX = "aioj-backend-gateway";
* 白名单放行
* */ /*
private String[] whiteList; * 白名单放行
*/
private String[] whiteList;
} }

View File

@@ -13,15 +13,16 @@ import reactor.core.publisher.Mono;
@RequiredArgsConstructor @RequiredArgsConstructor
public class AuthGlobalFilter implements GlobalFilter, Ordered { public class AuthGlobalFilter implements GlobalFilter, Ordered {
private final WebClient.Builder webClientBuilder; private final WebClient.Builder webClientBuilder;
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return null; return null;
} }
@Override
public int getOrder() {
return 0;
}
@Override
public int getOrder() {
return 0;
}
} }

View File

@@ -8,76 +8,76 @@ import java.time.LocalDateTime;
@Schema(description = "日志查询对象") @Schema(description = "日志查询对象")
public class SysLogDTO { public class SysLogDTO {
/** /**
* 编号 * 编号
*/ */
private Long id; private Long id;
/** /**
* 日志类型 * 日志类型
*/ */
@NotBlank(message = "日志类型不能为空") @NotBlank(message = "日志类型不能为空")
private String logType; private String logType;
/** /**
* 日志标题 * 日志标题
*/ */
@NotBlank(message = "日志标题不能为空") @NotBlank(message = "日志标题不能为空")
private String title; private String title;
/** /**
* 创建者 * 创建者
*/ */
private String createBy; private String createBy;
/** /**
* 更新时间 * 更新时间
*/ */
private LocalDateTime updateTime; private LocalDateTime updateTime;
/** /**
* 操作IP地址 * 操作IP地址
*/ */
private String remoteAddr; private String remoteAddr;
/** /**
* 用户代理 * 用户代理
*/ */
private String userAgent; private String userAgent;
/** /**
* 请求URI * 请求URI
*/ */
private String requestUri; private String requestUri;
/** /**
* 操作方式 * 操作方式
*/ */
private String method; private String method;
/** /**
* 操作提交的数据 * 操作提交的数据
*/ */
private String params; private String params;
/** /**
* 执行时间 * 执行时间
*/ */
private Long time; private Long time;
/** /**
* 异常信息 * 异常信息
*/ */
private String exception; private String exception;
/** /**
* 服务ID * 服务ID
*/ */
private String serviceId; private String serviceId;
/** /**
* 创建时间区间 [开始时间,结束时间] * 创建时间区间 [开始时间,结束时间]
*/ */
private LocalDateTime[] createTime; private LocalDateTime[] createTime;
} }

View File

@@ -7,7 +7,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication
@MapperScan("cn.meowrain.aioj.backend.userservice.dao.mapper") @MapperScan("cn.meowrain.aioj.backend.userservice.dao.mapper")
public class UserServiceApplication { public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args); public static void main(String[] args) {
} SpringApplication.run(UserServiceApplication.class, args);
}
} }

View File

@@ -5,20 +5,22 @@ import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor @RequiredArgsConstructor
public enum ChainMarkEnums { public enum ChainMarkEnums {
/**
* 用户注册请求验证
*/
USER_REGISTER_REQ_PARAM_VERIFY("USER_REGISTER_REQ_PARAM_VERIFY"),
/**
* 用户登录请求验证
*/
USER_LOGIN_REQ_PARAM_VERIFY("USER_LOGIN_REQ_PARAM_VERIFY");
@Getter /**
private final String markName; * 用户注册请求验证
*/
USER_REGISTER_REQ_PARAM_VERIFY("USER_REGISTER_REQ_PARAM_VERIFY"),
/**
* 用户登录请求验证
*/
USER_LOGIN_REQ_PARAM_VERIFY("USER_LOGIN_REQ_PARAM_VERIFY");
@Getter
private final String markName;
@Override
public String toString() {
return markName;
}
@Override
public String toString() {
return markName;
}
} }

View File

@@ -5,10 +5,7 @@ import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
@ComponentScans({ @ComponentScans({ @ComponentScan("cn.meowrain.aioj.backend.framework.core.banner") })
@ComponentScan("cn.meowrain.aioj.backend.framework.core.banner")
})
public class FrameworkConfiguration { public class FrameworkConfiguration {
} }

View File

@@ -16,23 +16,25 @@ import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
@EnableKnife4j @EnableKnife4j
public class SwaggerConfiguration implements ApplicationRunner { public class SwaggerConfiguration implements ApplicationRunner {
@Value("${server.port:8080}")
private String serverPort;
@Value("${server.servlet.context-path:}")
private String contextPath;
@Bean @Value("${server.port:8080}")
public OpenAPI customerOpenAPI() { private String serverPort;
return new OpenAPI()
.info(new Info() @Value("${server.servlet.context-path:}")
.title("AIOJ-用户微服务✨") private String contextPath;
.description("用户注册,用户登录等功能")
.version("v1.0.0") @Bean
.contact(new Contact().name("meowrain").email("meowrain@126.com")) public OpenAPI customerOpenAPI() {
.license(new License().name("MeowRain").url("https://meowrain.cn"))); return new OpenAPI().info(new Info().title("AIOJ-用户微服务✨")
} .description("用户注册,用户登录等功能")
@Override .version("v1.0.0")
public void run(ApplicationArguments args) throws Exception { .contact(new Contact().name("meowrain").email("meowrain@126.com"))
log.info("✨API Document: http://127.0.0.1:{}{}/doc.html", serverPort, contextPath); .license(new License().name("MeowRain").url("https://meowrain.cn")));
} }
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("✨API Document: http://127.0.0.1:{}{}/doc.html", serverPort, contextPath);
}
} }

View File

@@ -13,25 +13,24 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/v1/user") @RequestMapping("/v1/user")
public class UserController { public class UserController {
private final UserService userService; private final UserService userService;
@PostMapping("/register")
public Result<Long> register(@RequestBody UserRegisterRequestDTO userRegisterRequest) {
Long l = userService.userRegister(userRegisterRequest);
return Results.success(l);
}
@PostMapping("/register") @GetMapping("/inner/get-by-username")
public Result<Long> register(@RequestBody UserRegisterRequestDTO userRegisterRequest) { public Result<UserAuthRespDTO> getUserByUserName(@RequestParam("userAccount") String userAccount) {
Long l = userService.userRegister(userRegisterRequest); UserAuthRespDTO userAuthDTO = userService.findAuthInfoByUserAccount(userAccount);
return Results.success(l); return Results.success(userAuthDTO);
} }
@GetMapping("/inner/get-by-username") @GetMapping("/inner/get-by-userid")
public Result<UserAuthRespDTO> getUserByUserName(@RequestParam("userAccount") String userAccount) { public Result<UserAuthRespDTO> getUserById(@RequestParam("userId") String userid) {
UserAuthRespDTO userAuthDTO = userService.findAuthInfoByUserAccount(userAccount); UserAuthRespDTO userAuthRespDTO = userService.findAuthInfoByUserId(userid);
return Results.success(userAuthDTO); return Results.success(userAuthRespDTO);
} }
@GetMapping("/inner/get-by-userid")
public Result<UserAuthRespDTO> getUserById(@RequestParam("userId") String userid) {
UserAuthRespDTO userAuthRespDTO = userService.findAuthInfoByUserId(userid);
return Results.success(userAuthRespDTO);
}
} }

View File

@@ -12,69 +12,70 @@ import java.util.Date;
@Accessors(chain = true) @Accessors(chain = true)
public class User implements Serializable { public class User implements Serializable {
/** /**
* id * id
*/ */
@TableId(type = IdType.ASSIGN_ID) @TableId(type = IdType.ASSIGN_ID)
private Long id; private Long id;
/** /**
* 用户账号 * 用户账号
*/ */
private String userAccount; private String userAccount;
/** /**
* 用户密码 * 用户密码
*/ */
private String userPassword; private String userPassword;
/** /**
* 开放平台id * 开放平台id
*/ */
private String unionId; private String unionId;
/** /**
* 公众号openId * 公众号openId
*/ */
private String mpOpenId; private String mpOpenId;
/** /**
* 用户昵称 * 用户昵称
*/ */
private String userName; private String userName;
/** /**
* 用户头像 * 用户头像
*/ */
private String userAvatar; private String userAvatar;
/** /**
* 用户简介 * 用户简介
*/ */
private String userProfile; private String userProfile;
/** /**
* 用户角色user/admin/ban * 用户角色user/admin/ban
*/ */
private String userRole; private String userRole;
/** /**
* 创建时间 * 创建时间
*/ */
@TableField(fill = FieldFill.INSERT) @TableField(fill = FieldFill.INSERT)
private Date createTime; private Date createTime;
/** /**
* 更新时间 * 更新时间
*/ */
private Date updateTime; private Date updateTime;
/** /**
* 是否删除 * 是否删除
*/ */
@TableLogic @TableLogic
private Integer isDelete; private Integer isDelete;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
} }

View File

@@ -4,4 +4,5 @@ import cn.meowrain.aioj.backend.userservice.dao.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface UserMapper extends BaseMapper<User> { public interface UserMapper extends BaseMapper<User> {
} }

View File

@@ -12,32 +12,34 @@ import org.springframework.stereotype.Component;
@Component @Component
@Slf4j @Slf4j
public class UserRegisterRequestParamVerifyChain implements AbstractChianHandler<UserRegisterRequestDTO> { public class UserRegisterRequestParamVerifyChain implements AbstractChianHandler<UserRegisterRequestDTO> {
@Override
public void handle(UserRegisterRequestDTO requestParam) {
// 校验参数里面用户名和密码是不是空的
if (StringUtils.isAnyBlank(requestParam.getUserAccount(), requestParam.getUserPassword())) {
throw new ClientException("参数为空", ErrorCode.PARAMS_ERROR);
}
if (requestParam.getUserAccount().length() < 4) {
throw new ClientException("账号长度不小于4位", ErrorCode.PARAMS_ERROR);
}
if (requestParam.getUserPassword().length() < 8) {
throw new ClientException("密码长度不小于8位", ErrorCode.PARAMS_ERROR);
}
// 密码和校验密码相同
if (!requestParam.getUserPassword().equals(requestParam.getCheckPassword())) {
throw new ClientException("两次输入的密码不一致", ErrorCode.PARAMS_ERROR);
}
} @Override
public void handle(UserRegisterRequestDTO requestParam) {
// 校验参数里面用户名和密码是不是空的
if (StringUtils.isAnyBlank(requestParam.getUserAccount(), requestParam.getUserPassword())) {
throw new ClientException("参数为空", ErrorCode.PARAMS_ERROR);
}
if (requestParam.getUserAccount().length() < 4) {
throw new ClientException("账号长度不小于4位", ErrorCode.PARAMS_ERROR);
}
if (requestParam.getUserPassword().length() < 8) {
throw new ClientException("密码长度不小于8位", ErrorCode.PARAMS_ERROR);
}
// 密码和校验密码相同
if (!requestParam.getUserPassword().equals(requestParam.getCheckPassword())) {
throw new ClientException("两次输入的密码不一致", ErrorCode.PARAMS_ERROR);
}
@Override }
public String mark() {
return ChainMarkEnums.USER_REGISTER_REQ_PARAM_VERIFY.getMarkName(); @Override
} public String mark() {
return ChainMarkEnums.USER_REGISTER_REQ_PARAM_VERIFY.getMarkName();
}
@Override
public int getOrder() {
return 10;
}
@Override
public int getOrder() {
return 10;
}
} }

View File

@@ -6,4 +6,5 @@ import org.springframework.stereotype.Component;
@Component @Component
public class UserRegisterRequestParamVerifyContext extends CommonChainContext<UserRegisterRequestDTO> { public class UserRegisterRequestParamVerifyContext extends CommonChainContext<UserRegisterRequestDTO> {
} }

View File

@@ -5,6 +5,9 @@ import lombok.Data;
@Data @Data
public class UserLoginRequestDTO { public class UserLoginRequestDTO {
private String userAccount;
private String userPassword; private String userAccount;
private String userPassword;
} }

View File

@@ -5,7 +5,11 @@ import lombok.NoArgsConstructor;
@Data @Data
public class UserRegisterRequestDTO { public class UserRegisterRequestDTO {
private String userAccount;
private String userPassword; private String userAccount;
private String checkPassword;
private String userPassword;
private String checkPassword;
} }

View File

@@ -10,60 +10,59 @@ import java.util.Date;
@Data @Data
public class UserAuthRespDTO { public class UserAuthRespDTO {
/** /**
* id * id
*/ */
private Long id; private Long id;
/** /**
* 用户账号 * 用户账号
*/ */
private String userAccount; private String userAccount;
/**
* 用户密码
*/
private String userPassword;
/** /**
* 开放平台id * 用户密码
*/ */
private String unionId; private String userPassword;
/** /**
* 公众号openId * 开放平台id
*/ */
private String mpOpenId; private String unionId;
/** /**
* 用户昵称 * 公众号openId
*/ */
private String userName; private String mpOpenId;
/** /**
* 用户头像 * 用户昵称
*/ */
private String userAvatar; private String userName;
/** /**
* 用户简介 * 用户头像
*/ */
private String userProfile; private String userAvatar;
/** /**
* 用户角色user/admin/ban * 用户简介
*/ */
private String userRole; private String userProfile;
/** /**
* 创建时间 * 用户角色user/admin/ban
*/ */
private Date createTime; private String userRole;
/**
* 更新时间
*/
private Date updateTime;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
} }

View File

@@ -8,68 +8,69 @@ import java.util.Date;
@Data @Data
public class UserLoginResponseDTO implements Serializable { public class UserLoginResponseDTO implements Serializable {
/**
* id
*/
@TableId(type = IdType.ASSIGN_ID)
private Long id;
/** /**
* 用户账号 * id
*/ */
private String userAccount; @TableId(type = IdType.ASSIGN_ID)
private Long id;
/** /**
* 开放平台id * 用户账号
*/ */
private String unionId; private String userAccount;
/** /**
* 公众号openId * 开放平台id
*/ */
private String mpOpenId; private String unionId;
/** /**
* 用户昵称 * 公众号openId
*/ */
private String userName; private String mpOpenId;
/** /**
* 用户头像 * 用户昵称
*/ */
private String userAvatar; private String userName;
/** /**
* 用户简介 * 用户头像
*/ */
private String userProfile; private String userAvatar;
/** /**
* 用户角色user/admin/ban * 用户简介
*/ */
private String userRole; private String userProfile;
/** /**
* 创建时间 * 用户角色user/admin/ban
*/ */
private Date createTime; private String userRole;
/** /**
* 更新时间 * 创建时间
*/ */
private Date updateTime; private Date createTime;
/** /**
* 是否删除 * 更新时间
*/ */
private Date updateTime;
private Integer isDelete; /**
* 是否删除
*/
private Integer isDelete;
/** /**
* JWT令牌登录成功返回 * JWT令牌登录成功返回
*/ */
private String token; private String token;
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
} }

View File

@@ -9,5 +9,7 @@ import java.io.Serializable;
*/ */
@Data @Data
public class UserRegisterResponseDTO implements Serializable { public class UserRegisterResponseDTO implements Serializable {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
} }

View File

@@ -7,24 +7,25 @@ import cn.meowrain.aioj.backend.userservice.dto.resp.UserAuthRespDTO;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
public interface UserService extends IService<User> { public interface UserService extends IService<User> {
/**
* 用户注册
* @param request {@link cn.meowrain.aioj.backend.userservice.dto.req.UserRegisterRequestDTO}
* @return {@link Long}
*/
Long userRegister(UserRegisterRequestDTO request);
/**
* 用户注册
* @param request
* {@link cn.meowrain.aioj.backend.userservice.dto.req.UserRegisterRequestDTO}
* @return {@link Long}
*/
Long userRegister(UserRegisterRequestDTO request);
/**
* 根据用户账号查找用户认证信息
* @param userAccount
* @return
*/
UserAuthRespDTO findAuthInfoByUserAccount(String userAccount);
/** /**
* 根据用户账号查找用户认证信息 * 根据用户id查找用户认证信息
* @param userAccount */
* @return UserAuthRespDTO findAuthInfoByUserId(String userId);
*/
UserAuthRespDTO findAuthInfoByUserAccount(String userAccount);
/**
* 根据用户id查找用户认证信息
*/
UserAuthRespDTO findAuthInfoByUserId(String userId);
} }

View File

@@ -25,56 +25,61 @@ import java.util.Date;
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j @Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
private final UserRegisterRequestParamVerifyContext userRegisterRequestParamVerifyContext;
@Override private final UserRegisterRequestParamVerifyContext userRegisterRequestParamVerifyContext;
public Long userRegister(UserRegisterRequestDTO request) {
UserAuthRespDTO authInfoByUserAccount = findAuthInfoByUserAccount(request.getUserAccount());
if (authInfoByUserAccount != null) {
throw new ClientException("重复创建用户");
}
log.info("进行用户注册"); @Override
userRegisterRequestParamVerifyContext.handler(ChainMarkEnums.USER_REGISTER_REQ_PARAM_VERIFY.getMarkName(), public Long userRegister(UserRegisterRequestDTO request) {
request); UserAuthRespDTO authInfoByUserAccount = findAuthInfoByUserAccount(request.getUserAccount());
// 使用 BCrypt 加密密码 if (authInfoByUserAccount != null) {
Date now = new Date(); throw new ClientException("重复创建用户");
String salt = BCrypt.gensalt(); }
String encryptPassword = BCrypt.hashpw(request.getUserPassword(), salt);
User user = new User().setUserAccount(request.getUserAccount()).setUserPassword(encryptPassword)
.setUserRole("user").setCreateTime(now).setUpdateTime(now);
try {
// 需要修改表,使得用户名是唯一的
this.save(user);
} catch (DuplicateKeyException e) {
log.error("重复创建用户");
throw new ServiceException("用户名已存在", ErrorCode.SYSTEM_ERROR);
}
return user.getId(); log.info("进行用户注册");
} userRegisterRequestParamVerifyContext.handler(ChainMarkEnums.USER_REGISTER_REQ_PARAM_VERIFY.getMarkName(),
request);
// 使用 BCrypt 加密密码
Date now = new Date();
String salt = BCrypt.gensalt();
String encryptPassword = BCrypt.hashpw(request.getUserPassword(), salt);
User user = new User().setUserAccount(request.getUserAccount())
.setUserPassword(encryptPassword)
.setUserRole("user")
.setCreateTime(now)
.setUpdateTime(now);
try {
// 需要修改表,使得用户名是唯一的
this.save(user);
}
catch (DuplicateKeyException e) {
log.error("重复创建用户");
throw new ServiceException("用户名已存在", ErrorCode.SYSTEM_ERROR);
}
return user.getId();
}
@Override @Override
public UserAuthRespDTO findAuthInfoByUserAccount(String userAccount) { public UserAuthRespDTO findAuthInfoByUserAccount(String userAccount) {
User one = this.lambdaQuery().eq(User::getUserAccount, userAccount).one(); User one = this.lambdaQuery().eq(User::getUserAccount, userAccount).one();
UserAuthRespDTO userAuthDTO = new UserAuthRespDTO(); UserAuthRespDTO userAuthDTO = new UserAuthRespDTO();
if (one != null) { if (one != null) {
BeanUtils.copyProperties(one, userAuthDTO); BeanUtils.copyProperties(one, userAuthDTO);
return userAuthDTO; return userAuthDTO;
} }
return null; return null;
} }
@Override @Override
public UserAuthRespDTO findAuthInfoByUserId(String userId) { public UserAuthRespDTO findAuthInfoByUserId(String userId) {
User one = this.lambdaQuery().eq(User::getId, userId).one(); User one = this.lambdaQuery().eq(User::getId, userId).one();
UserAuthRespDTO userAuthDTO = new UserAuthRespDTO(); UserAuthRespDTO userAuthDTO = new UserAuthRespDTO();
if (one != null) { if (one != null) {
BeanUtils.copyProperties(one, userAuthDTO); BeanUtils.copyProperties(one, userAuthDTO);
return userAuthDTO; return userAuthDTO;
} }
return null; return null;
}
}
} }

20
pom.xml
View File

@@ -31,6 +31,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>3.5.7</spring-boot.version> <spring-boot.version>3.5.7</spring-boot.version>
<spring-cloud-alibaba.version>2025.0.0.0</spring-cloud-alibaba.version> <spring-cloud-alibaba.version>2025.0.0.0</spring-cloud-alibaba.version>
<spring.checkstyle.plugin>0.0.47</spring.checkstyle.plugin>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
@@ -84,6 +85,10 @@
<resources> <resources>
<resource> <resource>
<directory>src/main/resources</directory> <directory>src/main/resources</directory>
<includes>
<include>application*.yml</include>
<include>application*.properties</include>
</includes>
<filtering>true</filtering> <filtering>true</filtering>
</resource> </resource>
</resources> </resources>
@@ -103,6 +108,21 @@
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version> <version>${spring-boot.version}</version>
</plugin> </plugin>
<!--
代码格式插件默认使用spring 规则,可运行命令进行项目格式化:./mvnw spring-javaformat:apply 或 mvn spring-javaformat:apply可在IDEA中安装插件以下插件进行自动格式化
https://repo1.maven.org/maven2/io/spring/javaformat/spring-javaformat-intellij-idea-plugin
-->
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>${spring.checkstyle.plugin}</version>
<executions>
<execution>
<phase>validate</phase>
<inherited>true</inherited>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
<profiles> <profiles>