복붙노트

[SPRING] 스프링 보안으로 추가 매개 변수를 확인하십시오.

SPRING

스프링 보안으로 추가 매개 변수를 확인하십시오.

Spring Security에 힌트를 주시면 사용자 로그인 중에 추가 매개 변수를 어떻게 확인할 수 있습니까? 예를 들어 "사용자 이름"과 "비밀번호"뿐만 아니라 전자 메일 링크로 등록을 확인한 경우에도 확인하십시오. 모든 데이터는 DB에 저장되어 있으며 UserDetailsService를 쉽게 구현할 수 있습니다. 그러나 보안 서비스를 추가 매개 변수 "isValidated"에주의를 기울이는 방법은 무엇입니까? 저는 Spring 3.2.0을 사용하고 있습니다.

해결법

  1. ==============================

    1.이를 달성하는 한 가지 방법은 사용자 정의 AuthenticationProvider를 작성하거나 기존 AuthenticationProvider를 확장하는 것입니다. 귀하의 경우에는 예를 들어 DaoAuthenticationProvider를 확장하고 additionalAuthenticationChecks () 메소드에서 계정이 확인되었는지 여부를 확인하는 로직을 입력하는 것으로 충분할 수 있습니다.

    이를 달성하는 한 가지 방법은 사용자 정의 AuthenticationProvider를 작성하거나 기존 AuthenticationProvider를 확장하는 것입니다. 귀하의 경우에는 예를 들어 DaoAuthenticationProvider를 확장하고 additionalAuthenticationChecks () 메소드에서 계정이 확인되었는지 여부를 확인하는 로직을 입력하는 것으로 충분할 수 있습니다.

    다음은 그 예입니다.

    public class CustomAuthenticationProvider extends DaoAuthenticationProvider {
    
        @Override
        protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
            // Perform the checks from the super class
            super.additionalAuthenticationChecks(userDetails, authentication);
    
            // Cast the UserDetails to the implementation you use
            User user = (User) userDetails;
    
            // Check the confirmed status
            if (!user.isAccountConfirmed()) {
                throw new AccountNotConfirmedException("Account is not confirmed yet.");
            }
        }
    
        public static class AccountNotConfirmedException extends AuthenticationException {
            public AccountNotConfirmedException(String message) {
                super(message);
            }
        }
    
    }
    

    귀하의 UserDetails 구현에는 계정 확인 상태에 대한 정보가 포함되어야합니다. UserDetailsService 구현에서이 정보를 매핑 할 수 있습니다.

    편집 : 이제 살펴 보았습니다. 첫 번째 해결 방법은 조금 복잡합니다. 사용자 정의 AuthenticationProvider를 사용하지 않고도이 문제를 쉽게 해결할 수 있습니다. 계정이 확인되지 않으면 UserDetails 구현의 isEnabled ()가 false를 반환하는지 확인하십시오. enabled 속성이 false이면 인증이 허용되지 않습니다 (이것은 Spring Security가 자동으로 처리합니다).

    첫 번째 솔루션은 예를 들어 AuthenticationFailureHandler에서 AccountNotConfirmedException을 명시 적으로 처리하려는 경우 여전히 유용 할 수 있습니다.

  2. from https://stackoverflow.com/questions/27658030/check-extra-parameters-with-spring-security by cc-by-sa and MIT license