複数の認証メカニズムを接続するSpring Boot Security

ルディガー:

を介してユーザーを認証するアプリケーションのセキュリティ設定がありますLDAPこれはかなりうまくいきますが、今度AuthenticationProviderは、認証を試みるユーザーに対してさらにチェックを行う別のものを追加したいと思います。だから私はDbAuthenticationProvider(テスト目的で)常にアクセスを拒否することを追加しようとしましたしたがって、(で機能するactiveDirectoryLdapAuthenticationProviderドメインアカウントでログインしようとすると、2番目のプロバイダーが認証に失敗するため、ページにアクセスできません。

この目標を達成するために、次のコードを使用しました。

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${ad.domain}")
    private String AD_DOMAIN;

    @Value("${ad.url}")
    private String AD_URL;

    @Autowired
    UserRoleComponent userRoleComponent;

    @Autowired
    DbAuthenticationProvider dbAuthenticationProvider;

    private final Logger logger = LoggerFactory.getLogger(WebSecurityConfig.class);

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        this.logger.info("Verify logging level");
        http.authorizeRequests().anyRequest().fullyAuthenticated().and().formLogin()
                .successHandler(new CustomAuthenticationSuccessHandler()).and().httpBasic().and().logout()
                .logoutUrl("/logout").invalidateHttpSession(true).deleteCookies("JSESSIONID");
        http.formLogin().defaultSuccessUrl("/", true);
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
        auth.authenticationProvider(dbAuthenticationProvider);
    }

    @Bean
    public AuthenticationManager authenticationManager() {
        return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider(), dbAuthenticationProvider));
    }

    @Bean
    public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
        ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(AD_DOMAIN,
                AD_URL);
        provider.setConvertSubErrorCodesToExceptions(true);
        provider.setUseAuthenticationRequestCredentials(true);
        return provider;
    }
}

そして、これは私のDbAuthenticationProviderです。

@Component
public class DbAuthenticationProvider implements AuthenticationProvider {

    Logger logger = LoggerFactory.getLogger(DbAuthenticationProvider.class);

    @Override
    public Authentication authenticate(Authentication auth) throws AuthenticationException {
        auth.setAuthenticated(false);
        this.logger.info("Got initialized");
        return auth;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return true;
    }

}

悲しいことに、ログインできました(予期したとおり、アクセスは拒否されません)。私は何かを逃したのですか?

スタッカー:

Spring AuthenticationProviderはリクエストの認証に複数を使用しないため、オブジェクトをサポートし、リクエストを正常に認証した最初の(内のArrayListAuthenticationProviderAuthentication使用される唯一のものになります。あなたの場合はそれactiveDirectoryLdapAuthenticationProviderです。

を使用する代わりにActiveDirectoryLdapAuthenticationProvider、LDAPに委任して追加のチェックを行うカスタムAuthenticationProviderを使用できます。

    CustomerAuthenticationProvider implements AuthenticationProvider{
        privtae ActiveDirectoryLdapAuthenticationProvider  delegate; // add additional methods to initialize delegate during your configuration

          @Override
         public Authentication authenticate(Authentication auth) throws 
             AuthenticationException {
            Authentication  authentication= delegate.authenticate(auth);
            additionalChecks(authentication);
           return auth;
           }


          @Override
          public boolean supports(Class<?> authentication) {
            return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
          }

        public void additionalCheck(Authentication authentication){
               // throw AuthenticationException when it's not allowed
        }

    }

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Spring Boot + Spring Security:基本認証フォームを抑制する方法

分類Dev

Spring Boot + Spring Boot Security Start Error

分類Dev

CORS problems with spring boot security

分類Dev

Spring Boot SecurityでのNoSuchMethodError

分類Dev

Spring Boot Security2.0で2つの異なる認証方法を使用する方法

分類Dev

What are Spring Security default credentials for Spring Boot?

分類Dev

Spring Boot 2、Spring Security 5、@ WithMockUser

分類Dev

Spring Boot Security:java.lang.StackOverflowError

分類Dev

Spring Boot Security CORS with POST request

分類Dev

Spring Boot Securityを無効にする方法

分類Dev

Spring BootとOAuth 2によるSpring Security

分類Dev

Spring BootのSpring Securityのデフォルト認証情報は何ですか?

分類Dev

Vaadin LoginによるSpring Boot Security

分類Dev

Spring-Boot / Securityを使用したNull @ AuthenticationPrincipal

分類Dev

Spring Boot + Spring Securityのカスタムログインページ

分類Dev

Application does not start with Spring Boot 1.2.1 + Spring Security + Servlet 2.5

分類Dev

Application does not start with Spring Boot 1.2.1 + Spring Security + Servlet 2.5

分類Dev

Migrating from Spring Boot Oauth2 to Spring Security 5

分類Dev

Spring Boot - How to kill current Spring Security session?

分類Dev

Spring Boot, Spring Security and Thymeleaf: Apply CsrfFilter to website with form

分類Dev

Spring Boot RestTempalte Call(Get Data)with Security Handel

分類Dev

Spring Boot Security OAuth Angular Response to preflight request was not pass?

分類Dev

How to invalidate previous sessions of user in spring boot security

分類Dev

Spring Boot Security 403「アクセス拒否」

分類Dev

Spring boot 2.0.3 + Security + Oauth2 autoconfigure

分類Dev

複数の認証によるSpring Security

分類Dev

Spring Boot Security-認証なしで許可

分類Dev

Spring Bootを使用したSpring Security:基本認証とJWTトークン認証を組み合わせる

分類Dev

Spring BootとSpring SecurityでREST APIを保護する方法は?

Related 関連記事

  1. 1

    Spring Boot + Spring Security:基本認証フォームを抑制する方法

  2. 2

    Spring Boot + Spring Boot Security Start Error

  3. 3

    CORS problems with spring boot security

  4. 4

    Spring Boot SecurityでのNoSuchMethodError

  5. 5

    Spring Boot Security2.0で2つの異なる認証方法を使用する方法

  6. 6

    What are Spring Security default credentials for Spring Boot?

  7. 7

    Spring Boot 2、Spring Security 5、@ WithMockUser

  8. 8

    Spring Boot Security:java.lang.StackOverflowError

  9. 9

    Spring Boot Security CORS with POST request

  10. 10

    Spring Boot Securityを無効にする方法

  11. 11

    Spring BootとOAuth 2によるSpring Security

  12. 12

    Spring BootのSpring Securityのデフォルト認証情報は何ですか?

  13. 13

    Vaadin LoginによるSpring Boot Security

  14. 14

    Spring-Boot / Securityを使用したNull @ AuthenticationPrincipal

  15. 15

    Spring Boot + Spring Securityのカスタムログインページ

  16. 16

    Application does not start with Spring Boot 1.2.1 + Spring Security + Servlet 2.5

  17. 17

    Application does not start with Spring Boot 1.2.1 + Spring Security + Servlet 2.5

  18. 18

    Migrating from Spring Boot Oauth2 to Spring Security 5

  19. 19

    Spring Boot - How to kill current Spring Security session?

  20. 20

    Spring Boot, Spring Security and Thymeleaf: Apply CsrfFilter to website with form

  21. 21

    Spring Boot RestTempalte Call(Get Data)with Security Handel

  22. 22

    Spring Boot Security OAuth Angular Response to preflight request was not pass?

  23. 23

    How to invalidate previous sessions of user in spring boot security

  24. 24

    Spring Boot Security 403「アクセス拒否」

  25. 25

    Spring boot 2.0.3 + Security + Oauth2 autoconfigure

  26. 26

    複数の認証によるSpring Security

  27. 27

    Spring Boot Security-認証なしで許可

  28. 28

    Spring Bootを使用したSpring Security:基本認証とJWTトークン認証を組み合わせる

  29. 29

    Spring BootとSpring SecurityでREST APIを保護する方法は?

ホットタグ

アーカイブ