在spring boot中使用@WebFilter配置filter(包括排除URL)
2017-07-03 19:40
3967 查看
@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 = request.getRequestURI().substring(request.getContextPath().length()).replaceAll("[/]+$", ""); boolean allowedPath = ALLOWED_PATHS.contains(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越小,优先级越高。
ALLOWED_PATHS
这个是一个集合,存放的是需要排出的URL,用来判断是否是需要排除的URL。
关于为什么SpringBoot中使用了@WebFilter但是过滤器却没有生效:一定要加上@Configuration注解,@Service其实也可以,其他类似。
相关文章推荐
- Spring Boot实战之Filter实现使用JWT进行接口认证 jwt(json web token) 用户发送按照约定,向服务端发送 Header、Payload 和 Signature,
- Spring Boot中使用@WebFilter的Filter不起作用解决方案
- Spring Boot 之FilterRegistrationBean --支持web Filter 排序的使用
- web.xml 文件中一般包括 servlet, spring, filter, listenr的配置。那么他们是按照一个什么顺序加载呢?
- Spring Boot 之FilterRegistrationBean --支持web Filter 排序的使用
- Spring Boot 之web Filter --不支持排序的使用
- spring boot web filter的使用
- SpringBoot入坑笔记之spring-boot-starter-web 配置文件的使用
- 使用springboot配置webscoket
- spring boot开发遇到坑之spring-boot-starter-web配置文件使用教程
- springboot使用注解配置filter以及对象转换
- Spring Boot 之web Filter --支持排序的使用扩展
- 使用spring的DelegatingFilterProxy代理配置web.xml的自定义filter
- springboot filter使用配置
- 关于使用spring需要在web.xml配置的内容的解析
- 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 <configuration>\<system.web>\<httpModules> 节中包括..
- 备忘:在web.xml中使用org.springframework.js.resource.ResourceServlet的配置对静态资源的访问
- 关于使用Spring和hibernate开发web程序的配置说明和简单实例的详细说明
- web.xml文件中配置( listenr, filter,servlet, spring)的加载顺序 研究(转)
- web.xml中配置Spring的OpenSessionInViewFilter的作用