gpt4 book ai didi

java - 如何重定向到 j_spring_security_check

转载 作者:行者123 更新时间:2023-11-30 06:18:15 24 4
gpt4 key购买 nike

我有一个 java spring mvc Web 应用程序,并且我已经使用 spring security 实现了登录部分。我使用的 spring security 版本是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。我还尝试将表单发布到我的自定义 Controller 方法之一,然后从那里重定向到 spring 安全检查。我打算将方法中的当前 URL 存储到 session 中,并将用户从 Spring Security 的默认目标方法重定向到该方法。但是重定向到 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";
}

有什么办法可以解决我的问题吗?我想要的只是用户登录后返回到浏览器上的网址。

最佳答案

有一个适合您的用例的解决方案,但这不是您应该应用的正确流程。此外,您不能使用 RedirectAttributes这样,因为它仅适用于 MVC层和form-login过滤器位于过滤层。

您想要实现的顺序是这样的:

public_url > [login:if required] > private_url

对于这样的流程,您可以利用一个组件,即 org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler 。正如 javadoc 中所述:

An authentication success strategy which can make use of the DefaultSavedRequest which may have been stored in the session by the ExceptionTranslationFilter. When such a request is intercepted and requires authentication, the request data is stored to record the original destination before the authentication process commenced, and to allow the request to be reconstructed when a redirect to the same URL occurs. This class is responsible for performing the redirect to the original URL if appropriate.

所以,假设您现有的不 protected 网址是 /public/edu第二个网址( protected 网址)是 /private/edu

/public/edu您应该提供一个指向 /private/edu 的链接。访问时/private/edu SpringSecurityFilterChain 将检查用户是否经过身份验证并拥有所需的授权。

  • 如果用户已通过身份验证,则将直接访问 URL。
  • 如果没有,它将重定向到登录,同时保留请求的 URL /private/edu暂时在 session 中。一旦用户执行正确的登录,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>

请注意,我已删除 default-target-url="/welcome" <http>的元素并引入了属性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>

关于java - 如何重定向到 j_spring_security_check,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48762780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com