[英]Spring security j_spring_security_check strange redirect
[英]How to redirect to j_spring_security_check
我有一個java spring mvc web application
,我已經使用spring security
實現了登錄部分。 我使用的 spring 安全版本是3.2.5
。 我的 spring-security.xml 文件如下:
<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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/login" access="isAnonymous()" />
<form-login login-page="/login" default-target-url="/welcome"
authentication-failure-url="/login?error" username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/login?logout" invalidate-session="false" />
<!-- access denied page -->
<access-denied-handler error-page="/403" />
</http>
<beans:bean id="daoAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="userDetailsService"/>
</beans:bean>
<beans:bean id="authenticationManager"
class="org.springframework.security.authentication.ProviderManager">
<beans:property name="providers">
<beans:list>
<beans:ref local="daoAuthenticationProvider" />
</beans:list>
</beans:property>
</beans:bean>
<!-- Declare an authentication-manager to use a custom userDetailsService -->
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
<password-encoder hash="bcrypt" />
</authentication-provider>
</authentication-manager>
</beans:beans>
現在我有一個要求,我將首先顯示一些內容,然后用戶必須登錄才能查看完整內容。 所以我提供了一個登錄鏈接,它會顯示登錄表單。 但是當用戶登錄時,會顯示默認頁面。 我希望將用戶重定向到用戶所在的 URL。我還嘗試將表單 POST 到我的自定義控制器方法之一,然后從那里重定向到 spring 安全檢查。 我打算將當前 URL 從方法存儲到會話,並將用戶從 spring 安全性的默認目標方法重定向到該方法。 但是重定向到 spring 安全檢查總是給我無效的用戶名或密碼錯誤,因為它無法對用戶進行身份驗證。 我的自定義方法如下:
@RequestMapping(value = "/edu-login", method = RequestMethod.POST)
public String eduLogin(@ModelAttribute ("username") String username, @ModelAttribute ("password") String password, Model model, HttpServletRequest request, HttpServletResponse response, RedirectAttributes ra)
{
//My custom logic to stroe url to session
ra.addAttribute("username", username);
ra.addAttribute("password", password);
return "redirect:/j_spring_security_check";
}
有沒有辦法解決我的問題。 我想要的只是用戶在登錄后返回瀏覽器上的 url。
有適用於您的用例的解決方案,但這不是您應該應用的正確流程。 此外,您不能以這種方式使用RedirectAttributes
,因為它僅適用於MVC
層,並且form-login
過濾器位於過濾器層。
您試圖實現的順序是這樣的:
public_url > [login:if required] > private_url
對於這樣的流程,您可以利用一個組件,即org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler 。 正如它在 javadoc 中所述:
一種身份驗證成功策略,它可以利用ExceptionTranslationFilter可能已存儲在會話中的DefaultSavedRequest 。 當此類請求被攔截並需要身份驗證時,請求數據將被存儲以記錄身份驗證過程開始前的原始目的地,並允許在重定向到同一 URL 時重新構建請求。 如果合適,此類負責執行到原始 URL 的重定向。
因此,假設您現有的未受保護的 url 是/public/edu
,第二個 url(受保護的)是/private/edu
。
在/public/edu
您應該提供指向/private/edu
的鏈接。 在訪問/private/edu
,SpringSecurityFilterChain 將檢查用戶是否已通過身份驗證並具有所需的授權。
/private/edu
臨時保留在會話中。 一旦用戶執行正確的登錄, SavedRequestAwareAuthenticationSuccessHandler
會將用戶重定向到/private/edu
而不是默認的成功 url 頁面。這可能是一個示例配置:
<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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/login" access="isAnonymous()" />
<intercept-url pattern="/private/edu" access="isAnonymous()" />
<form-login
login-page="/login"
authentication-success-handler-ref="savedRequestSuccesHandler"
authentication-failure-url="/login?error"
username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/login?logout" invalidate-session="false" />
<!-- access denied page -->
<access-denied-handler error-page="/403" />
</http>
<beans:bean id="savedRequestSuccesHandler"
class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
<beans:property name="defaultTargetUrl" value="/welcome" />
</beans:bean>
<beans:bean id="daoAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="userDetailsService"/>
</beans:bean>
<beans:bean id="authenticationManager"
class="org.springframework.security.authentication.ProviderManager">
<beans:property name="providers">
<beans:list>
<beans:ref local="daoAuthenticationProvider" />
</beans:list>
</beans:property>
</beans:bean>
<!-- Declare an authentication-manager to use a custom userDetailsService -->
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
<password-encoder hash="bcrypt" />
</authentication-provider>
</authentication-manager>
</beans:beans>
注意我已經刪除了<http>
元素的default-target-url="/welcome"
並引入了屬性authentication-success-handler-ref="savedRequestSuccesHandler"
,它引用了我剛剛創建的新 bean:
<beans:bean id="savedRequestSuccesHandler"
class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
<beans:property name="defaultTargetUrl" value="/welcome" />
</beans:bean>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.