Spring Security中successHandler無效問題及解決
更新時間:2024年08月01日 15:31:02 作者:一支萬寶路
這篇文章主要介紹了Spring Security中successHandler無效問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Spring Security中successHandler無效
原先代碼
@Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() // 自定義登錄頁面 .and() .formLogin() .loginPage("/login.html") .successHandler((req, resp, auth) -> { Object principal = auth.getPrincipal(); resp.setContentType("application/json;charset=utf-8"); PrintWriter out = resp.getWriter(); out.write(new ObjectMapper().writeValueAsString(principal)); out.flush(); out.close(); }) .permitAll() // 關閉 csrf .and() .csrf().disable(); }
以上代碼運行之后
無論怎么測試,successHandler
都無效,只會返回原來的登錄頁面,
但是,加入自定義登錄接口url
之后,successHandler
又生效:
@Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() // 自定義登錄頁面 .and() .formLogin() .loginPage("/login.html") // 自定義登錄接口 .loginProcessingUrl("/doLogin") .successHandler((req, resp, auth) -> { Object principal = auth.getPrincipal(); resp.setContentType("application/json;charset=utf-8"); PrintWriter out = resp.getWriter(); out.write(new ObjectMapper().writeValueAsString(principal)); out.flush(); out.close(); }) .permitAll() // 關閉 csrf .and() .csrf().disable(); }
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring data elasticsearch使用方法詳解
這篇文章主要介紹了Spring data elasticsearch使用方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01Maven依賴管理之parent與dependencyManagement深入分析
首先我們來說說parent標簽,其實這個不難解釋,就是父的意思,pom也有繼承的。比方說我現(xiàn)在有A,B,C,A是B,C的父級。現(xiàn)在就是有一個情況B,C其實有很多jar都是共同的,其實是可以放在父項目里面,這樣,讓B,C都繼承A就方便管理了2022-10-10Java中的Set、List、Map的用法與區(qū)別介紹
這篇文章主要介紹了Java中的Set、List、Map的用法與區(qū)別,需要的朋友可以參考下2016-06-06jxls2.4.5如何動態(tài)導出excel表頭與數(shù)據(jù)
這篇文章主要介紹了jxls2.4.5如何動態(tài)導出excel表頭與數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08