首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在飞行前响应中,访问控制-允许标头不允许请求头字段x-xsrf-token。

在飞行前响应中,访问控制-允许标头不允许请求头字段x-xsrf-token。
EN

Stack Overflow用户
提问于 2020-06-22 20:24:09
回答 2查看 1.6K关注 0票数 0

SprinBoot键盘遮挡器会被浏览器屏蔽,

在飞行前响应中,访问控制-允许标头不允许请求头字段x-xsrf-token。

从源'http://localhost:8080/auth/realms/test/protocol/openid-connect/token‘获取'http://localhost:8081’的访问已被CORS策略所阻止:请求标头字段x-xsrf-令牌不允许在飞行前响应中被访问控制-允许-标头。

代码语言:javascript
复制
This cors configs were added to spring boot app,
      cors: true
      cors-allowed-methods: GET,POST,HEAD,PUT,DELETE,OPTIONS
      cors-allowed-headers: x-xsrf-token

同时,客户端url http://localhost:8081也被添加到keeycloak中的Web中。不知道还缺什么让它起作用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-29 16:35:27

您可以使用CorsConfiguration设置允许的标头。

代码语言:javascript
复制
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().configurationSource(corsConfigurationSource());
    }

    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        List<String> allowOrigins = Arrays.asList("*");
        configuration.setAllowedOrigins(allowOrigins);
        configuration.setAllowedMethods(Collections.singletonList("*"));
        configuration.setAllowedHeaders(Collections.singletonList("*"));
        configuration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}
票数 0
EN

Stack Overflow用户

发布于 2020-06-30 20:05:15

您是否尝试过在控制器类和存储库类上使用@跨界源(origins=“http://localhost:8081")?

还有:尝试在您的主WebConfigurer应用程序类中添加SpringBoot Bean,并将其与@跨界起源(origins=“http://localhost:8081")连接起来。”

代码语言:javascript
复制
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                System.out.println("here");
                registry.addMapping("/**").allowedOrigins("http://localhost:8081").allowedMethods("PUT", "DELETE" )
                .allowedHeaders("header1", "header2", "header3")
                .exposedHeaders("header1", "header2")
                .allowCredentials(false).maxAge(3600);;
            }
        };
    }

请访问此链接以获取在应用服务器端启用CORS。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62522909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档