Настройка OAuth2 через Яндекс ID, используя SpringSecurity
пытаюсь разобраться с OAuth2. Настроил сервер для авторизации через Yandex ID. Все работает, когда обращасюсь к адресу авторизации сервера через браузер. Но у меня rest api приложение и когда я на клиенте (vue js) пытаюсь обратится к этому же методу, то появляется ошибка:
В заголовке действительно нет поля 'Access-Control-Allow-Origin', но тем не менее у клиента есть доступ к другим методам контроллера и никакой ошибки не возникает, хотя там в заголовках также нет этого поля
Мой код SecurityConfig:
@Configuration
@EnableWebSecurity
public class SecurityConfig {
private final PersonDetailsService personDetailsService;
@Autowired
public SecurityConfig(PersonDetailsService personDetailsService) {
this.personDetailsService = personDetailsService;
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf()
.disable()
.authorizeHttpRequests((requests)-> requests
.requestMatchers("/login-from-yandex").authenticated()
.anyRequest().permitAll())
.oauth2Login();
return http.build();
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(personDetailsService);
authProvider.setPasswordEncoder(passwordEncoder());
return authProvider;
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
return authConfig.getAuthenticationManager();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(11);
}
}
Конфигурация Cors:
@Configuration
public class CorsConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:8081")
.allowedMethods("*")
.allowedHeaders("*")
.allowCredentials(true); // Разрешение передачи cookies и авторизационных заголовков
System.out.println("FROM CORS CONFIGURATION");
}
}
Тестовый метод контроллера:
@GetMapping("/login-from-yandex")
@CrossOrigin
public ResponseEntity<?> login(@AuthenticationPrincipal OAuth2User principal){
return ResponseEntity.ok(principal);
}
Vue js метод:
yandexAuthorizated(){
axios.get('http://localhost:8080/login-from-yandex')
.then(response => {
console.log(response.data)
this.yandex = response.data
})
.catch(error => {
console.log(error)
})
},
Подскажите пожалуйста, что я делаю не так, потому-что я не совсем понимаю, что делать
