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() // 關(guān)閉 csrf .and() .csrf().disable(); }
以上代碼運(yùn)行之后
無論怎么測(cè)試,successHandler
都無效,只會(huì)返回原來的登錄頁面,
但是,加入自定義登錄接口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() // 關(guān)閉 csrf .and() .csrf().disable(); }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 實(shí)現(xiàn)LZ78壓縮算法的示例代碼
這篇文章主要介紹了Java 實(shí)現(xiàn)LZ78壓縮算法的示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05Spring data elasticsearch使用方法詳解
這篇文章主要介紹了Spring data elasticsearch使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01SpringBoot圖文并茂帶你掌握devtools熱啟動(dòng)
這篇文章主要介紹springBoot插件工具熱部署Devtools,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06Java中實(shí)現(xiàn)文件預(yù)覽的功能(實(shí)例代碼)
大家都知道word,Excel,PPT實(shí)現(xiàn)在線預(yù)覽常用的方式就是先轉(zhuǎn)換成pdf,然后在進(jìn)行預(yù)覽,下面給大家介紹Java中如何實(shí)現(xiàn)文件預(yù)覽的功能,需要的朋友可以參考下2023-05-05Maven依賴管理之parent與dependencyManagement深入分析
首先我們來說說parent標(biāo)簽,其實(shí)這個(gè)不難解釋,就是父的意思,pom也有繼承的。比方說我現(xiàn)在有A,B,C,A是B,C的父級(jí)?,F(xiàn)在就是有一個(gè)情況B,C其實(shí)有很多jar都是共同的,其實(shí)是可以放在父項(xiàng)目里面,這樣,讓B,C都繼承A就方便管理了2022-10-10Java中的Set、List、Map的用法與區(qū)別介紹
這篇文章主要介紹了Java中的Set、List、Map的用法與區(qū)別,需要的朋友可以參考下2016-06-06jxls2.4.5如何動(dòng)態(tài)導(dǎo)出excel表頭與數(shù)據(jù)
這篇文章主要介紹了jxls2.4.5如何動(dòng)態(tài)導(dǎo)出excel表頭與數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08關(guān)于java的九個(gè)預(yù)定義Class對(duì)象
這篇文章主要介紹了關(guān)于java的九個(gè)預(yù)定義Class對(duì)象,在Java中,沒有類就無法做任何事情。然而,并不是所有的類都具有面向?qū)ο筇卣?。如Math.random,并只需要知道方法名和參數(shù),需要的朋友可以參考下2023-05-05