默认AuthenticationFailureHandler源码
- org.springframework.security.config.annotation.web.configurers.AbstractAuthenticationFilterConfigurer
protected final void updateAuthenticationDefaults() {
if (loginProcessingUrl == null) {
loginProcessingUrl(loginPage);
}
if (failureHandler == null) {
failureUrl(loginPage + "?error");
}
final LogoutConfigurer<B> logoutConfigurer = getBuilder().getConfigurer(
LogoutConfigurer.class);
if (logoutConfigurer != null && !logoutConfigurer.isCustomLogoutSuccess()) {
logoutConfigurer.logoutSuccessUrl(loginPage + "?logout");
}
}
public final T failureUrl(String authenticationFailureUrl) {
T result = failureHandler(new SimpleUrlAuthenticationFailureHandler(
authenticationFailureUrl));
this.failureUrl = authenticationFailureUrl;
return result;
}
protected final void updateAccessDefaults(B http) {
if (permitAll) {
PermitAllSupport.permitAll(http, loginPage, loginProcessingUrl, failureUrl);
}
}
- org.springframework.security.config.annotation.web.configurers.PermitAllSupport
public static void permitAll(
HttpSecurityBuilder<? extends HttpSecurityBuilder<?>> http, String... urls) {
for (String url : urls) {
if (url != null) {
permitAll(http, new ExactUrlRequestMatcher(url));
}
}
}
private final static class ExactUrlRequestMatcher implements RequestMatcher {
private String processUrl;
private ExactUrlRequestMatcher(String processUrl) {
this.processUrl = processUrl;
}
public boolean matches(HttpServletRequest request) {
String uri = request.getRequestURI();
String query = request.getQueryString();
if (query != null) {
uri += "?" + query;
}
if ("".equals(request.getContextPath())) {
return uri.equals(processUrl);
}
return uri.equals(request.getContextPath() + processUrl);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("ExactUrl [processUrl='").append(processUrl).append("']");
return sb.toString();
}
}
使用自定义AuthenticationFailureHandler
- org.springframework.security.config.annotation.web.configurers.AbstractAuthenticationFilterConfigurer
public final T failureHandler(
AuthenticationFailureHandler authenticationFailureHandler) {
this.failureUrl = null;
this.failureHandler = authenticationFailureHandler;
return getSelf();
}
总结
- 如果使用自定义AuthenticationFailureHandler继承SimpleUrlAuthenticationFailureHandler或进行类似的页面跳转,
需在WebSecurityConfigurerAdapter中额外配置失败跳转url的访问控制规则
http.authorizeRequests().antMatchers("/login?error").permitAll()