У меня есть сервер авторизации и сервер ресурсов. Я автоматически настроил реализацию OAuth по умолчанию. Код не требовался, и только конфигурация была сделана в
spring:
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri:<auth server configuration url here>
Maven Dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>
spring-boot-starter-oauth2-resource-server
</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
В настоящее время все мои URL-адреса защищены.
@PostMapping(path = "/secure/test", consumes = "application/json", produces = "application/json")
public @ResponseBody String testService(@RequestBody String name ,@RequestHeader Map<String, String>headers) {
headers.forEach((K,V)->printHeaders(K,V));
}
Я хочу, чтобы мой URI ниже был доступен для всех,
@PostMapping(path = "/test", consumes = "application/json", produces = "application/json")
public @ResponseBody String testAuthService(@RequestBody String name ,@RequestHeader Map<String, String>headers) {
headers.forEach((K,V)->printHeaders(K,V));
}
Я попытался переопределить приведенный ниже, но получил ошибку 403 при попытке без токена-носителя
@Configuration
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) {
try {
http
.authorizeRequests(authorize -> authorize
.mvcMatchers("/api/secure/**").authenticated()
.anyRequest().permitAll()
)
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Ответ об ошибке
{
"timestamp": "2020-05-07T14:28:22.729+0000",
"status": 403,
"error": "Forbidden",
"message": "Forbidden",
"path": "/api/test"
}
Как я могу разрешить аутентификацию только определенных URL-адресов с помощью Spring Security 5.2.2 + Spring Boot 2.2 + Автоконфигурация Oauth