登录后重定向(GAE上的Spring安全性)

时间:2011-03-01 17:16:07

标签: java spring-security

我遇到了一些麻烦,使我的登录始终重定向到同一个地方。我做过类似的事情

<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/

4 个答案:

答案 0 :(得分:7)

您不应该在那里拥有那个然后执行重定向的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>

答案 1 :(得分:7)

我认为我们可以延长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);
       }

   }

 }

答案 2 :(得分:5)

你可以在没有处理程序的情况下通过在表单登录设置中添加“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

答案 3 :(得分:0)

它不漂亮,但我通过在最后位置定位自定义过滤器来解决它。

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。