Подтвердить что ты не робот

Перенаправление после входа в систему (Spring защита от GAE)

У меня возникают некоторые проблемы, делающие мой переадресацию на вход в одно и то же место всегда. Я сделал что-то вроде этого

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

4b9b3361

Ответ 1

Вам не нужно иметь этот сервлет, который затем перенаправляет. Вы можете иметь перенаправление в самой конфигурации.

<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);
       }

   }

 }

Ответ 3

Вы можете сделать это без Handler, добавив "постоянное использование-по умолчанию" в ваши настройки формы входа

<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 обходит обычную процедуру входа и поэтому не могу использовать обработчик проверки подлинности.