解决Springboot@WebFilter未⽣效问题
问题描述
@WebFilter(filterName = “ssoFilter”,urlPatterns = “/*”)
未⽣效
解决⽅法
在springboot启动类上添加
@ServletComponentScan(basePackages = “full.package.path”)
路径替换为@WebFilter所在包springboot和过滤器
补充知识:在spring boot中使⽤@WebFilter配置filter(包括排除URL)
我就废话不多说了,⼤家还是直接看代码吧~
@WebFilter(urlPatterns = "/*")
@Order(value = 1)
public class TestFilter implements Filter {
private static final Set<String> ALLOWED_PATHS = Collections.unmodifiableSet(new HashSet<>(
Arrays.asList("/main/excludefilter", "/login", "/logout", "/register")));
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init-----------filter");
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
String path = RequestURI().ContextPath().length()).replaceAll("[/]+$", "");
boolean allowedPath = ains(path);
if (allowedPath) {
System.out.println("这⾥是不需要处理的url进⼊的⽅法");
chain.doFilter(req, res);
}
else {
System.out.println("这⾥是需要处理的url进⼊的⽅法");
}
}
@Override
public void destroy() {
System.out.println("destroy----------filter");
}
}
@Order中的value越⼩,优先级越⾼。
以上这篇解决Springboot @WebFilter未⽣效问题就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论