У меня возникают проблемы с тем, чтобы мой логин всегда перенаправлялся в одно и то же место. Я сделал что-то вроде этого
<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/
Ответ 1 · рейтинг 7 · Scott
Вам не нужно иметь этот сервлет, который затем выполняет перенаправление. Вы можете иметь перенаправление в самой конфигурации.
<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 · рейтинг 7 · Ritesh
Я думаю, мы можем расширить 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 · рейтинг 5 · digitaljoel
Вы можете сделать это без обработчика, добавив "всегда использовать цель по умолчанию" в настройках формы входа в систему.
<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 · AnAmuser
Это некрасиво, но я решил эту проблему, поместив пользовательский фильтр в ПОСЛЕДНЮЮ позицию.
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 обходит обычную процедуру входа в систему, и поэтому я не могу использовать обработчик аутентификации-успеха.