java 在 Spring Boot 1.4.* 中不推荐使用 FilterRegistrationBean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39911132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:44:50  来源:igfitidea点击:

FilterRegistrationBean is deprecated in Spring Boot 1.4.*

javaspringspring-mvcfilter

提问by pik4

I upgraded Spring Boot dependency in my project and I realized that some classes, such as FilterRegistrationBean, are deprecated.

我在我的项目中升级了 Spring Boot 依赖项,我意识到某些类,例如 FilterRegistrationBean,已被弃用。

Do you know how can implement a Filter in Spring Boot 1.4.1?

您知道如何在 Spring Boot 1.4.1 中实现过滤器吗?

Bean of Filter

过滤豆

@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;
}

My Filter code:

我的过滤器代码:

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() {
}
}

回答by Rubasace

Check the javadoc. It was just moved to the package org.springframework.boot.web.servlet

检查javadoc。它刚刚被移到包 org.springframework.boot.web.servlet