登录后重定向(GAE 上的 Spring 安全性)
Posted
技术标签:
【中文标题】登录后重定向(GAE 上的 Spring 安全性)【英文标题】:Redirect after login (Spring security on GAE) 【发布时间】:2011-03-01 17:16:06 【问题描述】:我在让我的登录名总是重定向到同一个地方时遇到了一些麻烦。我做过这样的事情
<http auto-config="true" use-expressions="true" entry-point-ref="gaeEntryPoint" >
<intercept-url pattern="/_ah/login" access="permitAll"/>
<intercept-url pattern="/**" access="isAuthenticated()"/>
<custom-filter position="PRE_AUTH_FILTER" ref="gaeFilter"/>
<form-login authentication-success-handler-ref="authSuccessHandler"/>
</http>
<beans:bean id="authSuccessHandler"
class="dk.lindhardt.arbejdsfordeling.server.security.AuthenticationSuccessHandlerImpl"/>
还有
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException
if (authentication != null && authentication.isAuthenticated())
response.sendRedirect(OrganizationListServlet.URL);
它永远不会进入上面的这个方法。我如何让它做到这一点?
编辑:我正在关注本指南http://blog.springsource.com/2010/08/02/spring-security-in-google-app-engine/
【问题讨论】:
你能分享你的自定义过滤器(gaeFilter)的bean定义吗? 【参考方案1】:您不需要在那里有那个 servlet 来执行重定向。您可以在配置本身中进行重定向。
<http auto-config="true">
<intercept-url pattern="/app/login" filters="none" />
<form-login
login-page="/app/login"
login-processing-url="/app/dologin"
authentication-failure-url="/app/login?login_error=1"
default-target-url="/app/home"/>
</http>
【讨论】:
【参考方案2】:我认为我们可以扩展SimpleUrlAuthenticationSuccessHandler
并调用super.onAuthenticationSuccess()
方法来使用DefaultRedirectStrategy
。修改后的代码为:
public class AuthenticationSuccessHandlerImpl extends SimpleUrlAuthenticationSuccessHandler
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException
if (authentication != null)
setDefaultTargetUrl(OrganizationListServlet.URL);
super.onAuthenticationSuccess(request, response, authentication);
【讨论】:
SimpleUrlAuthenticationSuccessHandler 是一个类而不是一个接口,所以你需要扩展它。【参考方案3】:您可以通过在表单登录设置中添加“always-use-default-target”来完成此操作而无需处理程序
<form-login default-target-url='/your/target/url.htm' always-use-default-target='true' />
请参阅“设置默认登录后目标”下的http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ns-form-and-basic
【讨论】:
我已经尝试过了,但它不起作用。我仍然被重定向到我试图访问的受限页面。 如果是我,我的下一个测试将是在没有自定义入口点的情况下尝试它。简化直到它起作用,然后重新引入,直到找到导致问题的原因。【参考方案4】:它并不漂亮,但我通过在 LAST 位置放置一个自定义过滤器来解决它。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws
IOException, ServletException
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.isAuthenticated())
User user = (User) authentication.getPrincipal();
if (user.isLoggingIn())
if (authentication.getAuthorities().contains(AppRole.ROLE_NEW_USER))
((HttpServletResponse) response).sendRedirect(RegistrationServlet.URL);
else
((HttpServletResponse) response).sendRedirect("/" + OrganizationListServlet.URL);
user.setLoggingIn(false);
else if (user.isLoggingOut())
((HttpServletRequest) request).getSession().invalidate();
filterChain.doFilter(request, response);
我认为问题在于 gaeFilter 绕过了正常的登录过程,因此我不能使用 authentication-success-handler。
【讨论】:
1.我认为这不是优雅的解决方案。如blog.springsource.com/2010/08/02/… 所示,没有“绕过正常日志记录程序”之类的东西。在该博客代码中,Luke Taylor 使用了 failureHandler。同样可以使用successHandler。 好的,可以看出这是一个更好的解决方案。 我的反对票已锁定。如果您编辑答案,我很乐意将其删除。 user.isLoggingIn() 不是方法以上是关于登录后重定向(GAE 上的 Spring 安全性)的主要内容,如果未能解决你的问题,请参考以下文章
使用Spring Social,Spring安全登录后重定向到原始URL?
Spring Oauth和安全性在登录成功后重定向到/ login
在验证其ID令牌(GAE / Firebase)后重定向用户
如何在 Spring Security 中的 SSO 登录后重定向我的上一页?