본문 바로가기
Java/spring

spring security를 알아보자 - 8

by 티코딩 2024. 3. 29.

저번까진 spring security에서 제공하는 비밀번호 인코딩 해싱에 대해 봤다.

이번엔 Authentication Provider에 대해 자세히 아라보자.

 

ㅇ Authentication Provider의 역할

사용자가 제공한 인증정보를 기반으로 사용자의 인증을 수행하는 역할을 함. 인증정보가 유효한지 검증하고, 해당 사용자에 대한 인증을 완료하는 과정을 담당함. Authentication 객체를 검사해 해당 사용자가 시스템에 접근할 권한이 있는지 확인하고, 사용자의 상세정보를 로드하고, 인증결과를 새로 Authentication 객체로 생성한다. 

 

ㅇ Authentication Provider의 종류

DaoAuthenticationProvider: 가장 기본적인 AuthenticationProvider로, UserDetailsService를 사용해 데이터베이스나 다른 영속성 메커니즘에서 사용자의 상세정보를 로드함. 로드된 사용자의정보와 사용자가 제공한 인증정보를 비교해서 인증과정을 수행함.

LdapAuthenticationProvider: LDAP(Lightweight Directory Access Protocol) 서버를 사용하여 인증을 수행함. UserDetailsService와 유사하게 LDAP 서버에서 사용자 정보를 조회하고, 해당 정보를 기반으로 인증을 진행한다.

InMemoryUserDetailsManager: 주로 테스트 또는 개발 목적으로 사용되며, 메모리 상에 사용자 정보를 저장함. 실제 운영 환경에서는 사용될 일이 거의 없으며, 간단한 인증 과정을 위해 사용된다.

JdbcDaoImpl: DataSource를 사용하여 JDBC를 통해 데이터베이스에서 사용자 정보를 조회함. DaoAuthenticationProvider와 함께 사용되며, 실제 데이터베이스에 저장된 사용자 정보를 기반으로 인증을 수행한다.

RememberMeAuthenticationProvider: "기억하기" 인증을 처리함. 사용자가 "기억하기" 옵션을 선택했을 때 발행된 토큰을 검증하여 사용자를 인증한다.

AnonymousAuthenticationProvider: 익명 사용자에 대한 인증을 처리함. 일반적으로 로그인하지 않은 사용자에게 부여되는 익명의 인증 토큰을 관리한다..

PreAuthenticatedAuthenticationProvider: 사전 인증 시나리오를 처리함. 이는 사용자가 외부 시스템(예: SSO 시스템)을 통해 이미 인증된 경우에 사용되며, Spring Security는 사전 인증된 사용자 정보를 기반으로 인증 과정을 수행한다.

JwtAuthenticationProvider: JWT(Json Web Token)를 사용하여 인증을 수행함. JWT 토큰을 검증하고 해당 토큰에 포함된 클레임을 기반으로 사용자를 인증함. 이 인증 제공자는 직접적으로 Spring Security에 포함되어 있지 않으나, JWT를 사용하는 인증 방식을 구현하기 위해 자주 사용된다.

 

 

ㅇ 커스텀 AuthenticationProvider

요구사항은 프로젝트별로 다 다르다. 그래서 우리만의 AuthenticationProvider를 만들어야 한다. 인증논리를 우리가 직접 작성해야한다.

요구사항이 다양하다면 여러가지 AuthenticationProvider를 사용할수도있다. 

 

ㅁ AuthenticationProvider인터페이스의 메서드

두개의 메서드가 있다.

1. Authenticate : authentication 객체를 파라미터로 받아 검증한다. 인증이되면 사용자의 상세정보를 담아 Authentication 객체를 반환한다.

2. supports : 현재 AuthenticationProvider가 Authentication 객체를 인증할수 있는지의 여부를 판단한다.

 

ㅁ 커스텀 메서드

@Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String username = authentication.getName();
        String pwd = authentication.getCredentials().toString();
        List<Customer> customer = customerRepository.findByEmail(username);
        if (customer.size() > 0) {
            if (passwordEncoder.matches(pwd, customer.get(0).getPwd())) {
                List<GrantedAuthority> authorities = new ArrayList<>();
                authorities.add(new SimpleGrantedAuthority(customer.get(0).getRole()));
                return new UsernamePasswordAuthenticationToken(username, pwd, authorities);
            } else {
                throw new BadCredentialsException("Invalid password!");
            }
        }else {
            throw new BadCredentialsException("No user registered with this details!");
        }
    }

 

'Java > spring' 카테고리의 다른 글

spring security를 알아보자 - 10  (0) 2024.04.15
spring security를 알아보자 - 9  (1) 2024.04.02
spring security를 알아보자 - 7  (0) 2024.03.26
spring security를 알아보자 - 6  (0) 2024.03.25
spring security를 알아보자 - 5  (0) 2024.03.21