-
-
Save yamabicodev/bf102a5ecda0f7878c1b30039e57902b to your computer and use it in GitHub Desktop.
Spring Security Demo 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class OAuth2ClientConfig { | |
// ルートパス単位でWebSecurityConfigurerAdapterを継承したクラスを用意する。 | |
@Configuration | |
@Order(1) | |
static class OAuth2ClientApp1Config extends WebSecurityConfigurerAdapter { | |
@Override | |
protected void configure(HttpSecurity http) throws Exception { | |
http. | |
//1. 特定のルートパスを認証処理対象に設定する。 | |
antMatcher("/app1/**") | |
.authorizeRequests() | |
.antMatchers("/css/**", "/app1/**").permitAll() | |
.anyRequest().authenticated() | |
.and() | |
//2. 明示的なAuthorization Endpointの指定する。 | |
.authorizationEndpoint() | |
.baseUri("/app1/authorization") | |
.and() | |
.oauth2Login() | |
//3. 明示的なRedirect Endpointの指定する。 | |
.redirectionEndpoint() | |
.baseUri("/app1/login/code/**") | |
.and() | |
//4. Javaでソーシャルログイン認証設定の実装 | |
.clientRegistrationRepository(clientRegistrationRepository("http://localhost:8080/app1/login/code/google", List.of("openid", "email"))) | |
.loginPage("/app1/login") | |
.defaultSuccessUrl("/app1/") | |
.and() | |
.logout() | |
.logoutRequestMatcher(new AntPathRequestMatcher("/app1/logout")) | |
.logoutSuccessUrl("/app1/"); | |
} | |
} | |
// ルートパス単位でWebSecurityConfigurerAdapterを継承したクラスを用意する。 | |
@Configuration | |
@Order(2) | |
static class OAuth2ClientApp2Config extends WebSecurityConfigurerAdapter { | |
@Override | |
protected void configure(HttpSecurity http) throws Exception { | |
http. | |
//1. 特定のルートパスを認証処理対象に設定する。 | |
antMatcher("/app2/**") | |
.authorizeRequests() | |
.antMatchers("/css/**", "/app2/**").permitAll() | |
.anyRequest().authenticated() | |
.and() | |
.oauth2Login() | |
//2. 明示的なAuthorization Endpointの指定する。 | |
.authorizationEndpoint() | |
.and() | |
.baseUri("/app2/authorization") | |
//3. 明示的なRedirect Endpointの指定する。 | |
.redirectionEndpoint() | |
.baseUri("/app2/login/code/**") | |
.and() | |
//4. Javaでソーシャルログイン認証設定の実装 | |
.clientRegistrationRepository(clientRegistrationRepository("http://localhost:8080/app2/login/code/google", List.of("openid", "email", "profile"))) | |
.loginPage("/app2/login") | |
.defaultSuccessUrl("/app2/") | |
.and() | |
.logout() | |
.logoutRequestMatcher(new AntPathRequestMatcher("/app2/logout")) | |
.logoutSuccessUrl("/app2/"); | |
} | |
} | |
//4. Javaでソーシャルログイン認証設定の実装 | |
static ClientRegistrationRepository clientRegistrationRepository(String redirectUrl, List<String> scope) { | |
var clientRegistration = ClientRegistration | |
.withRegistrationId("google") | |
.clientId("{your-google-client-id}") | |
.clientSecret("{your-google-client-secret}") | |
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) | |
.userNameAttributeName("sub") | |
.redirectUri(redirectUrl) | |
.scope(scope) | |
.authorizationUri("https://accounts.google.com/o/oauth2/v2/auth") | |
.tokenUri("https://www.googleapis.com/oauth2/v4/token") | |
.jwkSetUri("https://www.googleapis.com/oauth2/v3/certs") | |
.issuerUri("https://accounts.google.com") | |
.userInfoUri("https://accounts.google.com") | |
.build(); | |
// Googleの場合は認可エンドポイント情報等をSpring Securityのクラス「CommonOAuth2Provider」が補完してくれるので、以下の実装だけでも良い。 | |
// var clientRegistration = CommonOAuth2Provider.GOOGLE | |
// .getBuilder("google") | |
// .clientId("{your-google-client-id}") | |
// .clientSecret("{your-google-client-secret}") | |
// .redirectUri(redirectUrl) | |
// .scope(scope) | |
// .build(); | |
return new InMemoryClientRegistrationRepository(clientRegistration); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment