DockerでKeycloakサーバーをセットアップしました。レルムやクライアントなどを構成しました。一部のRestControllersのSpring Bootサービスを正常に作成できました。そのように働きます。
しかし、Spring SecurityをKeycloak Adapterで使用しようとすると、行き詰まってしまいます。
これが私のSecurityConfigです:
@KeycloakConfiguration
class SecurityConfig : KeycloakWebSecurityConfigurerAdapter() {
@Autowired
lateinit var keycloakClientRequestFactory: KeycloakClientRequestFactory
@Bean
fun keycloakConfigResolver(): KeycloakSpringBootConfigResolver = KeycloakSpringBootConfigResolver()
@Bean
override fun sessionAuthenticationStrategy(): SessionAuthenticationStrategy =
NullAuthenticatedSessionStrategy()
@Autowired
@Throws(Exception::class)
fun configureGlobal(auth: AuthenticationManagerBuilder) {
val keycloakAuthProvider: KeycloakAuthenticationProvider = keycloakAuthenticationProvider()
keycloakAuthProvider.setGrantedAuthoritiesMapper(SimpleAuthorityMapper())
auth.authenticationProvider(keycloakAuthProvider)
}
@Throws(Exception::class)
override fun configure(http: HttpSecurity) {
super.configure(http)
http.cors().disable()
.csrf().disable()
.exceptionHandling().and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.sessionAuthenticationStrategy(sessionAuthenticationStrategy()).and()
.formLogin().disable()
.authorizeRequests()
.antMatchers("/apps/manual-data-collection/**").hasRole("user")
.anyRequest().permitAll()
}
/**
* https://www.keycloak.org/docs/latest/securing_apps/index.html#avoid-double-bean-registration
*/
@Bean
@ConditionalOnMissingBean(HttpSessionManager::class)
override fun httpSessionManager(): HttpSessionManager = HttpSessionManager()
}
ここでサーバーにアクセスしようとしましたが、403 Forbidden Errorが発生しました。以下は春ごとのログです:
2020-01-08 14:08:01.817 DEBUG 12396 --- [nio-8080-exec-5] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/apps/manual-data-collection/document-templates'; against '/apps/manual-data-collection/**'
2020-01-08 14:08:01.817 DEBUG 12396 --- [nio-8080-exec-5] o.s.s.w.a.i.FilterSecurityInterceptor : Secure object: FilterInvocation: URL: /apps/manual-data-collection/document-templates; Attributes: [hasRole('ROLE_user')]
2020-01-08 14:08:01.817 DEBUG 12396 --- [nio-8080-exec-5] o.s.s.w.a.i.FilterSecurityInterceptor : Previously Authenticated: org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken@bc432bbc: Principal: willy; Credentials: [PROTECTED]; Authenticated: true; Details: org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount@42db7613; Not granted any authorities
2020-01-08 14:08:01.818 DEBUG 12396 --- [nio-8080-exec-5] o.s.s.access.vote.AffirmativeBased : Voter: org.springframework.security.web.access.expression.WebExpressionVoter@3d0602a7, returned: -1
2020-01-08 14:08:01.818 DEBUG 12396 --- [nio-8080-exec-5] o.s.s.w.a.ExceptionTranslationFilter : Access is denied (user is not anonymous); delegating to AccessDeniedHandler
org.springframework.security.access.AccessDeniedException: Access is denied
私を混乱させるのは、それが言うことです:Not granted any authorities
。この部分を省略しても、すべてが正常に機能します。必要なロール(この場合は「ユーザー」)はレルムに格納されます。JWTトークンもこれらを提供します。なぜうまくいかないのか私にはわかりません。
編集:私は言及すべきです。Spring Boot Serverは、AngularアプリケーションのRESTサーバーとしてのみ使用する必要があるため、これを使用しますSTATELESS
。
はい、間違いを見つけました。application.ymlで以下をアクティブにしました:
keycloak.use-resource-role-mappings=true
しかし、それはそうでなければなりませんfalse
。Keycloakのドキュメントには次のように書かれています:
use-resource-role-mappings -trueに設定されている場合、アダプターはトークン内でユーザーのアプリケーションレベルのロールマッピングを検索します。falseの場合、ユーザーロールマッピングのレルムレベルを調べます。これはオプションです。デフォルト値はfalseです。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加