Spring
Spring Security: AnonymousAuthenticationFilter
S2채닝S2
2023. 4. 6. 12:04
AnonymousAuthenticationFilter
- 해당 필터에 요청이 도달할때까지 사용자가 인증되지 않았다면, 사용자를 null 대신 Anonymous 인증 타입으로 표현
- 사용자가 null 인지 확인하는것보다 어떤 구체적인 타입으로 확인할수 있도록 함
- index.html : thymeleaf 이용.
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset="UTF-8"/>
<meta content="no-cache"/>
<title th:text="#{html.title}"></title>
<link rel="shortcut icon" type="image/x-icon" th:href="@{/assets/icons/favi.png}">
</head>
<body>
index page<br>
<span sec:authentication="name"></span> 님 반갑습니다.
<span th:text="${#authentication.getAuthorities()}"></span>
</body>
</html>
- 인증 없이 index 페이지 접근 시 '익명 사용자'라는 아래와 같은 문구를 확인할 수 있다.
AnonymousAuthenticationFilter 설정
- Spring security 설정 클래스: WebSecurityConfigurerAdapter를 상속
@Configuraion
@EnableWebSecurity
public class WebSecurityConfigure extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.anonymous()
.principal("thisIsAnonymousUser") //anonymous user 이름
.authorities("ROLE_ANONYMOUS", "ROLE_UNKNOWN") // role
}
}
- 서버를 띄우고 index에 접근하면 설정한 username, role을 확인할 수 있다.
AnonymousAuthenticaionFilter를 설정할 수는 있지만 굳이 사용하진 않는다고 한다...