1 CSRF
CSRF是指跨站請求偽造(Cross-site request forgery)斥季,是web常見的攻擊之一剥纷。
spring security防止CSRF攻擊的方式就是通過csrf_token预厌,在后端生成一個csrf_token带饱,前端發(fā)起請求的時候需要攜帶這個csrf_token邻奠,后端會有過濾器進(jìn)行校驗酌儒,如果沒有攜帶或者是偽造的就不允許訪問辜妓。
CSRF攻擊依靠的是cookie中所攜帶的認(rèn)證信息,但是在前后端分離的項目中我們的認(rèn)證信息其實是token忌怎,而token并不存儲在cookie中籍滴,并且需要前端代碼去把token設(shè)置到請求頭中才可以,所以CSRF攻擊問題在前后端分離項目中不存在榴啸。我們只需在配置類中將其禁用即可孽惰。
@Override
protected void configure(HttpSecurity http) throws Exception {
//關(guān)閉csrf
http.csrf().disable()
2 自定義認(rèn)證成功處理器
實際上在UsernamePasswordAuthenticationFilter進(jìn)行登錄認(rèn)證的時候,如果認(rèn)證成功了會調(diào)用AuthenticationSuccessHandler的方法進(jìn)行認(rèn)證成功后的處理AuthenticationSuccessHandler就是登錄成功處理器鸥印,我們也可以自己去自定義成功處理器進(jìn)行成功后的相應(yīng)處理勋功。
@Component
public class OwnSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
System.out.println("認(rèn)證成功了");
}
}
然后在配置文件中進(jìn)行配置
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AuthenticationSuccessHandler successHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin().successHandler(successHandler);
http.authorizeRequests().anyRequest().authenticated();
}
}
3 認(rèn)證失敗處理器
在UsernamePasswordAuthenticationFilter進(jìn)行登錄認(rèn)證的時候,如果認(rèn)證失敗了會調(diào)用AuthenticationFailureHandler的方法進(jìn)行認(rèn)證失敗后的處理
AuthenticationFailureHandler就是登錄失敗處理器库说,我們也可以自己去自定義失敗處理器失敗后的相應(yīng)處理
@Component
public class OwnFailureHandler implements AuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
System.out.println("認(rèn)證失敗了");
}
}
配置文件中進(jìn)行配置
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AuthenticationSuccessHandler successHandler;
@Autowired
private AuthenticationFailureHandler failureHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
// 配置認(rèn)證成功處理器
.successHandler(successHandler)
// 配置認(rèn)證失敗處理器
.failureHandler(failureHandler);
http.authorizeRequests().anyRequest().authenticated();
}
}
4 注銷成功處理器
同理
@Component
public class OwnLogoutSuccessHandler implements LogoutSuccessHandler {
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
System.out.println("注銷成功");
}
}
配置SecurityConfig文件
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AuthenticationSuccessHandler successHandler;
@Autowired
private AuthenticationFailureHandler failureHandler;
@Autowired
private LogoutSuccessHandler logoutSuccessHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
// 配置認(rèn)證成功處理器
.successHandler(successHandler)
// 配置認(rèn)證失敗處理器
.failureHandler(failureHandler);
http.logout()
//配置注銷成功處理器
.logoutSuccessHandler(logoutSuccessHandler);
http.authorizeRequests().anyRequest().authenticated();
}
}