Springboot cross-domain Filter

page visit with the following 401 error

background breakpoint, the Filter class cannot be entered, and the code is not executed

here is my Filter class and inject

with @ Component annotation

ask for answers

@Component
public class CORSFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        System.out.println("Filtering on...........................................................");
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Authorization, Origin, Accept, Access-Control-Request-Method, Access-Control-Request-Headers");

        chain.doFilter(req, res);
    }

    public void init(FilterConfig filterConfig) {}

    public void destroy() {}

}

Mar.15,2022

    @Bean
    public FilterRegistrationBean httpContextFilter() {
        System.out.println("===== filter =====");
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new AbcFilter());
        registration.addUrlPatterns("/*");
        registration.setName("abc");
        registration.setOrder(1);
        return registration;
    }

Lei Feng


clipboard.png
CrossOrigin Notes to understand, solve cross-domain problems, do not need to do so troublesome

Menu