小编Jac*_*ing的帖子

如何更改 Spring Boot 中允许的标头

我目前正在使用 Auth0(和 Angular 2 GUI),它将"x-xsrf-token"请求中类型的标头发送到 Spring Boot API。

我收到错误:

“XMLHttpRequest 无法加载http://localhost:3001/ping。预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 x-xsrf-token。”

这很公平,因为响应标头中的访问控制响应标头列表不包括x-xsrf-token(在 Chrome 的网络选项卡中调试请求时)。

我尝试了多种解决方案,我认为最接近的解决方案是覆盖 中的配置方法AppConfig,并添加我自己的方法CorsFilter,如下所示:

(Imports removed for brevity)

@Configuration
@EnableWebSecurity(debug = true)
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class AppConfig extends Auth0SecurityConfig {

    @Bean
    public Auth0Client auth0Client() {
        return new Auth0Client(clientId, issuer);
    }

    @Bean
    public Filter corsFilter() {
        UrlBasedCorsConfigurationSource source = new     UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("Content-Type");
        config.addAllowedHeader("x-xsrf-token");
        config.addAllowedHeader("Authorization");
        config.addAllowedHeader("Access-Control-Allow-Headers");
        config.addAllowedHeader("Origin");
        config.addAllowedHeader("Accept"); …
Run Code Online (Sandbox Code Playgroud)

java spring auth0

6
推荐指数
1
解决办法
4万
查看次数

标签 统计

auth0 ×1

java ×1

spring ×1