在 Spring Boot 1.4 中不推荐使用 FilterRegistrationBean。*
Posted
技术标签:
【中文标题】在 Spring Boot 1.4 中不推荐使用 FilterRegistrationBean。*【英文标题】:FilterRegistrationBean is deprecated in Spring Boot 1.4.* 【发布时间】:2017-02-16 02:13:01 【问题描述】:我在我的项目中升级了 Spring Boot 依赖项,我意识到某些类,例如 FilterRegistrationBean,已被弃用。
你知道如何在 Spring Boot 1.4.1 中实现过滤器吗?
过滤豆
@Bean
public FilterRegistrationBean filterRegistrationBean()
AuthenticationFilter f = new AuthenticationFilter();
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(f);
registrationBean.addInitParameter("defaultToken", defaultToken);
registrationBean.addInitParameter("secretKey", secretKey);
ArrayList<String> match = new ArrayList<>();
match.add("/users/*");
registrationBean.setUrlPatterns(match);
return registrationBean;
我的过滤器代码:
public class AuthenticationFilter implements Filter
private String defaultToken;
private String secretKey;
private UserSessionTokenRepository userSessionTokenRepository;
private UserManager userManager;
@Override
public void init(FilterConfig filterConfig) throws ServletException
defaultToken = filterConfig.getInitParameter("defaultToken");
secretKey = filterConfig.getInitParameter("secretKey");
userSessionTokenRepository = WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig.getServletContext()).getBean(UserSessionTokenRepository.class);
userManager = WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig.getServletContext()).getBean(UserManager.class);
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
chain.doFilter(request, response);
public void addHeaders(HttpServletResponse response)
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Content-Type");
public void destroy()
【问题讨论】:
【参考方案1】:已移至另一个包:http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/servlet/FilterRegistrationBean.html
【讨论】:
【参考方案2】:检查the javadoc。它刚刚被移动到包 org.springframework.boot.web.servlet
【讨论】:
以上是关于在 Spring Boot 1.4 中不推荐使用 FilterRegistrationBean。*的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Spring Boot 1.4 中自定义 Jackson
在 Spring Boot 1.4 MVC 测试中使用 @WebMvcTest 设置 MockMvc
Spring Boot 1.4 自定义内部 Jackson 反序列化