mirror of
https://github.com/suyiiyii/SIMS.git
synced 2025-06-03 12:56:10 +08:00
refactor(common): 整理拦截器配置和JWT拦截器实现
This commit is contained in:
parent
5b9e835108
commit
7724eb43c3
@ -3,13 +3,11 @@ package top.suyiiyii.sims.common;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
||||
import top.suyiiyii.sims.service.RoleService;
|
||||
import top.suyiiyii.sims.service.UserService;
|
||||
import top.suyiiyii.sims.utils.JwtUtils;
|
||||
|
||||
/**
|
||||
@ -20,32 +18,29 @@ import top.suyiiyii.sims.utils.JwtUtils;
|
||||
* @Description: TODO 拦截器配置
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Configuration
|
||||
public class InterceptorConfig extends WebMvcConfigurationSupport {
|
||||
@Configuration
|
||||
public class InterceptorConfig extends WebMvcConfigurationSupport {
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
private RoleService roleService;
|
||||
|
||||
|
||||
@Autowired
|
||||
private JwtInterceptor jwtInterceptor;
|
||||
|
||||
//UserService userService;
|
||||
@Override
|
||||
protected void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(jwtInterceptor())
|
||||
.addPathPatterns("/**")
|
||||
.excludePathPatterns("/user/login") // 排除不需要验证的路径
|
||||
.excludePathPatterns("/user/register")
|
||||
.excludePathPatterns("/v3/api-docs/**");
|
||||
@Override
|
||||
protected void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(jwtInterceptor)
|
||||
.addPathPatterns("/**")
|
||||
.excludePathPatterns("/user/login") // 排除不需要验证的路径
|
||||
.excludePathPatterns("/user/register")
|
||||
.excludePathPatterns("/v3/api-docs/**");
|
||||
|
||||
// 注册AdminInterceptor,只拦截以admin/开头的路径
|
||||
registry.addInterceptor(new AdminInterceptor())
|
||||
.addPathPatterns("/admin/**");
|
||||
super.addInterceptors(registry);
|
||||
}
|
||||
// 注册AdminInterceptor,只拦截以admin/开头的路径
|
||||
registry.addInterceptor(new AdminInterceptor())
|
||||
.addPathPatterns("/admin/**");
|
||||
super.addInterceptors(registry);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JwtInterceptor jwtInterceptor() {
|
||||
return new JwtInterceptor();
|
||||
}
|
||||
|
||||
// AdminInterceptor的实现
|
||||
public class AdminInterceptor implements HandlerInterceptor {
|
||||
@ -78,5 +73,5 @@ import top.suyiiyii.sims.utils.JwtUtils;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
package top.suyiiyii.sims.common;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import top.suyiiyii.sims.entity.User;
|
||||
import top.suyiiyii.sims.exception.ServiceException;
|
||||
import top.suyiiyii.sims.mapper.UserMapper;
|
||||
import top.suyiiyii.sims.mapper.MpUserMapper;
|
||||
import top.suyiiyii.sims.utils.JwtUtils;
|
||||
|
||||
/**
|
||||
@ -20,10 +21,11 @@ import top.suyiiyii.sims.utils.JwtUtils;
|
||||
* @Version 1.0
|
||||
*/
|
||||
|
||||
@Component
|
||||
public class JwtInterceptor implements HandlerInterceptor {
|
||||
|
||||
@Resource
|
||||
UserMapper userMapper;
|
||||
@Autowired
|
||||
MpUserMapper userMapper;
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
// 从 Authorization 头中获取 token
|
||||
|
Loading…
x
Reference in New Issue
Block a user