后台管理功能实现
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.emotion.config;
|
||||
|
||||
import com.emotion.interceptor.AuthInterceptor;
|
||||
import com.emotion.interceptor.AdminAuthInterceptor;
|
||||
import com.emotion.interceptor.UserContextInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -21,6 +22,9 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
@Autowired
|
||||
private AuthInterceptor authInterceptor;
|
||||
|
||||
@Autowired
|
||||
private AdminAuthInterceptor adminAuthInterceptor;
|
||||
|
||||
@Autowired
|
||||
private UserContextInterceptor userContextInterceptor;
|
||||
|
||||
@@ -29,11 +33,17 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
*/
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
// 认证拦截器
|
||||
// 管理员认证拦截器 - 优先级最高
|
||||
registry.addInterceptor(adminAuthInterceptor)
|
||||
.addPathPatterns("/admin/**")
|
||||
.order(1);
|
||||
|
||||
// 用户认证拦截器
|
||||
registry.addInterceptor(authInterceptor)
|
||||
.addPathPatterns("/**")
|
||||
.excludePathPatterns(
|
||||
"/auth/**",
|
||||
"/admin/**", // 排除管理员接口,由AdminAuthInterceptor处理
|
||||
"/error",
|
||||
"/favicon.ico",
|
||||
"/actuator/**",
|
||||
@@ -45,7 +55,8 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
"/doc.html",
|
||||
"/static/**",
|
||||
"/public/**"
|
||||
);
|
||||
)
|
||||
.order(2);
|
||||
|
||||
// 用户上下文拦截器
|
||||
registry.addInterceptor(userContextInterceptor)
|
||||
@@ -62,7 +73,8 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
"/doc.html",
|
||||
"/static/**",
|
||||
"/public/**"
|
||||
);
|
||||
)
|
||||
.order(3);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -98,11 +98,13 @@ public class AdminAuthInterceptor implements HandlerInterceptor {
|
||||
private boolean isPublicAdminEndpoint(String requestURI) {
|
||||
String[] publicEndpoints = {
|
||||
"/admin/auth/login",
|
||||
"/admin/auth/refreshToken"
|
||||
"/admin/auth/refreshToken",
|
||||
"/api/admin/auth/login", // 包含context-path的路径
|
||||
"/api/admin/auth/refreshToken" // 包含context-path的路径
|
||||
};
|
||||
|
||||
for (String endpoint : publicEndpoints) {
|
||||
if (requestURI.equals(endpoint)) {
|
||||
if (requestURI.equals(endpoint) || requestURI.endsWith(endpoint)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,8 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
@Autowired
|
||||
private JwtUtil jwtUtil;
|
||||
|
||||
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||
@Autowired
|
||||
private org.springframework.security.crypto.password.PasswordEncoder passwordEncoder;
|
||||
|
||||
private static final String ADMIN_TOKEN_PREFIX = "admin_token:";
|
||||
private static final String ADMIN_REFRESH_TOKEN_PREFIX = "admin_refresh_token:";
|
||||
|
||||
@@ -92,13 +92,13 @@ public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return PageResult.<AdminResponse>builder()
|
||||
.records(responseList)
|
||||
.total(adminPage.getTotal())
|
||||
.current(adminPage.getCurrent())
|
||||
.size(adminPage.getSize())
|
||||
.pages(adminPage.getPages())
|
||||
.build();
|
||||
PageResult<AdminResponse> result = new PageResult<>();
|
||||
result.setRecords(responseList);
|
||||
result.setTotal(adminPage.getTotal());
|
||||
result.setCurrent(adminPage.getCurrent());
|
||||
result.setSize(adminPage.getSize());
|
||||
result.setPages(adminPage.getPages());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user