亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

spring security登錄成功后跳轉回登錄前的頁面

 更新時間:2021年09月16日 14:18:15   作者:qq_29914229  
這篇文章主要介紹了spring security登錄成功后跳轉回登錄前的頁面,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

spring security登錄成功后跳轉回登錄前的頁面

我剛好碰到了這么一個需求,正好自己也剛開始學spring security,但是我百度了一下,發(fā)現(xiàn)都講的好麻煩,其實大概了解完之后,親自實踐一下發(fā)現(xiàn),操作非常簡單。

需求如下

在未登錄的情況下訪問某些頁面被攔截,跳轉到登錄頁面,然后現(xiàn)在需要登錄成功之后跳轉到登錄之前的頁面。

要解決這個問題,就需要明白一點,就是我被攔截前的請求去哪里了?

答案是有個requestCache的東西保存了你的這些信息,那么知道了這一點,后面的東西就簡單了,登錄成功之后的處理從這東西里面把之前的請求取出來就好了。

代碼如下

.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 配置文件和登錄跳轉

好久沒碰springsecurity了,最近在做一個小東西的時候,部署遇到了跳轉問題,所以寫篇文章記錄一下。

項目的其中一個需求是登錄,賬號也是固定的,所以直接在配置文件中處理。springsecurity具體的配置網(wǎng)上一大堆,這邊就不展開說了。

項目結構

在這里插入圖片描述

直接上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標簽用來配置自定義的登錄頁面,項目里配置的是login.html,default-target-url是用來配置登錄成功以后,需要跳轉的頁面,這里配置的是/admin/index_user.html,因為index_user.html在webapp下的admin目錄里面。

自定義的登錄頁面login.html上需要加form標簽登錄框

如下:

<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">登&nbsp;&nbsp;錄</a>
								</div>
							</form>

完成。

eclipse調試成功后,導出war包,部署到tomcat里面。

瀏覽器輸入 http://localhost:8080/investigation/,直接跳轉到登錄頁面

在這里插入圖片描述

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

在這里插入圖片描述

查了半天,發(fā)現(xiàn)在登錄頁login.html里面form的action屬性里添加項目路徑investigation,然后重新運行服務器,就能成功跳轉。

具體修改如下

<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">登&nbsp;&nbsp;錄</a>
								</div>
							</form>

在這里插入圖片描述

在真正部署的時候,修改tomcat的配置文件里面默認的訪問路徑即可,不需要在上面的action里面改。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 使用IntelliJ IDEA 2017.2.5 x64中的Spring Initializr插件快速創(chuàng)建Spring Boot/Cloud工程(圖解)

    使用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-01
  • Spring Boot中如何使用斷路器詳解

    Spring Boot中如何使用斷路器詳解

    這篇文章主要給大家介紹了關于Spring Boot中如何使用斷路器的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-08-08
  • java爬蟲jsoup解析HTML的工具學習

    java爬蟲jsoup解析HTML的工具學習

    jsoup是一個解析HTML的第三方java庫,它提供了一套非常方便的API,可使用DOM,CSS以及類jQuery的操作方法來取出和操作數(shù)據(jù),本文就來開始jsoup的使用學習
    2022-07-07
  • 實例講解Java基礎之反射

    實例講解Java基礎之反射

    今天小編就為大家分享一篇關于實例講解Java基礎之反射,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • spring cloud gateway 限流的實現(xiàn)與原理

    spring cloud gateway 限流的實現(xiàn)與原理

    這篇文章主要介紹了spring cloud gateway 限流的實現(xiàn)與原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • Java設計模式之單例設計模式解析

    Java設計模式之單例設計模式解析

    這篇文章主要介紹了Java設計模式之單例設計模式解析,設計模式是在大量的實踐中總結和理論化之后優(yōu)選的代碼結構、編程風格、以及解決問題的思考方式,設計模式免去我們自己再思考和摸索,需要的朋友可以參考下
    2023-11-11
  • Java圖片與二進制相互轉換實現(xiàn)示例講解

    Java圖片與二進制相互轉換實現(xiàn)示例講解

    這篇文章主要介紹了Java圖片與二進制相互轉換實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2023-03-03
  • springboot中thymeleaf模板使用詳解

    springboot中thymeleaf模板使用詳解

    這篇文章將更加全面詳細的介紹thymeleaf的使用。thymeleaf 是新一代的模板引擎,在spring4.0中推薦使用thymeleaf來做前端模版引擎。
    2017-05-05
  • Scala 操作Redis使用連接池工具類RedisUtil

    Scala 操作Redis使用連接池工具類RedisUtil

    這篇文章主要介紹了Scala 操作Redis使用連接池工具類RedisUtil,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-06-06
  • 基于java springboot + mybatis實現(xiàn)電影售票管理系統(tǒng)

    基于java springboot + mybatis實現(xiàn)電影售票管理系統(tǒng)

    這篇文章主要介紹了基于java springboot + mybatis實現(xiàn)的完整電影售票管理系統(tǒng)基于java springboot + mybatis,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08

最新評論