0

我有一个使用 keycloack 保护的 Spring Boot 应用程序 (mvc)。(与 spring-boot-starter-security 和 keycloak-spring-boot-starter 一起使用)

我像这样配置了 KeycloakWebSecurityConfigurerAdapter;

    @Override
    protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
        return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
    }

    @Override
    protected KeycloakAuthenticationProvider keycloakAuthenticationProvider() {
        return this.tybsKimlikSaglayici;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);

        http.cors().and().authorizeRequests().antMatchers("/", 
                "/home").permitAll().antMatchers("/admin").permitAll()
                .anyRequest().authenticated().and()
                .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/sso/logout")).permitAll();

        http.exceptionHandling().accessDeniedPage("accessDeniedPage");
    }

  @Bean 
        public CorsConfigurationSource corsConfigurationSource() {
            final CorsConfiguration configuration = new CorsConfiguration();
            configuration.setAllowedOrigins(ImmutableList.of("*"));
            configuration.setAllowedMethods(ImmutableList.of("HEAD",
                    "GET", "POST", "PUT", "DELETE", "PATCH"));

            configuration.setAllowCredentials(true);

            configuration.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));
            final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", configuration);
            return source;
        }

请求控制器方法,响应 html 视图工作正常(keyclock 验证请求)

但是,对控制器方法的表单操作对休息控制器方法的 Ajax 请求不起作用(发布、放置、删除..请求)

我将 @CrossOrigin(origins = "*") 添加到我的控制器中。

这是我的ajax请求,

$.ajax({
    type : "method_here",
    contentType : "application/json; charset=utf-8;",
    url : "url_here",
    data : JSON.stringify(data),
    timeout : 30000, 
    success : function(response) {

    },
    error : function(error) {

    }
});

这是密钥锁客户端

在此处输入图像描述

这是kecloack json(我试过application.properties文件)

{
  "realm": "demo-realm",
  "auth-server-url": "url_of_sso_app",
  "ssl-required": "external",
  "resource": "kie-remote",
  "principal-attribute": "preferred_username",
  #"enable-cors": true,     **tryed to add**
  #"cors-max-age" : 10000,   
  #"cors-allowed-methods": "GET, POST, PUT, HEAD, OPTIONS", 
  #"cors-allowed-headers": "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headersl", 
  "credentials": {
     "secret": "secret_of_realm_client"
  }
}

我该如何解决这个问题。如何使用 keycloack 验证 ajax 请求帮助。

4

1 回答 1

0

我发现我失踪了。

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring()
                  .antMatchers(HttpMethod.POST, "/**")
                  .antMatchers(HttpMethod.PUT, "/**")
                  .antMatchers(HttpMethod.DELETE, "/**");
}

此方法必须在 web 安全配置中被覆盖。它必须像 .antMatchers(HttpMethod.POST, "/allowed_method_path")

编辑:

此代码忽略这些 http 方法类型的身份验证过程。正确的解决方案不使用 web.ignoring() 方法。问题与 csrf 相关,(csrf 的默认 spring 安全设置为启用)spring 阻止 put、delete、post http 方法以保护服务器免受 csrf 攻击。如果服务没有在浏览器上消费,可以禁用 csrf,但服务正在生成浏览器页面,解决方案是配置 csrf。请检查使用 spring security 时如何在速度宏中获取 csrf 令牌

谢谢

于 2017-11-06T07:05:11.530 回答