Поскольку вы используете весеннюю загрузку, вы можете использовать следующие зависимости для автоматической настройки безопасности пружины для oauth2:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
После добавления вышеуказанной зависимости, если вы хотите что-то переопределить. Spring Boot 2.x класс автоконфигурации для поддержки клиента OAuth: OAuth2ClientAutoConfiguration .
. Он выполняет следующие задачи:
Регистрирует репозиторий ClientRegistrationRepository @ Bean , состоящий из ClientRegistration (ов) из настроенных свойств клиента OAuth.
Предоставляет WebSecurityConfigurerAdapter @ Configuration и включает OAuth 2.0. Вход через httpSecurity.oauth2Login ().
Если вам необходимо переопределить автоконфигурацию в зависимости от вашей конкретнойтребований, вы можете сделать это следующими способами:
- Зарегистрировать репозиторий ClientRegistrationRepository @ Bean
- Предоставить WebSecurityConfigurerAdapter
- Полностью переопределить автоконфигурацию
Переопределив WebSecurityConfigurerAdapter следующим образом:
@EnableWebSecurity
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.oauth2Login()
.clientRegistrationRepository(this.clientRegistrationRepository())
.authorizedClientService(this.authorizedClientService())
.loginPage("/login")
.authorizationEndpoint()
.baseUri(this.authorizationRequestBaseUri())
.authorizationRequestRepository(this.authorizationRequestRepository())
.and()
.redirectionEndpoint()
.baseUri(this.authorizationResponseBaseUri())
.and()
.tokenEndpoint()
.accessTokenResponseClient(this.accessTokenResponseClient())
.and()
.userInfoEndpoint()
.userAuthoritiesMapper(this.userAuthoritiesMapper())
.userService(this.oauth2UserService())
.oidcUserService(this.oidcUserService())
.customUserType(GitHubOAuth2User.class, "github");
}
}
Для справки см .:
https://docs.spring.io/spring-security/site/docs/5.0.7.RELEASE/reference/html/oauth2login-advanced.html
и
https://docs.spring.io/spring-security/site/docs/current/reference/html/jc.html