Spring Security Access-Control-Allow-Origin: * (CORS) проблема с недопустимым токеном JWT - PullRequest
0 голосов
/ 11 декабря 2018

У меня настроена защита JWT с помощью Spring Security в приложении Spring Boot.У меня проблема с заголовком

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.Это описано в https://docs.spring.io/spring-security/site/docs/current/reference/html5/#cors и обсуждено в Как настроить CORS в приложении Spring Boot + Spring Security? и 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 голосов
/ 28 декабря 2018

Попробуйте использовать CrosFilter.Найдите приведенный ниже образец.

@Bean
public CorsFilter corsFilter() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowCredentials(true);
    configuration.addAllowedOrigin("*");
    configuration.addAllowedHeader("*");
    configuration.addAllowedMethod("OPTIONS");
    configuration.addAllowedMethod("HEAD");
    configuration.addAllowedMethod("GET");
    configuration.addAllowedMethod("PUT");
    configuration.addAllowedMethod("POST");
    configuration.addAllowedMethod("DELETE");
    configuration.addAllowedMethod("PATCH");
    source.registerCorsConfiguration("/**", configuration);
    return new CorsFilter(source);
}
0 голосов
/ 20 декабря 2018

с аналогичной проблемой, не может заставить его работать с 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;
}
...