2017-08-08 22 views
0

私はサービスを保護するためにkeycloakを使用しています。クライアントアプリケーションはkeycloakサーバーからアクセストークンを取得し、それを使用してSpringブートアプリケーションへのアクセスを保護します。 、今keycloakでスプリングブートサービスを保護する - JWTトークン

@Configuration 
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class) 
public class KeycloakSecurityConfig extends KeycloakWebSecurityConfigurerAdapter 
{ 
    /** 
    * Registers the KeycloakAuthenticationProvider with the authentication manager. 
    */ 
    @Autowired 
    public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception 
    { 
    final KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider(); 
    keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper()); 
    auth.authenticationProvider(keycloakAuthenticationProvider); 
    } 

    @Bean 
    public KeycloakConfigResolver keycloakConfigResolver() 
    { 
    return new KeycloakSpringBootConfigResolver(); 
    } 

    /** 
    * Defines the session authentication strategy. 
    */ 
    @Bean 
    @Override 
    protected SessionAuthenticationStrategy sessionAuthenticationStrategy() 
    { 
    return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl()); 
    } 

    @Override 
    protected void configure(final HttpSecurity http) throws Exception 
    { 
    super.configure(http); 
    http 
     .authorizeRequests() 
     .antMatchers(
      "/v2/api-docs", 
      "/configuration/ui", 
      "/swagger-resources", 
      "/configuration/security", 
      "/swagger-ui.html", 
      "/webjars/**", 
      "/swagger-resources/configuration/ui", 
      "/swagge‌​r-ui.html", 
      "/swagger-resources/configuration/security").permitAll() 
     .antMatchers("/*").hasRole("user") 
     .anyRequest().authenticated(); 
    } 
} 

:KeycloakWebSecurityConfigurerAdapter

<dependency> 
    <groupId>org.keycloak</groupId> 
    <artifactId>keycloak-spring-boot-starter</artifactId> 
</dependency> 

と設定:

keycloak.realm = master 
keycloak.realmKey = ... 
keycloak.auth-server-url = http://localhost:8080/auth 
keycloak.ssl-required = external 
keycloak.resource = boot-app 
keycloak.bearer-only = true 
keycloak.cors = true 

春ブーツkeycloakスターター:私は、ベアラのみのアクセスタイプを使用してkeycloakプロパティを持つ私の春のブートアプリケーションを構成しましたすべて正常に動作します。私の質問は:ベアラトークンはJWTトークンです、あなたはそれをデコード(およびアクセスを確認)するために必要なすべてのspecificalyあなたが他の設定が必要になるのはなぜ

keycloak.realmKey 

、ある公開鍵、次のとおりです。

keycloak.auth-server-url 

公開鍵では必要なものは何もありませんか?

事前

実際KCのURLが必要な理由あなたは不思議に思うことができ bearer-onlyため

答えて

2

のおかげで私たちは、使用キーローテーションなのでrealmKeyはもはや必須ではありませんいくつかのKCのバージョン以来。これは、アプリがauth-server-urlプロパティを使用してKCサーバーから公開キーを動的に取得することを意味します。

+0

ありがとうございます。それについて私がどこの文書を見つけることができるか知っていますか?または、おそらくSpringブートとの統合についてです。鍵クラークの公式サイトのドキュメントはごくわずかです。 – nejckorasa

+0

OIDCアダプタに関する一般的なJavaのドキュメントを確認しましたか?https://keycloak.gitbooks.io/documentation/securing_apps/topics/oidc/java/java-adapter-config.html? Spring Bootについてあなたは何が欠けていますか?多分私のblogpostはhttps://developers.redhat.com/blog/2017/05/25/easily-secure-your-spring-boot-applications-with-keycloak/を助けることができますか? –

+0

私はこれを見ていないのか分かりません...リンクをありがとう! – nejckorasa

関連する問題