注销后GWT + Spring Security登录问题

时间:2013-09-25 10:06:40

标签: spring gwt login spring-security

我正在使用Spring Security来保护对我的GWT首页应用程序“/ HumanResources.html”的访问。它检查用户的凭据是否正确(通过将它们与ldap内容进行比较),并查看用户是否存在于数据库(自定义授权表)中。

首次用户登录,没有问题,然后显示用户注销和“.jsp”页面。 但是当他想再次访问“HumanResources.html”页面时,显然会忽略身份验证(不显示登录表单)并显示页面。只有界面可见,安全的RPC服务才能检索数据。

此问题出现在外部Tomcat服务器上(在Firefox和Chrome上测试),但不在GWT Dev模式下。 CTRL + F5似乎有效,我查找了其他缓存问题,但没有帮助。

任何人都可以帮助我吗?

security-applicationContext.xml 的一部分:

<http use-expressions="true" auto-config="false">
    <intercept-url pattern="/HumanResources.html" access="isAuthenticated()" />
    <form-login 
        login-page='/login.jsp'
        authentication-failure-url = "/login.jsp?login_error=1"
        authentication-success-handler-ref="HRAuthenticationHandler" />
    <logout 
        logout-url="/logout" 
        logout-success-url="/logout.jsp" 
        delete-cookies="JSESSIONID"/>
</http>

<beans:bean id="HRAuthenticationHandler" class="lu.sfeir.candidate.server.auth.HRAuthenticationHandler">
    <beans:property name="useReferer" value="true" />
</beans:bean>

<ldap-server url="${ldap.serverUrl}" manager-dn="${ldap.adminLogin}" manager-password="${ldap.adminPassword}" />

<authentication-manager>
    <ldap-authentication-provider 
        group-search-base="${ldap.groups}"
        user-search-base="${ldap.users}"
        user-search-filter="${ldap.userId}">
    </ldap-authentication-provider>
</authentication-manager>

和我的自定义 AuthenticationHandler实现

public class HRAuthenticationHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Autowired
    private AuthorizedUsersDao usersDao;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException,
            ServletException {

        // Check if the user exist in the DB
        if(usersDao.findUser(((UserDetails)authentication.getPrincipal()).getUsername())) {
            // Redirect to home page
            super.onAuthenticationSuccess(request, response, authentication);
        } else {
            // Redirect to error page
            response.sendRedirect("/spring_security_login?login_error");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

编辑: 对不起你误导了你。 我挖了一下,发现安全性不起作用,因为用户浏览器正在缓存xxx.html页面,js,css等 您可以: 1)通过设置html标题强制刷新页面: 这是最简单的解决方案,因为您使用的是安全的RPC服务,应该为您提供。 或者根本禁用缓存。

请注意,它会增加您的网络流量。

2) 我们的应用程序正在检查RPC中是否存在安全错误:

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.InvocationException;

public abstract class CustomAsyncCallback<T> implements AsyncCallback<T> {
    @Override
    public void onFailure(Throwable caught) {
        if (caught instanceof InvocationException) {
            InvocationException ie = (InvocationException) caught;
            if(ie.getMessage().contains("j_spring_security_check")) {
                Window.alert("User session expired, please login again");
                Window.open(GWT.getHostPageBaseURL() + "login.jsp", "_self", null);
                return;
            }
        }
    }
}

您必须在某处调用它,即在显示您当前登录的用户时:

    userSecurityService.getUser(new CustomAsyncCallback<String>() {
        @Override
        public void onSuccess(String result) {
            usrLoginLbl.setText(result);
        }
    });

或者您可以在故障方法

中捕获此异常