SpringSecurity自定義登錄成功處理
更新時間:2022年09月06日 11:20:39 作者:搞錢自律
這篇文章主要為大家詳細介紹了SpringSecurity自定義登錄成功處理,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
有時候頁面跳轉(zhuǎn)并不能滿足我們,特別是在前后端分離開發(fā)中就不需要成功之后跳轉(zhuǎn)頁面。只需要給前端返回一個JSON通知登錄成功還是失敗與否。這個試試可以通過自定義AuthenticationSuccessHandler實現(xiàn)
修改WebSecurityConfigurer
successHandler
package com.example.config; import com.example.handler.MyAuthenticationSuccessHandler; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { ? ? @Override ? ? public void configure(HttpSecurity http) throws Exception { ? ? ? ? //【注意事項】放行資源要放在前面,認證的放在后面 ? ? ? ? http.authorizeRequests() ? ? ? ? ? ? ? ? .mvcMatchers("/index").permitAll() //代表放行index的所有請求 ? ? ? ? ? ? ? ? .mvcMatchers("/loginHtml").permitAll() //放行l(wèi)oginHtml請求 ? ? ? ? ? ? ? ? .anyRequest().authenticated()//代表其他請求需要認證 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .formLogin()//表示其他需要認證的請求通過表單認證 ? ? ? ? ? ? ? ? //loginPage 一旦你自定義了這個登錄頁面,那你必須要明確告訴SpringSecurity日后哪個url處理你的登錄請求 ? ? ? ? ? ? ? ? .loginPage("/loginHtml")//用來指定自定義登錄界面,不使用SpringSecurity默認登錄界面 ?注意:一旦自定義登錄頁面,必須指定登錄url ? ? ? ? ? ? ? ? //loginProcessingUrl ?這個doLogin請求本身是沒有的,因為我們只需要明確告訴SpringSecurity,日后只要前端發(fā)起的是一個doLogin這樣的請求, ? ? ? ? ? ? ? ? //那SpringSecurity應該把你username和password給捕獲到 ? ? ? ? ? ? ? ? .loginProcessingUrl("/doLogin")//指定處理登錄的請求url ? ? ? ? ? ? ? ? .usernameParameter("uname") //指定登錄界面用戶名文本框的name值,如果沒有指定,默認屬性名必須為username ? ? ? ? ? ? ? ? .passwordParameter("passwd")//指定登錄界面密碼密碼框的name值,如果沒有指定,默認屬性名必須為password // ? ? ? ? ? ? ? ?.successForwardUrl("/index")//認證成功 forward 跳轉(zhuǎn)路徑,forward代表服務器內(nèi)部的跳轉(zhuǎn)之后,地址欄不變 始終在認證成功之后跳轉(zhuǎn)到指定請求 // ? ? ? ? ? ? ? ?.defaultSuccessUrl("/index")//認證成功 之后跳轉(zhuǎn),重定向 redirect 跳轉(zhuǎn)后,地址會發(fā)生改變 ?根據(jù)上一保存請求進行成功跳轉(zhuǎn) ? ? ? ? ? ? ? ? .successHandler(new MyAuthenticationSuccessHandler()) //認證成功時處理 ?前后端分離解決方案 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .csrf().disable(); //禁止csrf 跨站請求保護 ? ? } }
新增處理成功handler
package com.example.handler; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.security.core.Authentication; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** ?* 自定義認證成功之后處理 ?*/ public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler { ? ? @Override ? ? public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { ? ? ? ? Map<String,Object> result = new HashMap<>(); ? ? ? ? result.put("msg","登錄成功"); ? ? ? ? result.put("status",200); ? ? ? ? result.put("authentication",authentication); ? ? ? ? response.setContentType("application/json;charset=UTF-8"); ? ? ? ? String s = new ObjectMapper().writeValueAsString(result); ? ? ? ? response.getWriter().println(s); ? ? } }
啟動成功,測試
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。