@@ -2,13 +2,11 @@ package com.microservice.kotlin.config
22
33import com.fasterxml.jackson.databind.ObjectMapper
44import com.microservice.authentication.autoconfigure.AuthenticationProperties
5- import com.microservice.authentication.common.service.Base64DecodeUtil
65import com.microservice.web.common.util.CustomDefaultErrorAttributes
76import org.springframework.beans.factory.annotation.Autowired
87import org.springframework.boot.web.error.ErrorAttributeOptions
8+ import org.springframework.context.annotation.Bean
99import org.springframework.context.annotation.Configuration
10- import org.springframework.core.env.Profiles
11- import org.springframework.core.io.FileSystemResource
1210import org.springframework.http.HttpHeaders
1311import org.springframework.http.MediaType
1412import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity
@@ -20,7 +18,6 @@ import org.springframework.security.oauth2.jwt.JwtDecoder
2018import org.springframework.security.oauth2.jwt.NimbusJwtDecoder
2119import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter
2220import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter
23- import org.springframework.security.rsa.crypto.KeyStoreKeyFactory
2421import org.springframework.web.context.request.ServletWebRequest
2522import java.io.IOException
2623import java.nio.charset.StandardCharsets
@@ -35,7 +32,7 @@ import javax.servlet.http.HttpServletResponse
3532class SpringSecurityConfiguration (@Autowired val customDefaultErrorAttributes : CustomDefaultErrorAttributes ,
3633 @Autowired val objectMapper : ObjectMapper ,
3734 @Autowired val properties : AuthenticationProperties ) : WebSecurityConfigurerAdapter() {
38- private val WHITELIST = arrayOf(
35+ private val WHITE_LIST = arrayOf(
3936 // -- swagger ui
4037 // -- swagger ui
4138 " /v3/api-docs/**" , " /swagger-resources" , " /swagger-resources/**" , " /configuration/ui" , " /configuration/security" , " /swagger-ui.html" , " /webjars/**" , " /**/*.js" , " /**/*.css" , " /**/*.html" , " /favicon.ico" ,
@@ -57,33 +54,28 @@ class SpringSecurityConfiguration(@Autowired val customDefaultErrorAttributes: C
5754 .httpBasic().disable()
5855 .logout().disable()
5956 .authorizeRequests()
60- .antMatchers(* WHITELIST ).permitAll()
57+ .antMatchers(* WHITE_LIST ).permitAll()
6158 .anyRequest().authenticated()
6259 .and ()
6360 .oauth2ResourceServer()
6461 .accessDeniedHandler(this ::handleErrorResponse)
6562 .authenticationEntryPoint(this ::handleErrorResponse)
6663 .jwt {
67- val environment = applicationContext.environment
68- val jwtDecoder = if (environment.acceptsProfiles(Profiles .of(" prod" ))) jwtDecoderProd(keyPair(properties)) else jwtDecoder(properties)
64+ val jwtDecoder = jwtDecoder(properties)
6965 it.decoder(jwtDecoder).jwtAuthenticationConverter(jwtAuthenticationConverter())
7066 }
7167 }
7268
73- fun keyPair (properties : AuthenticationProperties ): RSAPublicKey ? {
74- val jwt = properties.jwt
75- val password = Base64DecodeUtil .decodePassword(jwt.keyStorePassword)
76- val keyStoreKeyFactory = KeyStoreKeyFactory (FileSystemResource (jwt.keyStore.replaceFirst(" file:" .toRegex(), " " )), password)
77- return keyStoreKeyFactory.getKeyPair(jwt.keyAlias).public as RSAPublicKey
78- }
79-
80- fun jwtDecoderProd (publicKey : RSAPublicKey ? ): JwtDecoder ? {
81- return NimbusJwtDecoder .withPublicKey(publicKey).build()
82- }
83-
69+ @Bean
8470 fun jwtDecoder (properties : AuthenticationProperties ): JwtDecoder ? {
85- val secretKeySpec = SecretKeySpec (properties.jwt.keyValue.toByteArray(StandardCharsets .UTF_8 ), " HS256" )
86- return NimbusJwtDecoder .withSecretKey(secretKeySpec).build()
71+ val jwt = properties.jwt
72+ return if (jwt != null && jwt.keyValue != null ) {
73+ val secretKeySpec = SecretKeySpec (jwt.keyValue.toByteArray(StandardCharsets .UTF_8 ), " HS256" )
74+ NimbusJwtDecoder .withSecretKey(secretKeySpec).build()
75+ } else {
76+ val publicKey = applicationContext.getBean(RSAPublicKey ::class .java)
77+ NimbusJwtDecoder .withPublicKey(publicKey).build()
78+ }
8779 }
8880
8981 @Throws(IOException ::class )
0 commit comments