使spring security在登录页面的查询字符串中添加返回url

Abd*_*aly 4 spring-security

之前我问过是否有可能在Spring Security 2的表单登录页面中获取原始请求URL.事实证明实际上对我没有帮助,我需要的是重定向到表单登录页面以嵌入在它作为请求参数.所以,如果我有:

<form-login login-page="/login.html" /> 
Run Code Online (Sandbox Code Playgroud)

并尝试访问/secure.html我想最终进入/login.html?return_to=/secure.html.

Rag*_*ram 10

我想你需要使用spring-security-redirect.从本质上讲,如果您的网址是表单/login.html?spring-security-redirect=/secure.html,则spring安全性会在成功登录时自动重定向到secure.html.

从春季3.1.x开始,这不再是开箱即用的.你需要添加:

authentication-success-handler-ref="simpleUrlAuthenticationSuccessHandler"
Run Code Online (Sandbox Code Playgroud)

...到你的<form-login>元素并添加一个看起来像这样的bean:

<!-- Emulates the functionality of spring security 3.0.x by specifying the targetUrlParameter to be the value it
    defaulted to in 3.0.x. As of 3.1.x this is null by default with no option to specify in <form-login> -->
<beans:bean id="simpleUrlAuthenticationSuccessHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
    <beans:property name="useReferer" value="true"/>
    <beans:property name="defaultTargetUrl" value="/account"/>
    <beans:property name="targetUrlParameter" value="spring-security-redirect"/>
</beans:bean>
Run Code Online (Sandbox Code Playgroud)


Sha*_*eep 7

重定向到登录表单由AuthenticationEntryPoint.执行.对于Spring Security 3.0+,这通常是一个实例LoginUrlAuthenticationEntryPoint.在2.0中,相应的类是AuthenticationProcessingFilterEntryPoint.

入口点由ExceptionTranslationFilter负责缓存请求的入口点调用.因此,您可以编写一个自定义AuthenticationEntryPoint重定向到登录页面URL,并附加附加参数(包含当前请求URI).代码应该与标准实现几乎相同.

您可以使用namespace元素entry-point-ref上的属性将自定义AuthenticationEntryPoint注入命名空间配置http.如果你使用普通豆,你会把它注入ExceptionTranslationFilter.