티스토리 뷰

728x90

Authentication

Authentication이란 인증 주체이다. 당신이 누구인지 증명하는 것!

  • 사용자의 인증정보를 저장하는 토큰 개념
  • 인증 시 id와 password를 담고 인증 검증을 위해 전달되어 사용된다.
  • 인증 후 최종 인증 결과 (user 객체, 권한정보)를 담고 SecurityContext에 저장되어 전역적으로 참조가 가능)
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication()

Authentication 객체의 구조

1) principal: 사용자 ID 혹은 User 객체를 저장
2) credentials: 사용자 비밀번호
3) authorities: 인증된 사용자의 권한 목록
4) details: 인증 부가 정보
5) Authenticated: 인증 여부

 

1. 사용자가 로그인을 시도 (username 과 password를 전달)

2. UsernamePasswordAuthenticationFilter 인증 필터가 인증정보를 받아서 Authentication 객체를 생성해서 id와 password를 저장

3. AuthenticationManager가 인증 객체를 가지고 인증처리를 한다.

4. 인증에 실패하게되면 예외가 발생 => 후속 예외처리

5. 인증이 성공하게되면 AuthenticationManager를 구현한 객체(ProviderManager)가  Authentication 를 만들어서 최종 인증 결과를 저장한다. 위의 Authentication 와 같은 객체다.

6. SecurityContextHolder 객체 안에 SecurityContext에 저장한다.

 

UsernamePasswordAuthenticationFilter

public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

	public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "username";

	public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "password";

	private static final AntPathRequestMatcher DEFAULT_ANT_PATH_REQUEST_MATCHER = new AntPathRequestMatcher("/login",
			"POST");

	private String usernameParameter = SPRING_SECURITY_FORM_USERNAME_KEY;

	private String passwordParameter = SPRING_SECURITY_FORM_PASSWORD_KEY;

	private boolean postOnly = true;

	public UsernamePasswordAuthenticationFilter() {
		super(DEFAULT_ANT_PATH_REQUEST_MATCHER);
	}

	public UsernamePasswordAuthenticationFilter(AuthenticationManager authenticationManager) {
		super(DEFAULT_ANT_PATH_REQUEST_MATCHER, authenticationManager);
	}

	@Override
	public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
			throws AuthenticationException {
		if (this.postOnly && !request.getMethod().equals("POST")) {
			throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
		}
		String username = obtainUsername(request);
		username = (username != null) ? username.trim() : "";
		String password = obtainPassword(request);
		password = (password != null) ? password : "";
		UsernamePasswordAuthenticationToken authRequest = UsernamePasswordAuthenticationToken.unauthenticated(username,
				password);
		// Allow subclasses to set the "details" property
		setDetails(request, authRequest);
		return this.getAuthenticationManager().authenticate(authRequest);
	}

	...

}

참고

https://www.inflearn.com/course/%EC%BD%94%EC%96%B4-%EC%8A%A4%ED%94%84%EB%A7%81-%EC%8B%9C%ED%81%90%EB%A6%AC%ED%8B%B0

 

스프링 시큐리티 - Spring Boot 기반으로 개발하는 Spring Security - 인프런 | 강의

초급에서 중.고급에 이르기까지 스프링 시큐리티의 기본 개념부터 API 사용법과 내부 아키텍처를 학습하게 되고 이를 바탕으로 실전 프로젝트를 완성해 나감으로써 스프링 시큐리티의 인증과

www.inflearn.com

 

728x90
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함