Код: Выделить всё
Cannot invoke "org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken.getPrincipal()" because "authToken" is null
java.lang.NullPointerException: Cannot invoke "org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken.getPrincipal()" because "authToken" is null
Я зарегистрировал эти два в консоли разработчика.
Авторизованные источники JavaScript
Для использования с запросами из браузера
URI 1
http://localhost:8085/
Разрешенные URI перенаправления
Для использования с запросами от веб-сервера
URI 1
http://localhost:8085/v1/users/login/oauth2/code/google
Я успешно передаю этап, на котором вы вставляете свой адрес электронной почты и пароль в браузер, но затем происходит вышеуказанное исключение. Я попытался решить с помощью ИИ, и мой код превратился в спагетти.
А еще почтальон сделал что-то необычное, даже если раньше я зарегистрировал это, затем удалил из консоли разработчика, а из кода он все равно запрашивает
redirect_uri: https: //www.mydoamin.com/login/oauth2/code/google
flowName: GeneralOAuthFlow
Код: Выделить всё
@Configuration
@RequiredArgsConstructor
public class SecurityConfig {
private final UserService userService;
private final CustomOAuth2UserService customOAuth2UserService;
@Bean
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http, ClientRegistrationRepository clientRegistrationRepository) throws Exception {
CsrfTokenRequestAttributeHandler csrfTokenRequestAttributeHandler = new CsrfTokenRequestAttributeHandler();
http.authorizeHttpRequests((requests) -> requests
.requestMatchers("/oauth2/**", "/login/oauth2/**").permitAll()
.requestMatchers("v1/users/login/oauth2/code/google").permitAll()
.anyRequest().permitAll()
)
.securityContext(contextConfig -> contextConfig.requireExplicitSave(false))
.csrf(csrfConfigurer -> csrfConfigurer.disable())
.addFilterAfter(new CsrfCookieFilter(), BasicAuthenticationFilter.class)
.formLogin(withDefaults())
.httpBasic(withDefaults())
.oauth2Login(oauth2 -> oauth2
.clientRegistrationRepository(clientRegistrationRepository)
.userInfoEndpoint(userInfoEndpointConfig -> userInfoEndpointConfig
.oidcUserService(this.oidcUserService())
)
);
return http.build();
}
@Bean
public AuthenticationManager authenticationManager(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {
ProjectAuthenticationProvider authenticationProvider =
new ProjectAuthenticationProvider(userDetailsService, passwordEncoder);
ProviderManager providerManager = new ProviderManager(authenticationProvider);
providerManager.setEraseCredentialsAfterAuthentication(true);
return providerManager;
}
@Bean
ClientRegistrationRepository clientRegistrationRepository() {
ClientRegistration google = googleClientRegistration();
return new InMemoryClientRegistrationRepository(google);
}
private ClientRegistration googleClientRegistration() {
return CommonOAuth2Provider.GOOGLE.getBuilder("google").clientId("
Подробнее здесь: [url]https://stackoverflow.com/questions/79381971/oauth2-login-with-google-without-jwt-rest-api[/url]
Мобильная версия