fix security configuration
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
@@ -19,56 +19,53 @@ import org.springframework.security.web.SecurityFilterChain
|
|||||||
prePostEnabled = true,
|
prePostEnabled = true,
|
||||||
securedEnabled = true
|
securedEnabled = true
|
||||||
)
|
)
|
||||||
class WebSecurityConfig () {
|
class WebSecurityConfig {
|
||||||
|
|
||||||
private val logger = org.slf4j.LoggerFactory.getLogger(WebSecurityConfig::class.java)
|
private val logger = org.slf4j.LoggerFactory.getLogger(WebSecurityConfig::class.java)
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun filterChain(http: HttpSecurity): SecurityFilterChain? {
|
fun filterChain(http: HttpSecurity): SecurityFilterChain {
|
||||||
logger.info("=== INITIALIZING SECURITY FILTER CHAIN ===")
|
logger.info("=== INITIALIZING SECURITY FILTER CHAIN ===")
|
||||||
|
|
||||||
http
|
http
|
||||||
// .authorizeHttpRequests { auth ->
|
.oauth2ResourceServer { oauth2 ->
|
||||||
//
|
|
||||||
// auth.requestMatchers("/public/**", "/health", "/info").permitAll()
|
|
||||||
// auth.anyRequest().permitAll()
|
|
||||||
// //auth.anyRequest().authenticated()
|
|
||||||
// }
|
|
||||||
|
|
||||||
.oauth2ResourceServer { oauth2 -> {
|
|
||||||
logger.info("=== oauth2ResourceServer ===")
|
logger.info("=== oauth2ResourceServer ===")
|
||||||
// oauth2!!.jwt(Customizer.withDefaults())
|
|
||||||
// добавляем конвертер для извлечения ролей
|
|
||||||
oauth2.jwt { jwt ->
|
oauth2.jwt { jwt ->
|
||||||
{
|
logger.info("=== Configuring JWT ===")
|
||||||
logger.info("=== jwtAuthenticationConverter() ==="+jwtAuthenticationConverter())
|
|
||||||
jwt.jwtAuthenticationConverter(jwtAuthenticationConverter())
|
jwt.jwtAuthenticationConverter(jwtAuthenticationConverter())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}} // проверка JWT токен
|
.sessionManagement { session ->
|
||||||
.sessionManagement {{ session: SessionManagementConfigurer<HttpSecurity?>? ->
|
|
||||||
logger.info("=== sessionManagement ===")
|
logger.info("=== sessionManagement ===")
|
||||||
session!!.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
||||||
}} // без сессий
|
}
|
||||||
.csrf { csrf: CsrfConfigurer<HttpSecurity?>? -> csrf!!.disable() } // отключаем CSRF для API
|
.csrf { csrf ->
|
||||||
|
csrf.disable()
|
||||||
|
}
|
||||||
|
|
||||||
return http.build()
|
return http.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun jwtAuthenticationConverter(): JwtAuthenticationConverter {
|
private fun jwtAuthenticationConverter(): JwtAuthenticationConverter {
|
||||||
|
logger.info("=== Creating JWT Authentication Converter ===")
|
||||||
|
|
||||||
val converter = JwtAuthenticationConverter()
|
val converter = JwtAuthenticationConverter()
|
||||||
|
|
||||||
converter.setJwtGrantedAuthoritiesConverter { jwt ->
|
converter.setJwtGrantedAuthoritiesConverter { jwt ->
|
||||||
|
logger.info("=== JWT Converter called for token ===")
|
||||||
|
|
||||||
val roles = mutableSetOf<SimpleGrantedAuthority>()
|
val roles = mutableSetOf<SimpleGrantedAuthority>()
|
||||||
|
|
||||||
// Извлекаем роли из resource_access.test_client.roles
|
// Извлекаем роли из resource_access.test_client.roles
|
||||||
val resourceAccess = jwt.claims["resource_access"] as? Map<*, *>
|
val resourceAccess = jwt.claims["resource_access"] as? Map<*, *>
|
||||||
logger.info("Roles1! "+resourceAccess)
|
logger.info("resource_access: $resourceAccess")
|
||||||
|
|
||||||
val clientAccess = resourceAccess?.get("test_client") as? Map<*, *>
|
val clientAccess = resourceAccess?.get("test_client") as? Map<*, *>
|
||||||
val clientRoles = clientAccess?.get("roles") as? List<*>
|
val clientRoles = clientAccess?.get("roles") as? List<*>
|
||||||
|
|
||||||
clientRoles?.forEach { role ->
|
clientRoles?.forEach { role ->
|
||||||
|
logger.info("Adding ROLE_$role")
|
||||||
roles.add(SimpleGrantedAuthority("ROLE_$role"))
|
roles.add(SimpleGrantedAuthority("ROLE_$role"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,9 +74,11 @@ class WebSecurityConfig () {
|
|||||||
val realmRoles = realmAccess?.get("roles") as? List<*>
|
val realmRoles = realmAccess?.get("roles") as? List<*>
|
||||||
|
|
||||||
realmRoles?.forEach { role ->
|
realmRoles?.forEach { role ->
|
||||||
|
logger.info("Adding realm ROLE_$role")
|
||||||
roles.add(SimpleGrantedAuthority("ROLE_$role"))
|
roles.add(SimpleGrantedAuthority("ROLE_$role"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.info("Final authorities: ${roles.map { it.authority }}")
|
||||||
roles.toList()
|
roles.toList()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,6 +86,7 @@ class WebSecurityConfig () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// // Конвертер JWT Authentication (извлекает роли из токена)
|
// // Конвертер JWT Authentication (извлекает роли из токена)
|
||||||
// private fun jwtAuthenticationConverter(): JwtAuthenticationConverter {
|
// private fun jwtAuthenticationConverter(): JwtAuthenticationConverter {
|
||||||
// val converter = JwtAuthenticationConverter()
|
// val converter = JwtAuthenticationConverter()
|
||||||
|
|||||||
Reference in New Issue
Block a user