标签:spring spring-security redirect login
我知道之前已经问过这个问题,但是我在这里遇到了一个特殊的问题.
我使用spring security 3.1.3.
我的Web应用程序中有3个可能的登录案例:
>通过登录页面登录:好的.
>通过限制页面登录:也可以.
>通过非限制性页面登录:不行……每个人都可以访问“产品”页面,如果用户登录,用户可以发表评论.因此,登录表单包含在同一页面中,以便允许用户连接.
案例3)的问题是我无法将用户重定向到“产品”页面.无论如何,他们会在成功登录后重定向到主页.
请注意,对于案例2),成功登录后,重定向到受限页面的工作开箱即用.
这是我的security.xml文件的相关部分:
<!-- Authentication policy for the restricted page -->
<http use-expressions="true" auto-config="true" pattern="/restrictedPage/**">
<form-login login-page="/login/restrictedLogin" authentication-failure-handler-ref="authenticationFailureHandler" />
<intercept-url pattern="/**" access="isAuthenticated()" />
</http>
<!-- Authentication policy for every page -->
<http use-expressions="true" auto-config="true">
<form-login login-page="/login" authentication-failure-handler-ref="authenticationFailureHandler" />
<logout logout-url="/logout" logout-success-url="/" />
</http>
我怀疑“每个页面的身份验证策略”都要对此问题负责.但是,如果我将其删除,我将无法再登录… j_spring_security_check发送404错误.
编辑:
感谢拉尔夫,我找到了解决方案.所以这就是事情:我使用了这个属性
<property name="useReferer" value="true"/>
拉尔夫给我看了.之后我遇到了我的案例问题1):当通过登录页面登录时,用户停留在同一页面(并没有重定向到主页,就像以前一样).直到此阶段的代码如下:
<!-- Authentication policy for login page -->
<http use-expressions="true" auto-config="true" pattern="/login/**">
<form-login login-page="/login" authentication-success-handler-ref="authenticationSuccessHandlerWithoutReferer" />
</http>
<!-- Authentication policy for every page -->
<http use-expressions="true" auto-config="true">
<form-login login-page="/login" authentication-failure-handler-ref="authenticationFailureHandler" />
<logout logout-url="/logout" logout-success-url="/" authentication-success-handler-ref="authenticationSuccessHandler"/>
</http>
<beans:bean id="authenticationSuccessHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
<!-- After login, return to the last visited page -->
<beans:property name="useReferer" value="true" />
</beans:bean>
<beans:bean id="authenticationSuccessHandlerWithoutReferer" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
<!-- After login, stay to the same page -->
<beans:property name="useReferer" value="false" />
</beans:bean>
至少在理论上这应该有效,但事实并非如此.我仍然不知道为什么,所以如果有人对此有答案,我很乐意创建一个新主题,以便他分享他的解决方案.
与此同时,我找到了解决方法.不是最好的解决方案,但就像我说的,如果有人有更好的表现,我会全力以赴.这是登录页面的新身份验证策略:
<http use-expressions="true" auto-config="true" pattern="/login/**" >
<intercept-url pattern="/**" access="isAnonymous()" />
<access-denied-handler error-page="/"/>
</http>
这里的解决方案非常明显:只有匿名用户才能使用登录页面.连接用户后,错误处理程序会将其重定向到主页.
我做了一些测试,一切似乎都运行良好.
解决方法:
登录后(用户被重定向到哪个URL)发生的事情由AuthenticationSuccessHandler处理.
此接口(实现它的具体类是SavedRequestAwareAuthenticationSuccessHandler)由AbstractAuthenticationProcessingFilter或其子类之一(例如,UsernamePasswordAuthenticationFilter)在successAuthentication方法中调用.
因此,为了在案例3中进行其他重定向,您必须继承SavedRequestAwareAuthenticationSuccessHandler,并使其执行您想要的操作.
有时(取决于您的确切用例),只需启用AbstractArlAuthenticationSuccessHandler(超类SavedRequestAwareAuthenticationSuccessHandler)调用的AbstractAuthenticationTargetUrlRequestHandler的useReferer标志即可.
<bean id="authenticationFilter"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<property name="filterProcessesUrl" value="/login/j_spring_security_check" />
<property name="authenticationManager" ref="authenticationManager" />
<property name="authenticationSuccessHandler">
<bean class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
<property name="useReferer" value="true"/>
</bean>
</property>
<property name="authenticationFailureHandler">
<bean class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<property name="defaultFailureUrl" value="/login?login_error=t" />
</bean>
</property>
</bean>
标签:spring,spring-security,redirect,login 来源: https://codeday.me/bug/20190917/1809668.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。