在使用Spring Security框架過程當中,常常會有這樣的需求,即在登陸驗證時,附帶增長額外的數據,如驗證碼、用戶類型等。下面將介紹如何實現。css
注:個人工程是在Spring Boot框架基礎上的,使用xml方式配置的話請讀者自行研究吧。html
該類提供了獲取用戶登陸時攜帶的額外信息的功能,默認實現WebAuthenticationDetails提供了remoteAddress與sessionId信息。開發者能夠經過Authentication的getDetails()獲取WebAuthenticationDetails。咱們編寫自定義類CustomWebAuthenticationDetails繼承自WebAuthenticationDetails,添加咱們關心的數據(如下是一個token字段)。java
package com.cgs.courses.service; import javax.servlet.http.HttpServletRequest; import org.springframework.security.web.authentication.WebAuthenticationDetails; public class CustomWebAuthenticationDetails extends WebAuthenticationDetails { /** * */ private static final long serialVersionUID = 6975601077710753878L; private final String token; public CustomWebAuthenticationDetails(HttpServletRequest request) { super(request); token = request.getParameter("token"); } public String getToken() { return token; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(super.toString()).append("; Token: ").append(this.getToken()); return sb.toString(); } }
注:在登陸頁面,可將token字段放在form表單中,也能夠直接加在url的參數中,進而把額外數據發送給後臺。web
該接口用於在Spring Security登陸過程當中對用戶的登陸信息的詳細信息進行填充,默認實現是WebAuthenticationDetailsSource,生成上面的默認實現WebAuthenticationDetails。咱們編寫類實現AuthenticationDetailsSource,用於生成上面自定義的CustomWebAuthenticationDetails。spring
package com.cgs.courses.service;
import javax.servlet.http.HttpServletRequest;
import org.springframework.security.authentication.AuthenticationDetailsSource;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.stereotype.Component;
@Component
public class CustomAuthenticationDetailsSource implements AuthenticationDetailsSource<HttpServletRequest, WebAuthenticationDetails> {
@Override
public WebAuthenticationDetails buildDetails(HttpServletRequest context) {
return new CustomWebAuthenticationDetails(context);
}
}
只要看這一句.formLogin().authenticationDetailsSource(authenticationDetailsSource)session
@Autowired private AuthenticationDetailsSource<HttpServletRequest, WebAuthenticationDetails> authenticationDetailsSource; protected void configure(HttpSecurity http) throws Exception { http .headers() .cacheControl() .contentTypeOptions() .httpStrictTransportSecurity() .xssProtection() .and() .authorizeRequests() .antMatchers( "/css/**", "/js/**") .permitAll() .antMatchers("/**") .authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .defaultSuccessUrl("/todo.html", true) .authenticationDetailsSource(authenticationDetailsSource) .and() .logout() .logoutUrl("/logout") .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) .logoutSuccessUrl("/login") .and() .csrf().disable(); }
AuthenticationProvider提供登陸驗證處理邏輯,咱們實現該接口編寫本身的驗證邏輯。app
package com.cgs.courses.service; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.stereotype.Component; @Component public class CustomAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { CustomWebAuthenticationDetails details = (CustomWebAuthenticationDetails) authentication.getDetails(); // 如上面的介紹,這裏經過authentication.getDetails()獲取詳細信息 // System.out.println(details); details.getRemoteAddress(); details.getSessionId(); details.getToken();
// 下面是驗證邏輯,驗證經過則返回UsernamePasswordAuthenticationToken,
// 不然,可直接拋出錯誤(AuthenticationException的子類,在登陸驗證不經過重定向至登陸頁時可經過session.SPRING_SECURITY_LAST_EXCEPTION.message獲取具體錯誤提示信息)
if (驗證經過) {
return UsernamePasswordAuthenticationToken(省略參數);
} else {
throw new AuthenticationException的子類("你要顯示的錯誤信息")
} } @Override public boolean supports(Class<?> authentication) { return authentication.equals(UsernamePasswordAuthenticationToken.class); } }
@Autowired
private AuthenticationProvider authenticationProvider;
@Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(authenticationProvider); }