Мое приложение использует Oauth2 + Keycloack.
Вот мое приложение.yaml:
spring:
devtools:
add-properties: 'true'
flyway:
enabled: 'true'
baseline-on-migrate: 'true'
datasource:
password: ${DB_PASS}
username: ${DB_USER}
url: jdbc:postgresql://${DB_HOSTNAME}:${DB_PORT}/${DB_DATABASE}
jpa:
hibernate:
ddl-auto: validate
properties:
hibernate:
format_sql: 'false'
show_sql: 'false'
security:
oauth2:
resourceserver:
jwt:
issuer-uri: ${SSO_URI}/realms/crm
jwk-set-uri: ${SSO_URI}/realms/crm/protocol/openid-connect/certs
client:
registration:
keycloak:
provider: keycloak
authorization-grant-type: authorization_code
client-id: ${SSO_CLLIENT_ID}
client-secret: ${SSO_SECRET}
scope: openid
provider:
keycloak:
issuer-uri: ${SSO_URI}/realms/crm
keycloak:
cors: true
logging:
level:
org.springframework.security: DEBUG
Конфигурация безопасности:
package io.crm.backend.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.core.GrantedAuthorityDefaults;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.JwtDecoders;
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 org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebSecurity
public class SecurityConfiguration {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.anyRequest().authenticated()
)
.oauth2Login(Customizer.withDefaults())
.oauth2Client(Customizer.withDefaults())
.oauth2ResourceServer(oauth2 -> oauth2
.jwt(Customizer.withDefaults())
);
return http.build();
}
@Bean
public JwtDecoder jwtDecoder() {
// IM AWARE THAT THIS ENV WILL NOT WORK. IT'S HERE JUST FOR SIMPLICITY OF MY EXAMPLE.
return JwtDecoders.fromIssuerLocation("${SSO_URI}");
}
@Bean
GrantedAuthorityDefaults grantedAuthorityDefaults() {
return new GrantedAuthorityDefaults("");
}
}
Ошибка:
Это HTTP-запрос:
curl 'http://localhost:8080/parameters' \
-H 'sec-ch-ua-platform: "Linux"' \
-H 'Authorization: Bearer A_VALID_JWT_TOKEN' \
-H 'Referer: http://localhost:5173/' \
-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36' \
-H 'sec-ch-ua: "Chromium";v="130", "Brave";v="130", "Not?A_Brand";v="99"' \
-H 'Content-Type: application/json' \
-H 'sec-ch-ua-mobile: ?0' \
--data-raw '{"name":"A","value":"B","measureUnit":"C"}'
Итак, возникла ошибка CORS, которую я не знаю, как исправить.
Как вы можете видеть ниже, я настроил источники на клавиатуре на "*", но это не работает. Я не уверен, что в Spring не хватает чего-то еще для настройки.

Я не уверен, что запрос на выборку не выполнен из-за предполетной проверки, которая получает 401....
Ребята, вы настроили весеннюю загрузку с помощью oauth2 и плащ-ключ? Как это исправить?
Зависимости моего проекта:
plugins {
id 'java'
id 'org.springframework.boot' version '3.4.0'
id 'io.spring.dependency-management' version '1.1.6'
}
---
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.security:spring-security-oauth2-resource-server'
implementation 'org.springframework.boot:spring-boot-starter-web'
Подробнее здесь: https://stackoverflow.com/questions/792 ... ess-this-r
Мобильная версия