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