public class OddAdminVoter implements AccessDecisionVoter<FilterInvocation> {
static final Pattern PATTERN = Pattern.compile("[0-9]+$");
private final RequestMatcher requiresAuthorizationRequestMatcher;
public OddAdminVoter(RequestMatcher requiresAuthorizationRequestMatcher) {
this.requiresAuthorizationRequestMatcher = requiresAuthorizationRequestMatcher;
}
@Override
public int vote(Authentication authentication, FilterInvocation fi, Collection<ConfigAttribute> attributes) { ... }
private boolean requiresAuthorization(HttpServletRequest request) { ... }
@Override
public boolean supports(ConfigAttribute attribute) { ... }
@Override
public boolean supports(Class<?> clazz) { ...}
}
@Bean
public AccessDecisionManager accessDecisionManager() {
List<AccessDecisionVoter<?>> decisionVoters = new ArrayList<>();
decisionVoters.add(new WebExpressionVoter());
decisionVoters.add(new OddAdminVoter(new AntPathRequestMatcher("/admin")));
return new UnanimousBased(decisionVoters);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
...
.authorizeRequests()
.antMatchers("/me").hasAnyRole("USER", "ADMIN")
.antMatchers("/admin").access("isFullyAuthenticated() and hasRole('ADMIN')")
.anyRequest().permitAll()
.accessDecisionManager(accessDecisionManager())
.and()
...
}
인증 성공 또는 실패가 발생했을 때 관련 이벤트(ApplicationEvent)가 발생한다.
abstract class ApplicationEvent
abstract class AbstractAuthenticationEvent
ApplicationEvent
class AuthenticationSuccessEvent
AbstractAuthenticationEvent
abstract class AbstractAuthenticationFailureEvent
AbstractAuthenticationEvent
모듈간의 결합도를 느슨하게 만들기 위해 사용한다.
AuthenticationEventPublisher
publishAuthenticationSuccess()
: 성공 eventpublishAuthenticationFailure()
: 실패 event@EventListener
어노테이션을 이용하여 리스너 등록한다.