Spring Boot 2 RESTアプリケーションがあり、同じリソースへのGoogleログインまたはLDAP認証をサポートするようにSpring Securityを構成したい(たとえば、/ employees)
私はすでにhttpBasic(Apache AD LDAPサーバーに接続)を介して認証を行っています。
また、Google OAuth2サインインによる認証を設定しました。この両方の構成は別々に正しく機能します(Googleのサインインを介して認証できますが、Springのセキュリティを再設定する必要があるため、同時にLDAPを使用して認証することはできません)。次に、この両方の方法で認証する機能が必要です同時に。
LDAP認証のSpring Security構成
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/", "/login**","/callback/", "/webjars/**", "/error**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.httpBasic()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.ldapAuthentication()
.ldapAuthoritiesPopulator(customLdapAuthoritiesPopulator)
.userDnPatterns("uid={0},ou=people")
.groupSearchBase("ou=groups")
.contextSource()
.url(env.getProperty("spring.ldap.urls") + env.getProperty("spring.ldap.base"))
.and()
.passwordCompare()
.passwordAttribute("userPassword")
.passwordEncoder(new LdapShaPasswordEncoder());
}
そして、これは私がGoogle OAuth2サインインのためにSpring Securityを再構成したときの見え方です
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/", "/login**","/callback/", "/webjars/**", "/error**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.oauth2Login()
.userInfoEndpoint().oidcUserService(customOAuth2UserService);
}
私が必要とする結果:ユーザーには2つのオプションがあります。Oauth2で認証するか、または必要に応じて、httpBasic LDAPで認証します。
OAuth2とhttpBasic LDAPが連動するようにSpring Securityを構成する方法はあると思いますが、それを行う方法がわかりません。
可能です。
基本認証は基本を使用しますが、oauthはヘッダー承認ヘッダーの一部としてベアラーを使用します。
カスタム要求マッチャーを使用して、基本認証を検出し、LDAPで認証できます。そうでない場合は、oauthを介して流れます。
まず、WebSecurityConfigurerAdapterをOauth認証サーバーよりも上位に注文します。
@Configuration
@Order(2)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
カスタムリクエストマッパーを使用します。
http
.csrf()
.disable()
.requestMatcher(new BasicRequestMatcher())
.authorizeRequests()
.antMatchers("/", "/login**","/callback/", "/webjars/**", "/error**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.httpBasic()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
カスタムリクエストマッチャー、
private static class BasicRequestMatcher implements RequestMatcher {
@Override
public boolean matches(HttpServletRequest request) {
String auth = request.getHeader("Authorization");
return (auth != null && auth.startsWith("Basic"));
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加