I’m trying to implement auth-server – resource-server – client-server cluster.
I successfully can start auth server but when I try to run resource server I get the following error
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘jwtDecoder’ defined in class path resource [io/xdata/group/exchange/configuration/SecurityConfig.class]: Failed to instantiate [org.springframework.security.oauth2.jwt.JwtDecoder]: Factory method ‘jwtDecoder’ threw exception with message: Unable to resolve the Configuration with the provided Issuer of “http://127.0.0.1:9000”
.
Caused by: org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [java.util.Map<java.lang.String, java.lang.Object>] and content type [text/html;charset=UTF-8]
Here are my configs:
Auth server config
@Configuration(proxyBeanMethods = false)
class OAuthServerConfig {
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
fun filterChain(http: HttpSecurity): SecurityFilterChain {
OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http)
return http.formLogin(Customizer.withDefaults()).build()
}
@Bean
fun clientRepository(): InMemoryRegisteredClientRepository {
val client = RegisteredClient.withId(UUID.randomUUID().toString())
.clientId("frontend")
.clientSecret("frontend")
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
.authorizationGrantType(AuthorizationGrantType.PASSWORD)
.authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
.redirectUri("http://127.0.0.1/login/oauth2/frontend")
.redirectUri("http://127.0.0.1/login/oauth2/authorized")
.scope(OidcScopes.OPENID)
.scope("api.all")
.clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
.build()
return InMemoryRegisteredClientRepository(client)
}
@Bean
fun jwkSource(): JWKSource<SecurityContext> {
val key = generateRsa()
val set = JWKSet(key)
return JWKSource { selector, _ -> selector.select(set) }
}
private fun generateRsa(): RSAKey {
val keyPair = generateRsaKey()
val publicKey = keyPair.public as RSAPublicKey
val privateKey = keyPair.private as RSAPrivateKey
return RSAKey.Builder(publicKey)
.privateKey(privateKey)
.keyID(UUID.randomUUID().toString())
.build()
}
private fun generateRsaKey() = try {
val generator = KeyPairGenerator.getInstance("RSA")
generator.initialize(2048)
generator.generateKeyPair()
} catch (e: Exception) {
throw IllegalArgumentException()
}
}
Auth server security config
@Configuration
@EnableWebSecurity
class WebSecurityConfig(
private val customAuthenticationProvider: CustomAuthenticationProvider
) {
@Bean
fun authorizationServerSecurityFilterChain(http: HttpSecurity): SecurityFilterChain {
http.authorizeHttpRequests {
it.anyRequest().authenticated()
}.formLogin(withDefaults())
return http.formLogin(withDefaults()).build()
}
@Autowired
fun bindAuthenticationProvider(managerBuilder: AuthenticationManagerBuilder) {
managerBuilder.authenticationProvider(customAuthenticationProvider)
}
}
Auth server application.yml
server:
port: 9000
spring:
datasource:
url: jdbc:h2:mem:crypto-exchange
driver-class-name: org.h2.Driver
username: sa
password: password
hikari:
schema: PUBLIC
security:
oauth2:
authorizationserver:
client:
frontend:
registration:
client-id: "frontend"
client-secret: "{noop}frontend"
client-authentication-methods:
- client_secret_basic
authorization-grant-types:
- authorization_code
- refresh_token
redirect-uris:
- http://127.0.0.1:8080/login/oauth2/code/frontend
scopes:
- openid
- all
Resource server security config
@Configuration
@EnableWebSecurity
class SecurityConfig {
@Bean
fun userDetailsService(http: HttpSecurity): SecurityFilterChain {
http.authorizeHttpRequests {
it.requestMatchers("/api/**")
.hasAuthority("SCOPE_api.all")
}.oauth2ResourceServer {
it.jwt { jwt ->
jwt.decoder(jwtDecoder())
}
}
return http.build()
}
@Bean
fun jwtDecoder(): JwtDecoder =
JwtDecoders.fromIssuerLocation("http://127.0.0.1:9000")
}
Resource server application.yml
spring:
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: http://127.0.0.1:9000/.well-known/jwks.json
issuer-uri: http://127.0.0.1:9000
Any ideas?