Spring/Spring Security

[Spring Security] Username/password authentication : form / basic

공대키메라 2022. 7. 17. 18:24

[Spring Security] Username/password authentication : form / basic

 

지난 시간에는 Authentication 과 Servlet Authentication Architecture에 대해 알아보았다. 

 

(Authentication 과 Servlet Authentication Architecture 여기 😁😁 클릭!)

 

이번 시간에는 인증에서의 username/password 인증에 대해서 알아보겠다.

 

참고한 사이트는 다음과 같다.

 

출처:

https://docs.spring.io/spring-security/reference/servlet/authentication/passwords/input.html

https://docs.spring.io/spring-security/reference/servlet/authentication/passwords/form.html

출처 : 인프런 정수원 강사님의 코어-스프링-시큐리티 중 Http Basic 인증

 

1. 사용자 이름/비밀번호 인증(Username/Password Authentication)

사용자를 인증하기 위한 가장 흔한 방법 중 하나는 username과 password를 검증하는 것이다. 

 

Spring Security는 사용자 이름과 비밀번호를 가지고 인증하기 위한 포괄적인 지원을 한다. 

 

2. Reading Username/password - Form Login

Spring Security는 html form을 통해 제공되는 username과 password에 대한 지원을 제공한다. 

 

Spring Security에서 폼 기반 로그인이 어떻게 작동하는지 알아보자. 

 

https://docs.spring.io/spring-security/reference/_images/servlet/authentication/unpwd/loginurlauthenticationentrypoint.png - Figure 1. Redirecting to the Log In Page

 

1. 사용자(user)가 "/private"  자원에 인증되지않은 요청을 보낸다. 

 

2.  Spring Security의 FilterSecurityInterceptor가 인증되지 않은 요청이 AccessDeniedException을 던져서 거부됬다는것을 나타낸다.

 

3. 사용자가 인증이 안됐기 때문에, ExceptionTralslationFilter가 Start Authentication을 실행하고 설정된 AuthenticationEntryPoint로그인 페이지로 redirect한다. 대부분의 경우에 AuthenticationEntryPoint는 LoginUrlAuthenticationEntryPoint이 인스턴스다.

 

4. 브라우저는 리다이렉트된 로그인 페이지에 요청을 보낸다.

 

5. 어플리케이션 내에서 로그인 페이지를 보여준다.

username과 password가 제출되었을 때, UsernamePasswordAuthenticationFiler가 username과 password를 인증한다. 

 

UsernamePasswordAuthenticationFilter가 AbstractAuthenticationProcessingFilter를 상속한다. 

 

https://docs.spring.io/spring-security/reference/_images/servlet/authentication/unpwd/usernamepasswordauthenticationfilter.png - Figure 2. Authenticating Username and Password

 

 

1. 사용자가 username과 password를 제출할 때, UsernamePasswordAuthenticationFilter가 UsernamePasswordAuthenticationToken을 생성하는데, 이것은 HttpServletRequest로부터 username과 password를 추출해서 만든 인증의 한 형태이다.

 

2. UssernamePasswordAuthenticationToken은 인증되기 위해 AuthenticationManger로 넘겨진다. AuthenticationManger가 어떠헥 보이는지에 대한 상세 정보는 유저 정보가 어떻게 저장되어있는지에 따라 다르다.

 

3. 만약 인증이 실패하면, 다음 로직을 수행한다.

  • SecurityContextHolder내용이 지워진다.
  • RembmerMeServices.loginFail이 호출된다. remember me가 설정되지 않았다면, 아무것도 안한다.
  • AuthenticationFailureHandler가 호출된다.

 

4. 만약 인증이 성공한다면, 다음 로직을 수행한다.

  • SessionAuthenticationStrategy가 새로운 로그인을 알아챈다.
  • Authentication이 SecurityContextHolder에 세팅된다. 
  • RememberMe.loginSuccess가 호출된다. remember me가 설정되지 않았다면, 아무것도 안한다.
  • ApplicationEventPublisher가 InteractiveAutheticationSuccessEvent를 발행한다. 
  • AuthenticaitonSuccessHandler가 호출된다. 전형적으로 SimpleUrlAuthenticationSuccessHandler가 있는데, 이것은 log in 페이지에서 redirect할 때 ExceptionTranslationFilter에 저장된 요청을 redirect한다. 

Spring Security form 로그인은 기본적으로 가능하다. 하지만, 설정 기반의 모든 서블릿이 제공되자마나 폼 기반 로그인은 명확히 제공되어야 한다. 

 

3. Reading Username/password - Basic Login

https://docs.spring.io/spring-security/reference/_images/servlet/authentication/unpwd/basicauthenticationentrypoint.png - Figure 1. Sending WWW-Authenticate Header

 

1. 사용자가 인증되지 않은 자원인 /private에 인증되지않은 요청을 보낸다. 

 

2. Spring Security의 FilterSecurityInterceptor가 AccessDeniendException을 던지면서 인증 되지 않은 요청이 거부되었다는것을 나타낸다.

 

3. 사용자가 인증되지 않았기 때문에, ExceptionTranslationFilter가 인증을 시작한다. 설정된 AuthenticationEntryPoint는 WWW-Authenticate header를 보내는 BasicAuthenticationEntryPoint의 인스턴스이다. RequestCache는 전형적으로 요청을 저장하지 않는 NullRequest인데, client가 요청을 다시 보낼 수 있기 때문이다. (굳이 저장할 필요가 없는 것이다.)

 

client가 WWW-Authenticate heade를 받을 때 username 과 password를 가지고 다시 시도한다는 것을 안다. 

 

https://docs.spring.io/spring-security/reference/_images/servlet/authentication/unpwd/basicauthenticationfilter.png - Figure 2. Authenticating Username and Password

 

1. 사용자가 username과 password를 제출할 때, BasicAuthenticationFilter가 username과 passsword를 HttpServletRequest에서 추출해서 UsernamePasswordAuthenticationToken을 생성한다. 

 

2.  UsernamePasswordAuthenticationToken은 인증받기 위해 AuthenticationManager로 보내진다.  AuthenticationManager이 어떻게 생길지에 대한 상세한 정보는 사용자 정보가 저장되는 방식에 따라 다르다. 

 

3,4는 실패 성공에 따른 호출 순서를 보여준다. 

 

HTTP는 자체적인 인증 관련 기능을 제공하며 HTTP 표준에 정의된 가장 단순한 인증 기법이다

간단한 설정과 Stateless가 장점 - Session Cookie(JSESSIONID) 사용하지 않음

보호자원 접근시 서버가 클라이언트에게  401 Unauthorized 응답과 함께 WWW-Authenticate header를 기술해서 인증요구를 보냄

Client는 ID:Password 값을 Base64로 Encoding한 문자열을 Authorization Header에 추가한 뒤 Server에게 Resource를 요청

Authorization: Basic 
ID, Password가 Base64로 Encoding 되어 있어ID, Password 가 외부에 쉽게 노출되는 구조이기 때문에SSL
이나TLS 는 필수이다

출처 : 인프런 정수원 강사님의 코어-스프링-시큐리티 중 Http Basic 인증 중

이번 시간에는 Username/password authentication 중 Form 방식과 Basic 방식에 대해 알아보았다.

 

다음 시간에는 이어서 Storage Mechanism에 대해 공부할 것이다.