spring security登錄成功后跳轉(zhuǎn)回登錄前的頁面
spring security登錄成功后跳轉(zhuǎn)回登錄前的頁面
我剛好碰到了這么一個(gè)需求,正好自己也剛開始學(xué)spring security,但是我百度了一下,發(fā)現(xiàn)都講的好麻煩,其實(shí)大概了解完之后,親自實(shí)踐一下發(fā)現(xiàn),操作非常簡(jiǎn)單。
需求如下
在未登錄的情況下訪問某些頁面被攔截,跳轉(zhuǎn)到登錄頁面,然后現(xiàn)在需要登錄成功之后跳轉(zhuǎn)到登錄之前的頁面。
要解決這個(gè)問題,就需要明白一點(diǎn),就是我被攔截前的請(qǐng)求去哪里了?
答案是有個(gè)requestCache的東西保存了你的這些信息,那么知道了這一點(diǎn),后面的東西就簡(jiǎn)單了,登錄成功之后的處理從這東西里面把之前的請(qǐng)求取出來就好了。
代碼如下
.successHandler(new AuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
response.setContentType("application/json;charset=utf-8");
RequestCache cache = new HttpSessionRequestCache();
SavedRequest savedRequest = cache.getRequest(request, response);
String url = savedRequest.getRedirectUrl();
response.sendRedirect(url);
}
})
Springsecurity 配置文件和登錄跳轉(zhuǎn)
好久沒碰springsecurity了,最近在做一個(gè)小東西的時(shí)候,部署遇到了跳轉(zhuǎn)問題,所以寫篇文章記錄一下。
項(xiàng)目的其中一個(gè)需求是登錄,賬號(hào)也是固定的,所以直接在配置文件中處理。springsecurity具體的配置網(wǎng)上一大堆,這邊就不展開說了。
項(xiàng)目結(jié)構(gòu)

直接上springsecurity配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> <!--配置不用攔截的資源--> <http pattern="/*.html" security="none"></http> <http pattern="/css/**" security="none"></http> <http pattern="/img/**" security="none"></http> <http pattern="/js/**" security="none"></http> <http pattern="/plugins/**" security="none"></http> <!-- 頁面的攔截規(guī)則 --> <http use-expressions="false"> <intercept-url pattern="/**" access="ROLE_ADMIN"/> <form-login login-page="/login.html" default-target-url="/admin/index_user.html" authentication-failure-url="/error_page.html" /> <csrf disabled="true"/> <headers> <frame-options policy="SAMEORIGIN"/> </headers> <logout logout-url="/logout"/> </http> <!--配置登錄的賬號(hào)和密碼--> <authentication-manager> <authentication-provider> <user-service> <user name="admin" password="123456" authorities="ROLE_ADMIN"/> </user-service> </authentication-provider> </authentication-manager> </beans:beans>
form-login標(biāo)簽用來配置自定義的登錄頁面,項(xiàng)目里配置的是login.html,default-target-url是用來配置登錄成功以后,需要跳轉(zhuǎn)的頁面,這里配置的是/admin/index_user.html,因?yàn)閕ndex_user.html在webapp下的admin目錄里面。
自定義的登錄頁面login.html上需要加form標(biāo)簽登錄框
如下:
<form class="sui-form" action="/login" method="post" id="loginform"> <div class="input-prepend"><span class="add-on loginname"></span> <input id="prependedInput" type="text" name="username" placeholder="用戶名" class="span2 input-xfat"> </div> <div class="input-prepend"><span class="add-on loginpwd"></span> <input id="prependedInput" type="password" name="password" placeholder="密碼" class="span2 input-xfat"> </div> <div class="logined"> <a class="sui-btn btn-block btn-xlarge btn-danger" onclick="doucment:loginform.submit()" target="_blank">登 錄</a> </div> </form>
完成。
eclipse調(diào)試成功后,導(dǎo)出war包,部署到tomcat里面。
瀏覽器輸入 http://localhost:8080/investigation/,直接跳轉(zhuǎn)到登錄頁面

一切正常。然后在下面的登錄框中輸入賬號(hào):admin,密碼:123456,點(diǎn)擊登錄,頁面就顯示404,提示為找不到/login。

查了半天,發(fā)現(xiàn)在登錄頁login.html里面form的action屬性里添加項(xiàng)目路徑investigation,然后重新運(yùn)行服務(wù)器,就能成功跳轉(zhuǎn)。
具體修改如下
<form class="sui-form" action="/investigation/login" method="post" id="loginform"><!--此處action需要加上工程的路徑--> <div class="input-prepend"><span class="add-on loginname"></span> <input id="prependedInput" type="text" name="username" placeholder="用戶名" class="span2 input-xfat"> </div> <div class="input-prepend"><span class="add-on loginpwd"></span> <input id="prependedInput" type="password" name="password" placeholder="密碼" class="span2 input-xfat"> </div> <div class="logined"> <a class="sui-btn btn-block btn-xlarge btn-danger" onclick="doucment:loginform.submit()" target="_blank">登 錄</a> </div> </form>
在真正部署的時(shí)候,修改tomcat的配置文件里面默認(rèn)的訪問路徑即可,不需要在上面的action里面改。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis源碼解析——獲取SqlSessionFactory方式
這篇文章主要介紹了MyBatis源碼解析——獲取SqlSessionFactory方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
java int轉(zhuǎn)byte和long轉(zhuǎn)byte的方法
下面小編就為大家?guī)硪黄猨ava int轉(zhuǎn)byte和long轉(zhuǎn)byte的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
Spring?+?ECharts實(shí)現(xiàn)數(shù)據(jù)可視化的案例詳解
Apache?ECharts是一個(gè)基于?JavaScript?的開源可視化圖表庫,在網(wǎng)頁上實(shí)現(xiàn)數(shù)據(jù)的可視化,非常好用,本文將通過一個(gè)簡(jiǎn)單的demo來給大家介紹一下Spring?+?ECharts如何數(shù)據(jù)可視化,需要的朋友可以參考下2023-07-07
解決idea2020 maven無法自動(dòng)導(dǎo)包的問題
這篇文章主要介紹了解決idea2020 maven無法自動(dòng)導(dǎo)包的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Mybatis新增數(shù)據(jù)并返回主鍵id的兩種方法實(shí)現(xiàn)
本文主要介紹了Mybatis新增數(shù)據(jù)并返回主鍵id的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02

