上一篇咱們說到如何在 spring security 中自定義登陸處理邏輯,這一篇咱們來說一下如何自定義登陸成功後的處理邏輯。先來回顧下默認狀況下,登陸成功事後spring security 會幫咱們作些什麼: 未登陸的狀況下,咱們直接訪問應用中的資源,頁面會自動跳轉到登陸頁;當登陸成功後,頁面會自動重定向到我登陸前請求的 url。html
好比:若是咱們想在登陸成功後,響應一個 json 字符串(包括「登陸成功」這樣的提示信息,響應code,以及跳轉的url)給前端。應該怎麼辦?前端
首先複製上一節的項目工程 spring-security-02,重命名爲 Spring-security-03。 maven 依賴不須要改變,以下所示:git
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
定義登陸成功後的處理類 GoAuthenticationSuccessHandlergithub
/** * 自定義 登陸成功 處理類 */ [@Component](https://my.oschina.net/u/3907912) public class GoAuthenticationSuccessHandler implements AuthenticationSuccessHandler { @Autowired private ObjectMapper objectMapper; /** * {"code":200,"message":"操做成功","data":"登陸成功"} * [@param](https://my.oschina.net/u/2303379) request * [@param](https://my.oschina.net/u/2303379) response * [@param](https://my.oschina.net/u/2303379) authentication * @throws IOException * @throws ServletException */ @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { response.setHeader("Content-Type", "application/json;charset=utf-8"); response.getWriter().print(objectMapper.writeValueAsString(CommonResult.success("登陸成功"))); response.getWriter().flush(); } }
在 WebSecurityConfig 配置類中,注入自定義處理類的依賴 :web
@Autowired private GoAuthenticationSuccessHandler successHandler; @Autowired private GoAuthenticationFailureHandler failureHandler;
在 protected void configure(HttpSecurity http) 方法中,追加以下代碼:spring
// 這些是原本就有的 http.formLogin() .loginPage("/loginPage.html")// 自定義登陸頁 .loginProcessingUrl("/form/login")// 自定義登陸 action, 名字隨便起 // 如下是新增的 .successHandler(successHandler)// 自定義登陸成功處理類 .failureHandler(failureHandler);// 自定義登陸失敗處理類
從上面的代碼中咱們能夠看到,新增了兩個配置,在 successHandler 和 failureHandler 方法中分別注入了一個處理類,咱們着重看下 GoAuthenticationSuccessHandler 處理類, 經過重寫 AuthenticationSuccessHandler 的方法,響應了一段json給前端。 而failureHandler 是登陸失敗時作一些處理,在這裏咱們會響應登陸失敗的message給前端。這些響應結果咱們均可以自定義。 你能夠根據實際需求去選擇是否須要自定義這些處理類。json
回顧一下,以前若是用戶沒有登陸直接訪問咱們的應用資源,會自動跳轉到登陸頁,若是登陸成功後,去訪問沒有權限的url,會給咱們一段英文提示,大體意思就是沒有權限。這些咱們仍然是能夠定製的。好比當咱們沒有登陸時,給前端提示「用戶未登陸」,當咱們沒有權限時,提示前端「用戶沒有權限」。app
定義兩個處理類 GoAuthenticationEntryPoint 和 GoAccessDeniedHandlermaven
`ide
/** * 自定義 未登陸 或者 token 失效 處理類 */ @Component public class GoAuthenticationEntryPoint implements AuthenticationEntryPoint { @Autowired private ObjectMapper objectMapper; @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { response.setCharacterEncoding("UTF-8"); response.setContentType("application/json"); response.getWriter().println(objectMapper.writeValueAsString(CommonResult.unauthorized(authException.getMessage()))); response.getWriter().flush(); } } /** * 自定義沒有訪問權限處理類 */ @Component public class GoAccessDeniedHandler implements AccessDeniedHandler { @Autowired private ObjectMapper objectMapper; /** * @param request * @param response * @param e * @throws IOException * @throws ServletException * * @return {"code":403,"message":"沒有相關權限","data":"Access is denied"} */ @Override public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException { response.setHeader("Content-Type", "application/json;charset=utf-8"); response.getWriter().print(objectMapper.writeValueAsString(CommonResult.forbidden(e.getMessage()))); response.getWriter().flush(); } }
`
將自定義的兩個處理類注入到 WebSecurityConfig 類中
` @Autowired private GoAccessDeniedHandler accessDeniedHandler;
@Autowired private GoAuthenticationEntryPoint entryPoint;
`
打開 WebSecurityConfig 配置類,在 configure 方法中追加以下代碼:
@Override protected void configure(HttpSecurity http) throws Exception { // 此處省略一部分代碼 http.exceptionHandling() .accessDeniedHandler(accessDeniedHandler)// 用戶沒有訪問權限處理器 .authenticationEntryPoint(entryPoint);// 用戶沒有登陸處理器 }
上面咱們總共定義了四個處理類,分別做用於如下四種狀況發生以後:
咱們能夠根據實際狀況選擇性去定義這些處理類,根據具體需求去定義處理邏輯。詳細代碼能夠參考 https://github.com/nimo10050/spring-security-sample/tree/master/spring-security-03