解决Springboot @WebFilter拦截器未生效问题
问题描述
@WebFilter(filterName=“ssoFilter”,urlPatterns=“/*”)
未生效拦截器
解决方法
在springboot启动类上添加
@ServletComponentScan(basePackages=“full.package.path”)
路径替换为@WebFilter所在包
补充知识:在springboot中使用@WebFilter配置filter(包括排除URL)
我就废话不多说了,大家还是直接看代码吧~
@WebFilter(urlPatterns="/*")
@Order(value=1)
publicclassTestFilterimplementsFilter{
privatestaticfinalSetALLOWED_PATHS=Collections.unmodifiableSet(newHashSet<>(
Arrays.asList("/main/excludefilter","/login","/logout","/register")));
@Override
publicvoidinit(FilterConfigfilterConfig)throwsServletException{
System.out.println("init-----------filter");
}
@Override
publicvoiddoFilter(ServletRequestreq,ServletResponseres,FilterChainchain)throwsIOException,ServletException{
HttpServletRequestrequest=(HttpServletRequest)req;
HttpServletResponseresponse=(HttpServletResponse)res;
Stringpath=request.getRequestURI().substring(request.getContextPath().length()).replaceAll("[/]+$","");
booleanallowedPath=ALLOWED_PATHS.contains(path);
if(allowedPath){
System.out.println("这里是不需要处理的url进入的方法");
chain.doFilter(req,res);
}
else{
System.out.println("这里是需要处理的url进入的方法");
}
}
@Override
publicvoiddestroy(){
System.out.println("destroy----------filter");
}
}
@Order中的value越小,优先级越高。
以上这篇解决Springboot@WebFilter拦截器未生效问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短