【问题标题】:CORS problems with spring boot security弹簧靴安全性的 CORS 问题
【发布时间】:2018-10-12 07:01:23
【问题描述】:

我使用 spring boot、spring security 并希望避免任何 cors 操作。我从这里 (Can you completely disable CORS support in Spring?) 尝试第二个答案,我需要向 cors 添加自定义过滤器,这里是过滤器:

@Component
public class CorsFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response,
                                    final FilterChain filterChain) throws ServletException, IOException {

        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH, HEAD");
        response.addHeader("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
        response.addHeader("Access-Control-Expose-Headers", "Access-Control-Allow-Origin, Access-Control-Allow-Credentials");
        response.addHeader("Access-Control-Allow-Credentials", "true");
        response.addIntHeader("Access-Control-Max-Age", 10);
        filterChain.doFilter(request, response);
    }
}

所以我创建了插入 CorsFilter 的类,但我得到了错误:

'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'corsFilter' is expected to be of type 'org.springframework.web.filter.CorsFilter' but was actually of type 'fa.backend.core.security.CorsFilter'

我的安全配置如下所示:

 @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().antMatcher("/users").authorizeRequests()
                .antMatchers(HttpMethod.POST, "/login").permitAll()
                .and()
                .addFilterBefore(
                        new JWTLoginFilter(tokenAuthentication, authenticationManagerBean()),
                        UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }

你能告诉我如何解决它吗?我的目标是禁用任何 cors 操作,因为我从前端收到:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8081' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

【问题讨论】:

    标签: spring-security cors


    【解决方案1】:

    在配置覆盖中删除 http 之后的 'cors().and' 部分。以下是sn-p。

    @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.antMatcher("/users").authorizeRequests()
                    .antMatchers(HttpMethod.POST, "/login").permitAll()
                    .and()
                    .addFilterBefore(
                            new JWTLoginFilter(tokenAuthentication, authenticationManagerBean()),
                            UsernamePasswordAuthenticationFilter.class)
                    .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
        }
    

    【讨论】:

      猜你喜欢
      • 2018-07-10
      • 2020-08-09
      • 2017-04-29
      • 1970-01-01
      • 2017-12-22
      • 2019-10-18
      • 2017-03-18
      • 2017-03-18
      相关资源
      最近更新 更多