AnonymousAuthenticationFilter

  1. Spring Security AnonymousAuthenticationFilter
    1. AnonymousAuthenticationFilter
    2. AbstractSecurityInterceptor

Spring Security AnonymousAuthenticationFilter

spring-security-filter

출처 : https://www.cnblogs.com/xuwenjin/p/9552303.html

AnonymousAuthenticationFilter 는 익명 사용자 접근 시 사용되는 필터 입니다.

당연히 인증객체에 세션을 저장 하지 않는 특징이 있다.

AnonymousAuthenticationFilter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// AnonymousAuthenticationFilter Class
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

if (SecurityContextHolder.getContext().getAuthentication() == null) {
SecurityContextHolder.getContext().setAuthentication(this.createAuthentication((HttpServletRequest)req));
if (this.logger.isDebugEnabled()) {
this.logger.debug("Populated SecurityContextHolder with anonymous token: '" + SecurityContextHolder.getContext().getAuthentication() + "'");
}
} else if (this.logger.isDebugEnabled()) {
this.logger.debug("SecurityContextHolder not populated with anonymous token, as it already contained: '" + SecurityContextHolder.getContext().getAuthentication() + "'");
}

chain.doFilter(req, res);
}

맨 처음 접속 시 해당 익명 사용자가

1
if (SecurityContextHolder.getContext().getAuthentication() == null) {

이전에 인증 과정을 처리 하지 않았기 때문에 Security Context 안에 인증 객체가 null 이다.

1
2
3
4
5
6
// AnonymousAuthenticationFilter Class
protected Authentication createAuthentication(HttpServletRequest request) {
AnonymousAuthenticationToken auth = new AnonymousAuthenticationToken(this.key, this.principal, this.authorities);
auth.setDetails(this.authenticationDetailsSource.buildDetails(request));
return auth;
}

익명 사용자 전용 인증 Token 객체가 생성 한다.

그런 다음 Security Context 안에 저장 시킨다.

1
chain.doFilter(req, res);

그 이후 다음 필터로 이동 한다.

AbstractSecurityInterceptor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
protected InterceptorStatusToken beforeInvocation(Object object) {
... 생략 ...
if (SecurityContextHolder.getContext().getAuthentication() == null) {
this.credentialsNotFound(this.messages.getMessage("AbstractSecurityInterceptor.authenticationNotFound", "An Authentication object was not found in the SecurityContext"), object, attributes);
}

Authentication authenticated = this.authenticateIfRequired();

try {
this.accessDecisionManager.decide(authenticated, object, attributes);
} catch (AccessDeniedException var7) {
this.publishEvent(new AuthorizationFailureEvent(object, attributes, authenticated, var7));
throw var7;
}

... 생략 ...
}

AbstractSecurityInterceptor 필터는 최종 필터로써 인가 처리를 전담하는 역활을 한다.

1
2
3
if (SecurityContextHolder.getContext().getAuthentication() == null) {
this.credentialsNotFound(this.messages.getMessage("AbstractSecurityInterceptor.authenticationNotFound", "An Authentication object was not found in the SecurityContext"), object, attributes);
}

AnonymousAuthenticationFilter 필터에서 Security Context 안에 인증 객체를 저장 시켰는데

AbstractSecurityInterceptor 필터에서 해당 인증 객체가 존재하는지 체크 한다.

1
this.accessDecisionManager.decide(authenticated, object, attributes);

실질적으로 인가 처리 하는 메소드 이다.


Copyright 2020- syh8088. 무단 전재 및 재배포 금지. 출처 표기 시 인용 가능.

💰

×

Help us with donation