Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring security 让spring security在登录页面的查询字符串中添加返回url_Spring Security - Fatal编程技术网

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

Spring security 让spring security在登录页面的查询字符串中添加返回url,spring-security,Spring Security,前面我讨论了是否可以在SpringSecurity2的表单登录页面中获取原始请求url。事实证明,这实际上对我没有帮助,我需要的是重定向到表单登录页面,将其作为请求参数嵌入其中。因此,如果我有: 然后尝试访问/secure.html我想在/login.html中结束?返回到=/secure.html我想您需要使用。本质上,如果您的url的格式为/login.html?spring security redirect=/secure.html,则成功登录后,spring security将自动

前面我讨论了是否可以在SpringSecurity2的表单登录页面中获取原始请求url。事实证明,这实际上对我没有帮助,我需要的是重定向到表单登录页面,将其作为请求参数嵌入其中。因此,如果我有:


然后尝试访问
/secure.html
我想在
/login.html中结束?返回到=/secure.html
我想您需要使用。本质上,如果您的url的格式为
/login.html?spring security redirect=/secure.html
,则成功登录后,spring security将自动重定向到secure.html

从spring 3.1.x开始,这不再适用于开箱即用。您需要添加:

authentication-success-handler-ref="simpleUrlAuthenticationSuccessHandler"
…添加到元素中,并添加一个如下所示的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>

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

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


您可以使用
http
namespace元素上的
entry point ref
属性将自定义AuthenticationEntryPoint注入命名空间配置。如果您使用的是普通bean,那么您可以将其注入
ExceptionTranslationFilter

如果您使用自己的登录页面,并且希望使用Raghuram建议的方法,那么您还必须在表单中放置一个隐藏字段,其中包含targetUrl参数的名称

对于您的示例(即使用*return_to*URL参数),它类似于:

<input type="hidden" name="spring-security-redirect" value="<c:out value="${param.return_to}" escapeXml="true" />" />


登录后,我不想去那里,我想去登录表单,并附加url作为参数。这听起来像是正确的答案,我正沿着这条路径前进,但有些东西似乎对我不起作用。听起来我需要再尝试一次。我尝试了这个解决方案,效果很好: