博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring boot 自定义过滤器链
阅读量:5158 次
发布时间:2019-06-13

本文共 3520 字,大约阅读时间需要 11 分钟。

spring boot 会按照order值的大小,从大到小的顺序来依次过滤。

贴下代码:

package com.osp.ucenter;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.core.annotation.Order;import org.springframework.web.filter.DelegatingFilterProxy;import com.osp.ucenter.filter.CrossDomainFilter;import com.osp.ucenter.filter.Initfilter;import com.osp.ucenter.filter.SecurityFilter;@EnableAutoConfiguration@SpringBootApplicationpublic class UcenterMain {			public static void main(String[] args) {		SpringApplication.run(UcenterMain.class, args);	}		/**	 * 跨域处理Filter(最先执行此过滤器,避免跨域请求调用两次而执行后续的过滤器链)	 * 	 * @return	 */	@Bean    @Order(Integer.MAX_VALUE)	public FilterRegistrationBean crossDomainFilterRegistration() {		FilterRegistrationBean registration = new FilterRegistrationBean();		registration.setFilter(new CrossDomainFilter());		registration.addUrlPatterns("/*");		registration.setName("CrossDomainFilter");		return registration;	}	/**	 * 初始化	 * 	 * @return	 */	@Bean	@Order(Integer.MAX_VALUE-1)	public FilterRegistrationBean InitfilterRegistration() {		FilterRegistrationBean registration = new FilterRegistrationBean();		registration.setFilter(new Initfilter());		registration.addUrlPatterns("/no");		registration.setName("Initfilter");		return registration;	}	/**	 * 安全权限 DelegatingFilterProxy servlet容器与IOC容器关联	 * 	 * @return	 */	@Bean	@Order(Integer.MAX_VALUE-2)	public FilterRegistrationBean securityFilter() {		FilterRegistrationBean registration = new FilterRegistrationBean();		registration.setFilter(new DelegatingFilterProxy("SecurityFilter"));		registration.addInitParameter("targetFilterLifecycle", "true");		registration.addUrlPatterns("/*");		return registration;	}		@Bean(name = "SecurityFilter")	public SecurityFilter getSecurityFilter(){		return new SecurityFilter();	}	 }

  随便贴一个过滤器

package com.osp.ucenter.filter;import java.io.IOException;import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger;import org.springframework.web.filter.OncePerRequestFilter;/** * 处理跨域的filter *  */public class CrossDomainFilter extends OncePerRequestFilter {	Logger logger = Logger.getLogger(CrossDomainFilter.class);		@Override    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {		try {			// 跨域			String origin = request.getHeader("Origin");			if (origin == null) {				response.addHeader("Access-Control-Allow-Origin", "*");			} else {				response.addHeader("Access-Control-Allow-Origin", origin);			}			response.addHeader("Access-Control-Allow-Headers",					"Origin, x-requested-with, Content-Type, Accept,X-Cookie, token");			response.addHeader("Access-Control-Allow-Credentials", "true");			response.addHeader("Access-Control-Allow-Methods", "GET,POST,PUT,OPTIONS,DELETE");			if (request.getMethod().equals("OPTIONS")) {				response.setStatus(HttpServletResponse.SC_OK);				return;			}			filterChain.doFilter(request, response);		} catch (Exception e) {			logger.error("Exception in crossDomainFilter.doFilter", e);			throw e;		}	}	@Override	public void destroy() {	}}

  

转载于:https://www.cnblogs.com/zhangmingcheng/p/7563407.html

你可能感兴趣的文章
不同程序语言的注释和变量要求
查看>>
语言基础(9):static, extern 和 inline
查看>>
windows linux—unix 跨平台通信集成控制系统
查看>>
【编程练习】复习一下树的遍历
查看>>
邮件和短信验证码
查看>>
(转)Android studio 使用心得(五)—代码混淆和破解apk
查看>>
构建之法阅读笔记03
查看>>
ES5_03_Object扩展
查看>>
Apache-ab 接口性能测试
查看>>
EF 4.1 Code First Walkthrough
查看>>
常用MySQL语法
查看>>
007API网关服务Zuul
查看>>
bzoj 2600: [Ioi2011]ricehub
查看>>
iOS __strong __weak @Strongify @Weakify
查看>>
thinkphp引入PHPExcel类---thinkPHP类库扩展-----引入没有采用命名空间的类库
查看>>
创建数据库,表
查看>>
Luogu 1970 NOIP2013 花匠 (贪心)
查看>>
javascript笔记---貌似大叔
查看>>
去重查询表mysql 中数据
查看>>
工厂模式
查看>>