2017-08-08 107 views
0

所以,我使用keycloak来保护我的服务。客户端应用程序从keycloak服务器获取访问令牌,并使用它来保护对Spring引导应用程序的访问。保护与keycloak春季启动服务 - 智威汤逊代币

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首发:

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

和配置KeycloakWebSecurityConfigurerAdapter:我只使用承载通路型keycloak属性来配置我的春节,启动应用程序

@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(); 
    } 
} 

现在,一切正常。我的问题是:承载令牌是智威汤逊的道理,你需要对它进行解码(并验证访问)是公共密钥,这是

keycloak.realmKey 

你为什么会需要其他设置,specificaly:

keycloak.auth-server-url 

是不是公钥你需要的一切?

在此先感谢

回答

2

确实为bearer-only你可能想知道为什么需要对KC URL,但由于几KC版本的realmKey不是强制性的了,因为我们使用的密钥轮换。这意味着您的应用程序将使用auth-server-url属性从KC服务器动态检索公钥。

+0

感谢您的解释。你知道我在哪里可以找到有关这方面的任何文件吗?或者也许关于与Spring引导的整合。我在keycloak官方网站上的文档非常少。 – nejckorasa

+0

您是否检查过有关OIDC适配器的“generic 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