2017-11-05 5 views
0

私はkeycloackで固定しているスプリングブートアプリケーション(mvc)を持っています。 (spring-boot-starter-securityとkeycloak-spring-boot-startterを使用して)KeycloackでJquery ajaxリクエストを認証する方法、

私はそのような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ビューが正常に動作方法をコントローラに要求(keycloack認証を行うリクエスト)

しかし、コントローラのメソッドを休ませる方法 Ajaxリクエストをコントローラに フォームアクションは削除、(ポスト作業、置かれていません..要求)

@CrossOrigin(originins = "*")を私のコントローラに追加しました。ここで

は私のAjaxのreqeust、

$.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) { 

    } 
}); 

はここkeycloackクライアント

enter image description here

が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リクエストのヘルプをどのように認証できますか?

+0

を確認してください。 AJAXリクエストの結果はどうですか? – Boomer

+0

eclipseコンソールでは、403メッセージが表示されていませんでしたが、ajaxエラー・レスポンスに404が見つかりません。あなたの注意に感謝し、私は問題を解決しました。私は答えに加えた – Batuhan

答えて

0

私は見つからない。

この方法は、Webセキュリティ設定では無効にする必要があります。

このコードは、これらのHTTPメソッドの種類の認証プロセスを無視:そして、それは.antMatchers(HttpMethod.POST、 "/ allowed_method_path")のような変更

を編集する必要があります。正しい解決策はweb.ignoring()メソッドを使用しません。問題はcsrfと関連しています(csrfのデフォルトの春のセキュリティ設定は有効です)。springは、サーバーをcsrf攻撃から保護するためのputメソッド、deleteメソッド、postメソッドを防ぎます。サービスがブラウザで使用されない場合、csrfは無効にすることができますが、サービスはブラウザページを生成しています。解決策はcsrfを設定することです。あなたが働いていないかについて、より詳細なことしてくださいできHow to obtain csrf token in a velocity macro when using spring security

おかげ

関連する問題