This commit is contained in:
a.ilin
2026-10-07 13:53:23 +03:00
parent f6c73cf062
commit 71c0274e6e
3 changed files with 39 additions and 1 deletions
@@ -0,0 +1,10 @@
package ru.gorodar.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
@ConfigurationProperties(prefix = "gorodar.cors")
public record CorsProperties(
List<String> allowedOrigins) {
}
@@ -6,6 +6,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
@@ -18,9 +19,13 @@ import org.springframework.security.oauth2.jwt.JwtValidators;
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import ru.gorodar.auth.JwkKeys;
import ru.gorodar.auth.TokenService;
import ru.gorodar.config.AuthProperties;
import ru.gorodar.config.CorsProperties;
import java.util.List;
@@ -31,9 +36,12 @@ public class SecurityConfig {
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http,
Converter<Jwt, JwtAuthenticationToken> jwtAuthenticationConverter,
JwtDecoder jwtDecoder) throws Exception {
JwtDecoder jwtDecoder,
CorsConfigurationSource corsConfigurationSource) throws Exception {
http
.csrf(csrf -> csrf.disable())
// preflight OPTIONS не несёт Authorization — CorsFilter должен отработать до security
.cors(cors -> cors.configurationSource(corsConfigurationSource))
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth -> auth
// собственная авторизация (email-код) и OIDC discovery — публичны
@@ -58,6 +66,22 @@ public class SecurityConfig {
return http.build();
}
/**
* Разрешённые origin'ы браузерных приложений (дев фронтенда, прод SPA) —
* gorodar.cors.allowed-origins. Авторизация Bearer-токеном, cookies не используются,
* поэтому allowCredentials не нужен.
*/
@Bean
CorsConfigurationSource corsConfigurationSource(CorsProperties properties) {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(properties.allowedOrigins());
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"));
configuration.setAllowedHeaders(List.of("Authorization", "Content-Type"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
/**
* Валидация собственных access-токенов по локальным ключам (без HTTP): подпись,
* срок, issuer и audience = gorodar.
+4
View File
@@ -30,6 +30,10 @@ spring:
enabled: ALWAYS
gorodar:
# origin'ы браузерных клиентов; на сервере задаётся GORODAR_CORS_ALLOWED_ORIGINS
# (список через запятую), по умолчанию — дев-сервер фронтенда
cors:
allowed-origins: ${GORODAR_CORS_ALLOWED_ORIGINS:http://localhost:4200}, https://gorodar.ru
auth:
client-id: gorodar
issuer: ${GORODAR_AUTH_ISSUER:http://localhost:8080}