मैंने अपनी परियोजना में स्प्रिंग बूट निर्भरता को अपग्रेड किया और मुझे एहसास हुआ कि कुछ कक्षाएं, जैसे फ़िल्टररिएस्ट्रेशन बीन, को बहिष्कृत किया गया है।फ़िल्टर पंजीकरण डिग्री वसंत बूट 1.4 में बहिष्कृत है। *
क्या आप जानते हैं कि स्प्रिंग बूट 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("https://stackoverflow.com/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() {
}
}
Yup https://github.com/spring-projects/spring-boot/issues/6518 –