spring security:自定义认证成功处理器
生活随笔
收集整理的這篇文章主要介紹了
spring security:自定义认证成功处理器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用spring認證登錄,登錄之后,一般還需要進行其他處理,例如:保存登錄時間、登錄ip到數據庫,緩存用戶信息到redis數據庫等等,這些操作可以通過自定義一個登錄成功處理器來處理。
自定義認證成功處理器
只需要繼承AuthenticationSucdessHandler即可實現自定義處理器:
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {@Overridepublic void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,Authentication authentication) throws IOException, ServletException {request.getRequestDispatcher("/login").forward(request, response);}}我自定義的處理器很簡單,直接轉發請求到/login,這是因為,我在使用spring security 之前,就已經寫了一個登錄功能,這樣,不用修改代碼就可以運行之前的登錄邏輯了。
當然,spring-security.xml文件也要做小小的修改:
<http><intercept-url pattern="/user/**" access="hasRole('USER')" /><intercept-url pattern="/admin/**" access="hasRole('ADMIN')" /><form-login login-page="/login"login-processing-url="/login"authentication-failure-url="/login?error"username-parameter="phone"password-parameter="password" authentication-success-handler-ref="authSuccess"/><logout invalidate-session="true"logout-url="loginout"logout-success-url="/login"/></http> <!-- 自定義認證成功處理器 --><beans:bean id="authSuccess" class="com.huanle.utils.security.AuthenticationSuccessHandlerImpl"></beans:bean>上面的代碼中,我們定義了一個AuthenticationSuccessHandlerImpl的bean
然后,在<form-login>標簽中添加了authentication-success-handler-ref="authSuccess",刪除了default-target-url屬性,因為自定義處理器之后,default-target-url就失效了。
總結
以上是生活随笔為你收集整理的spring security:自定义认证成功处理器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue-cli webpack配置分析
- 下一篇: Linux网络命令之 `Hping3`