[英]Redirect after login (Spring security on GAE)
I am having some troubles making my login redirect to the same place always. 我有一些麻烦使我的登录始终重定向到同一个地方。 I have done something like this
我做过这样的事情
<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"/>
And 和
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);
}
}
}
It never gets into this method above. 它永远不会进入上述方法。 How do I make it do that?
我怎么做到这一点?
Edit: I was following this guide http://blog.springsource.com/2010/08/02/spring-security-in-google-app-engine/ 编辑:我按照本指南http://blog.springsource.com/2010/08/02/spring-security-in-google-app-engine/
You shouldn't need to have that servlet there that then does the redirect. 您不应该在那里拥有那个然后执行重定向的servlet。 You can have the redirect in the config itself.
您可以在配置中进行重定向。
<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>
I think we can extend SimpleUrlAuthenticationSuccessHandler
and call super.onAuthenticationSuccess()
method to use DefaultRedirectStrategy
. 我想我们可以扩展
SimpleUrlAuthenticationSuccessHandler
并调用super.onAuthenticationSuccess()
方法来使用DefaultRedirectStrategy
。 The modified code will be: 修改后的代码将是:
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);
}
}
}
You could do it without the Handler by adding the "always-use-default-target" to your form-login settings 您可以在没有处理程序的情况下通过向表单登录设置添加“always-use-default-target”来完成此操作
<form-login default-target-url='/your/target/url.htm' always-use-default-target='true' />
See http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ns-form-and-basic under "Setting a Default Post-Login Destination" 请参阅“设置默认的登录后目的地”下的http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ns-form-and-basic
It is not pretty but I solved it by positioning a custom filter at position 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);
}
I think the problem is that the gaeFilter bypasses the normal login procedure and therefore I can't use the authentication-success-handler. 我认为问题是gaeFilter绕过了正常的登录过程,因此我不能使用authentication-success-handler。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.