[ODOP] 75 일차 - 익명 사용자 인증 처리 필터
![[ODOP] 75 일차 - 익명 사용자 인증 처리 필터](/content/images/size/w1200/2023/08/-----------2023-08-01------10.29.26.png)
💡
최신 버전에서는 아래의 방식과는 조금 다르게 권한설정을 진행한다.
아래의 설명들은 모두 구버전(2.6.7)을 기준으로 설명하고 있다
아래의 설명들은 모두 구버전(2.6.7)을 기준으로 설명하고 있다
![](https://blog.pollra.com/content/images/2023/08/-----------2023-08-11------8.44.25.png)
- 익명 사용자와 인증 사용자를 구분해서 처리하기 위한 용도로 사용
- 화면에서 인증 여부를 구현할 때 isAnonymous() 와 isAuthenticated() 로 구분해서 사용
- 인증객체를 세션에 저장하지 않는다
AnonymousAuthenticationFilter
익명 사용자를 인증 처리하기 위한 필터이다
SpringSecurity 에서는 Authentication 데이터가 null 일 경우 Exception 을 발생
따라서 익명 사용자를 허용하는 인증 체계일 경우 AnonymousAuthenticationFilter 를 사용해야 한다
![](https://blog.pollra.com/content/images/2023/08/-----------2023-08-11------8.45.24.png)
내부의 doFilter 에서 실제 로직이 돌아간다
최초 /
경로에 접근하는 사용자는 인증 정보가 없기 때문에 SecurityContext 에는 인증 정보가 없다
따라서 아래의 if 분기를 타게 된다
if (SecurityContextHolder.getContext().getAuthentication() == null) {
SecurityContextHolder.getContext().setAuthentication(createAuthentication((HttpServletRequest) req));
if (this.logger.isTraceEnabled()) {
this.logger.trace(LogMessage.of(() -> "Set SecurityContextHolder to "
+ SecurityContextHolder.getContext().getAuthentication()));
} else {
this.logger.debug("Set SecurityContextHolder to anonymous SecurityContext");
}
}
잘 보면 setAuthentication 으로 인증 정보를 입력 해 주는 것을 볼 수 있는데, 기본 권한 설정인 Anonymous 유저는 /
경로에 접근할 권한이 없기 때문에 /
페이지에 접근할 수 없다. 따라서 접근 할 수 있게 허용해야 한다.
AnonymousAuthenticationToken
익명 사용자를 인증 처리하기 위한 인증 객체
실제 인증을 처리한 사용자는 아니기에 세션에 저장되지는 않는다.
최종 처리
AnonymousAuthenticationToken 을 만들게 되면 익명 사용자를 위한 Authentication 객체도 함께 만들게 되는데 이는 SecurityContext 에 저장된다.
물론 SecurityContext 에 저장되기 때문에 Role(권한) 정보 역시 포함 되어 있다