【发布时间】:2021-12-12 02:56:50
【问题描述】:
我已经实现了 here 指定的代码,以将多租户功能添加到我的 Spring Security 配置中。但是,当我的 Spring Boot 应用程序启动时,我遇到以下错误:
2021-10-26 | 10:31:37.762 | main | WARN | ConfigServletWebServerApplicationContext | Trace: | Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.oauth2.jwt.JwtDecoder' available
2021-10-26 | 10:31:39.361 | main | ERROR | o.s.b.d.LoggingFailureAnalysisReporter | Trace: |
***************************
APPLICATION FAILED TO START
***************************
Description:
Method springSecurityFilterChain in org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration required a bean of type 'org.springframework.security.oauth2.jwt.JwtDecoder' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.security.oauth2.jwt.JwtDecoder' in your configuration.
文档说明:
这很好,因为发布者端点是延迟加载的。实际上,对应的 JwtAuthenticationProvider 只是在发送了对应 issuer 的第一个请求时才会实例化。
我不认为在应用程序启动时 JwtDecoder 会根据本文档被实例化。我的配置中缺少什么?
更新
在 Steve Riesenberg 的帮助下,我现在正在编译以下代码。你可以在我的代码 sn-p 中看到我曾经工作的内容(即在我们有多租户要求之前)现在被注释掉了:
//.jwt().jwtAuthenticationConverter(jwtAccessTokenConverter);
String[] issuers = new String[] {"https://www.example.com/auth/realms/example"};
JwtIssuerAuthenticationManagerResolver jwtIssuerAuthenticationManagerResolver =
new JwtIssuerAuthenticationManagerResolver(issuers);
...
.anyRequest()
.authenticated()
.and()
.oauth2ResourceServer(
oauth2ResourceServerConfigurer ->
oauth2ResourceServerConfigurer
.authenticationManagerResolver(jwtIssuerAuthenticationManagerResolver)
.authenticationEntryPoint(authenticationExceptionHandler));
// .jwt().jwtAuthenticationConverter(jwtAccessTokenConverter);
但是,由于我不得不删除 .jwt(),所以现在无法提供我自己的令牌转换器,我仍然不清楚默认转换器为我提供了什么。
另外,我不清楚为什么我需要使用JwtIssuerAuthenticationManagerResolver 的第三个构造函数并提供我自己的AuthenticationManagerResolver<String>?如果我上面的代码正在编译,为什么我需要这样做?
【问题讨论】:
标签: spring-security spring-security-oauth2