fix security configuration
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
dmitriy.churmeev
2026-03-29 15:56:26 +03:00
parent db617bbfc7
commit db6356bd01

View File

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