简体   繁体   English

Keycloak注销出现cors错误如何解决?

[英]Keycloak logout give cors error coming how to resolve it?

I am building an spring cloud gateway and trying to logout keycloak but it is giving me cors errors, my code it as below:我正在构建一个 spring 云网关并尝试注销 keycloak 但它给了我 cors 错误,我的代码如下:

Security class in which I defined logout code logic: Security class 其中我定义了注销代码逻辑:

@Bean
public ServerSecurityContextRepository securityContextRepository() {
    WebSessionServerSecurityContextRepository securityContextRepository =
            new WebSessionServerSecurityContextRepository();

    securityContextRepository.setSpringSecurityContextAttrName("langdope-security-context");

    return securityContextRepository;
}

private LogoutWebFilter logoutWebFilter() {
    LogoutWebFilter logoutWebFilter = new LogoutWebFilter();

    SecurityContextServerLogoutHandler logoutHandler = new SecurityContextServerLogoutHandler();
    logoutHandler.setSecurityContextRepository(securityContextRepository());

    RedirectServerLogoutSuccessHandler logoutSuccessHandler = new RedirectServerLogoutSuccessHandler();
    logoutSuccessHandler.setLogoutSuccessUrl(URI.create("http://localhost:9000/app/Default"));

    logoutWebFilter.setLogoutHandler(logoutHandler());
    logoutWebFilter.setLogoutSuccessHandler(logoutSuccessHandler);
    logoutWebFilter.setRequiresLogoutMatcher(
            ServerWebExchangeMatchers.pathMatchers(HttpMethod.GET, "/app/logout")
    );

    return logoutWebFilter;
}

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http,ReactiveClientRegistrationRepository repository) {
    // Authenticate through configured OpenID Provider
    http.addFilterAfter(new CustomWebFilter(), SecurityWebFiltersOrder.LAST).authorizeExchange()
            .pathMatchers("/app/logout").permitAll()
            .pathMatchers("/app/authenticate").authenticated()
            .pathMatchers("/app/**").authenticated().and().
            logout().disable()
            .securityContextRepository(securityContextRepository())
            .addFilterAt(logoutWebFilter(), SecurityWebFiltersOrder.LOGOUT)
            .oauth2Login(Customizer.withDefaults());

    // Also logout at the OpenID Connect provider
    http.httpBasic().disable();
    // Require authentication for all requests
   // http.authorizeExchange().anyExchange().authenticated();

    // Allow showing /home within a frame
    http.headers().frameOptions().mode(XFrameOptionsServerHttpHeadersWriter.Mode.SAMEORIGIN);

    // Disable CSRF in the gateway to prevent conflicts with proxied service CSRF
    http.csrf().disable();

    return http.build();
}

Now when from front-end I hit logout it gives me below error:现在,当我从前端点击注销时,它给了我以下错误:

Access to XMLHttpRequest at 'http://localhost:8280/auth/realms/Default/protocol/openid-connect/auth?response_type=code&client_id=Default&scope=openid%20email%20profile&state=qVQ46iGilTo9o2Ro7CdZzl9kmsMm23jnEqckybucgII%3D&redirect_uri=http://localhost:9000/login/oauth2/code/keycloak&nonce=Z6hMnfYEJaOpuJnX44obCe6GyW8Oc6FSn3MOU_2bRg4' (redirected from 'http://localhost:9000/app/logout') from origin 'http://localhost:9000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

In Keycloak for valid URL I have given * to test but still not working please help what am I missing?在 Keycloak 中有效的 URL 我已经给出 * 进行测试但仍然无法正常工作请帮助我错过了什么?

I've spent my share of time figuring out keycloak CORS errors and this is what I figured out.我花了很多时间来找出 keycloak CORS 错误,这就是我想出来的。

If you have web origins correctly configured ( https://stackoverflow.com/a/59072362/20992932 ) and you are still getting CORS errors there is high probability that the request you are sending is incorrect (jboss handles error before checking client web origin).如果您正确配置了 web 来源 ( https://stackoverflow.com/a/59072362/20992932 ) 并且您仍然收到 CORS 错误,则很有可能您发送的请求不正确(jboss 在检查客户端 web 来源之前处理错误).

To find out if that is the case for you the easiest solution would be to disable same origin policy on the browser (of course for the time of the testing only).要确定您是否属于这种情况,最简单的解决方案是在浏览器上禁用同源策略(当然仅在测试时)。 Then in.network console you should see the actual error response.然后在.network 控制台中,您应该会看到实际的错误响应。

Here is how to do it in chrome based browsers:以下是如何在基于 chrome 的浏览器中执行此操作:

chromium-browser --disable-web-security --user-data-dir="[some directory here]"

For more see: https://stackoverflow.com/a/59072362/20992932有关更多信息,请参阅: https://stackoverflow.com/a/59072362/20992932

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM