1.后端
@ServletComponentScan
在启动类中使用@ServletComponentScan注解,Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码。
@Slf4j
@SpringBootApplication
@ServletComponentScan
public class ReggieApplication {
public static void main(String[] args) {
SpringApplication.run(ReggieApplication.class,args);
log.info("项目启动成功");
}
}
定义LoginCheckFilter类
@WebFilter(filterName = "loginCheckFilter",urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {
//路径匹配器,支持通配符
private static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
//获取本次请求的URI
String requestURI = request.getRequestURI();
log.info("拦截到请求 :{}",requestURI);
//放行的URI
String[] urls = new String[]{
"/employee/login",
"/employee/logout",
"/backend/**",
"/front/**",
"/**"
};
//判断是否要处理
boolean check = check(urls, requestURI);
//不需要处理直接放行
if(check){
log.info("本次请求不需要处理:{}",requestURI);
filterChain.doFilter(request,response);
return ;
}
//判断登录状态 如果已登录 直接放行
if(request.getSession().getAttribute("employee") !=null){
log.info(" 用户已登录,用户id为:{}",request.getSession().getAttribute("employee"));
filterChain.doFilter(request,response);
return ;
}
//未登录则返回未登录结果,通过输出流方式向客户端页面相应数据
log.info("用户未登录");
response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
return ;
}
/**
* 路径匹配 检查本次请求是否需要放行
* @param urls
* @param requestURI
* @return
*/
public boolean check(String[] urls,String requestURI){
for(String url : urls){
boolean match = PATH_MATCHER.match(url, requestURI);
if(match){
return true;
}
}
return false;
}
}
路径匹配器
Spring Boot的路径匹配器`AntPathMatcher`是一个用于匹配URL路径的工具类,它可以用于处理URL路径的匹配和解析。
`AntPathMatcher`的功能和用法如下:
1. 路径匹配:`AntPathMatcher`可以用于匹配URL路径和模式,比如`/user/{id}`和`/user/*`等。它支持通配符`*`和`**`,可以用于匹配路径中的任意字符和任意子路径。
2. 路径解析:`AntPathMatcher`可以用于解析URL路径中的变量,比如`/user/{id}`中的`id`。它可以将URL路径中的变量提取出来,并进行处理。
3. 匹配规则:`AntPathMatcher`支持多种匹配规则,比如大小写敏感、是否允许尾部斜杠等。
在Spring Boot中,`AntPathMatcher`通常用于处理URL路径的匹配和解析,比如在`@RequestMapping`注解中,可以使用`AntPathMatcher`来匹配请求的URL路径。
示例代码如下:
private static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
public void handleRequest(String url) {
if (PATH_MATCHER.match("/user/*", url)) {
// 匹配处理逻辑
}
}
在上面的示例中,`AntPathMatcher`被用于匹配URL路径`/user/*`和`url`是否匹配,如果匹配成功,则执行相应的处理逻辑。
`AntPathMatcher`是Spring Boot中用于处理URL路径匹配和解析的工具类,可以用于处理RESTful风格的URL路径。
2.前端
// 响应拦截器
service.interceptors.response.use(res => {
if (res.data.code === 0 && res.data.msg === 'NOTLOGIN') {// 返回登录页面
console.log('---/backend/page/login/login.html---')
localStorage.removeItem('userInfo')
window.top.location.href = '/backend/page/login/login.html'
} else {
return res.data
}
},
error => {
console.log('err' + error)
let { message } = error;
if (message == "Network Error") {
message = "后端接口连接异常";
}
else if (message.includes("timeout")) {
message = "系统接口请求超时";
}
else if (message.includes("Request failed with status code")) {
message = "系统接口" + message.substr(message.length - 3) + "异常";
}
window.ELEMENT.Message({
message: message,
type: 'error',
duration: 5 * 1000
})
return Promise.reject(error)
}
)
win.$axios = service
})(window);