spring security登錄成功后跳轉(zhuǎn)回登錄前的頁面
spring security登錄成功后跳轉(zhuǎn)回登錄前的頁面
我剛好碰到了這么一個需求,正好自己也剛開始學(xué)spring security,但是我百度了一下,發(fā)現(xiàn)都講的好麻煩,其實(shí)大概了解完之后,親自實(shí)踐一下發(fā)現(xiàn),操作非常簡單。
需求如下
在未登錄的情況下訪問某些頁面被攔截,跳轉(zhuǎn)到登錄頁面,然后現(xiàn)在需要登錄成功之后跳轉(zhuǎn)到登錄之前的頁面。
要解決這個問題,就需要明白一點(diǎn),就是我被攔截前的請求去哪里了?
答案是有個requestCache的東西保存了你的這些信息,那么知道了這一點(diǎn),后面的東西就簡單了,登錄成功之后的處理從這東西里面把之前的請求取出來就好了。
代碼如下
.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了,最近在做一個小東西的時候,部署遇到了跳轉(zhuǎn)問題,所以寫篇文章記錄一下。
項(xiàng)目的其中一個需求是登錄,賬號也是固定的,所以直接在配置文件中處理。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> <!--配置登錄的賬號和密碼--> <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)到登錄頁面
一切正常。然后在下面的登錄框中輸入賬號: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>
在真正部署的時候,修改tomcat的配置文件里面默認(rèn)的訪問路徑即可,不需要在上面的action里面改。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用IntelliJ IDEA 2017.2.5 x64中的Spring Initializr插件快速創(chuàng)建Spring
這篇文章主要介紹了使用IntelliJ IDEA 2017.2.5 x64中的Spring Initializr插件快速創(chuàng)建Spring Boot/Cloud工程(圖解),需要的朋友可以參考下2018-01-01java爬蟲jsoup解析HTML的工具學(xué)習(xí)
jsoup是一個解析HTML的第三方j(luò)ava庫,它提供了一套非常方便的API,可使用DOM,CSS以及類jQuery的操作方法來取出和操作數(shù)據(jù),本文就來開始jsoup的使用學(xué)習(xí)2022-07-07spring cloud gateway 限流的實(shí)現(xiàn)與原理
這篇文章主要介紹了spring cloud gateway 限流的實(shí)現(xiàn)與原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12Java設(shè)計(jì)模式之單例設(shè)計(jì)模式解析
這篇文章主要介紹了Java設(shè)計(jì)模式之單例設(shè)計(jì)模式解析,設(shè)計(jì)模式是在大量的實(shí)踐中總結(jié)和理論化之后優(yōu)選的代碼結(jié)構(gòu)、編程風(fēng)格、以及解決問題的思考方式,設(shè)計(jì)模式免去我們自己再思考和摸索,需要的朋友可以參考下2023-11-11Java圖片與二進(jìn)制相互轉(zhuǎn)換實(shí)現(xiàn)示例講解
這篇文章主要介紹了Java圖片與二進(jìn)制相互轉(zhuǎn)換實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03Scala 操作Redis使用連接池工具類RedisUtil
這篇文章主要介紹了Scala 操作Redis使用連接池工具類RedisUtil,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06基于java springboot + mybatis實(shí)現(xiàn)電影售票管理系統(tǒng)
這篇文章主要介紹了基于java springboot + mybatis實(shí)現(xiàn)的完整電影售票管理系統(tǒng)基于java springboot + mybatis,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08