SecurityConfig.kt
/*
Copyright (C) 2026 Sber
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.archtool.archloader.configuration
import com.nimbusds.jose.jwk.RSAKey
import java.nio.file.Files
import java.nio.file.Paths
import org.archtool.archloader.configuration.properties.SecurityProperties
import org.archtool.archloader.controller.filter.JwtDevFilter
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.invoke
import org.springframework.security.oauth2.jwt.JwtDecoder
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder
import org.springframework.security.oauth2.server.resource.web.authentication.BearerTokenAuthenticationFilter
import org.springframework.security.web.SecurityFilterChain
@Configuration
class SecurityConfig(
private val securityProperties: SecurityProperties,
) {
@Bean
fun securityWebFilterChain(http: HttpSecurity): SecurityFilterChain {
http {
csrf { disable() }
if (securityProperties.devMode?.enabled == true) { // если devMode включен, добавляем фильтр, который подставляет заголовок с авторизацией
val jwtDevFilter = JwtDevFilter(securityProperties = securityProperties)
addFilterBefore<BearerTokenAuthenticationFilter>(jwtDevFilter)
}
authorizeHttpRequests {
authorize("/actuator/**", permitAll)
authorize("/v3/api-docs*/**", permitAll)
authorize("/swagger-ui/**", permitAll)
authorize("/swagger-ui.html", permitAll)
authorize(anyRequest, authenticated)
}
oauth2ResourceServer {
jwt {}
}
}
return http.build()
}
@Bean
fun jwtDecoder(securityProperties: SecurityProperties): JwtDecoder {
val publicKeyPEM = Files.readString(Paths.get(securityProperties.publicKeyLocation))
val publicKey = RSAKey.parseFromPEMEncodedObjects(publicKeyPEM).toRSAKey().toRSAPublicKey()
return NimbusJwtDecoder
.withPublicKey(publicKey)
.build()
}
}