Spring安全性Access-Control-Allow-Origin:*(CORS)关于无效的JWT令牌的问题

时间:2018-12-11 15:31:44

标签: java spring-boot spring-security jwt microservices

我在Spring Boot应用程序中使用Spring Security配置了JWT安全性。我对

有问题
Access-Control-Allow-Origin: *

标头,也称为CORS。我配置了应用程序,因此在每个服务器响应中都存在标头,但是一旦JWT令牌无效,服务器响应就会出现403错误代码而没有Access-Control-Allow-Origin:*标头。这会导致浏览器将错误消息写入控制台:

  

无法加载http:// ...没有“ Access-Control-Allow-Origin”标头   存在于请求的资源上。因此,来源“ http:// ...”   不允许访问。响应的HTTP状态码为403。

这似乎是错误的,并且我希望获得Access-Control-Allow-Origin:*标头作为响应,即使JWT令牌无效且服务器响应带有403错误代码。

现在我尝试了什么和我的代码。

依赖项:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
...

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

据我了解,此问题可能是由过滤器链中的过滤器顺序引起的,我尝试将JWT JwtAuthenticationFilter放在CorsFilter或CsrfFilter之后,创建CorsConfigurationSource bean。这在https://docs.spring.io/spring-security/site/docs/current/reference/html5/#cors进行了描述,并在How to configure CORS in a Spring Boot + Spring Security application?https://github.com/spring-projects/spring-boot/issues/5834进行了讨论,但似乎没有帮助

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Value("${com.faircloud.common.security.header}")
    private String header;
    @Value("${com.faircloud.common.security.prefix}")
    private String prefix;
    @Value("${com.faircloud.common.security.validateLink}")
    private String validateLink;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().authorizeRequests()
                .antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources/**", "/configuration/**",
                        "/swagger-ui.html", "/webjars/**")
                .permitAll()
                .and().authorizeRequests().anyRequest().authenticated().and()
                .addFilterAfter(new JwtAuthenticationFilter(header, prefix, validateLink),
                        CsrfFilter.class)
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        final CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(ImmutableList.of("*"));
        configuration.setAllowedMethods(ImmutableList.of("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH"));
        // setAllowCredentials(true) is important, otherwise:
        // The value of the 'Access-Control-Allow-Origin' header in the response must
        // not be the wildcard '*' when the request's credentials mode is 'include'.
        configuration.setAllowCredentials(true);
        // setAllowedHeaders is important! Without it, OPTIONS preflight request
        // will fail with 403 Invalid CORS request
        configuration.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}

这里是JwtAuthenticationFilter类。请注意,要验证令牌,它会通过http调用其他微服务。另外,我的应用程序没有登录端点,因为登录是在其他微服务应用程序上实现的。

public class JwtAuthenticationFilter extends BasicAuthenticationFilter {

    private String header;
    private String prefix;
    private String validateLink;

    public JwtAuthenticationFilter(String header, String prefix, String validateLink) {
        super(new AuthenticationManager() {
            public Authentication authenticate(Authentication authentication) throws AuthenticationException{
                return null;
            }
        });
        this.header = header;
        this.prefix = prefix;
        this.validateLink = validateLink;
    } 

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

        // 1. get the authentication header. Tokens are supposed to be passed in the
        // authentication header
        String headerValue = request.getHeader(header);

        // 2. validate the header and check the prefix
        if (headerValue == null || !headerValue.startsWith(prefix)) {
            chain.doFilter(request, response); // If not valid, go to the next filter.
            return;
        }
        // 3. Get the token     
        String token = headerValue.replace(prefix, ""); 

        try {

            GatewayResponse gatewayResponse = validate(token);

            String userId = gatewayResponse.getUserId();

            /*
            Roles could come from gateway or loaded from current
            microservice database by user id. They are
            hardcoded here to illustrate how to populate
            SecurityContextHolder
            */
            List<String> authorities = new LinkedList<String>();
            authorities.add("USER");
            authorities.add("ADMIN");

            UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(userId, null,
                    authorities.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()));
            SecurityContextHolder.getContext().setAuthentication(auth);
            addTokenToResponse(gatewayResponse.getAuthHeader(), response);
        } catch (Exception e) {
            // In case of failure. Make sure it's clear; so guarantee user won't be
            // authenticated
            SecurityContextHolder.clearContext();
        }

        // go to the next filter in the filter chain
        chain.doFilter(request, response);
    }

    private void addTokenToResponse(String authHeaderValue, HttpServletResponse response) {
        response.addHeader(header, prefix+authHeaderValue);
    }

    private GatewayResponse validate(String token) {
        /HTTP call here, returns null if invalid token
        ...
    }
}

2 个答案:

答案 0 :(得分:0)

有类似的问题,无法使其与CorsConfigurationSource一起使用。仅基于过滤器的CORS支持提供了帮助:

@Bean
public FilterRegistrationBean filterRegistrationBean() {
    final CorsConfiguration config = new CorsConfiguration();

    config.setAllowCredentials(true);
    config.addAllowedOrigin("http://localhost:4200");
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");

    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", config);

    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(Ordered.HIGHEST_PRECEDENCE);

    return bean;
}

答案 1 :(得分:0)

尝试使用CrosFilter。找到以下示例。

<ul class="nav navbar-nav" id="nav">
    <li ><a >Архивная отчетность</a></li>
    <li ><a >Мониторинг</a></li>
</ul>