From db6356bd010a6c8798192a3419386f32a945606c Mon Sep 17 00:00:00 2001 From: "dmitriy.churmeev" Date: Sun, 29 Mar 2026 15:56:26 +0300 Subject: [PATCH] fix security configuration --- .../com/auth/config/WebSecurityConfig.kt | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/com/auth/config/WebSecurityConfig.kt b/src/main/kotlin/com/auth/config/WebSecurityConfig.kt index 0804123..d2af1bd 100644 --- a/src/main/kotlin/com/auth/config/WebSecurityConfig.kt +++ b/src/main/kotlin/com/auth/config/WebSecurityConfig.kt @@ -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 -> { - logger.info("=== oauth2ResourceServer ===") - // oauth2!!.jwt(Customizer.withDefaults()) - // добавляем конвертер для извлечения ролей + .oauth2ResourceServer { oauth2 -> + logger.info("=== oauth2ResourceServer ===") oauth2.jwt { jwt -> - { - logger.info("=== jwtAuthenticationConverter() ==="+jwtAuthenticationConverter()) - jwt.jwtAuthenticationConverter(jwtAuthenticationConverter()) - } + logger.info("=== Configuring JWT ===") + jwt.jwtAuthenticationConverter(jwtAuthenticationConverter()) } - }} // проверка JWT токен - .sessionManagement {{ session: SessionManagementConfigurer? -> - logger.info("=== sessionManagement ===") - session!!.sessionCreationPolicy(SessionCreationPolicy.STATELESS) - }} // без сессий - .csrf { csrf: CsrfConfigurer? -> csrf!!.disable() } // отключаем CSRF для API + } + .sessionManagement { session -> + logger.info("=== sessionManagement ===") + 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() // Извлекаем роли из 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()