GWT和Spring安全集成



在当前的GWT应用程序登录中,我使用后端XMPP服务器对来自服务器的Username-Password组合进行身份验证,响应是通过RPC登录机制发回的连接ID。

但是,我创建了一个新的"User"数据库(与XMPP服务器共享),其中存储了用户信息,并用作Spring Security验证用户名和密码的一部分;

谁可以分享一些代码片段GWT + Spring安全,登录/注销代码?

我使用本文中的代码:http://www.javacodegeeks.com/2010/12/securing-gwt-apps-with-spring-security.html

基本上,您实现了Spring接口
org.springframework.security.authentication.AuthenticationProvider

具有authenticate(Authentication)方法。用户在这个方法中输入的用户名和密码:

String username = (String) authentication.getPrincipal();
String password = (String) authentication.getCredentials();
// now try to get the user from your DB
User user = db.getUser(username, password);

和在Spring上下文中,您配置Spring安全过滤器(请参阅链接)并声明AuthenticationProvider:

<bean id="authProvider" class="com.example.security.MyAuthenticationProvider" />
<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider ref="authProvider" />
</security:authentication-manager>

我根本不使用GWT来登录用户…只是一个普通的JSP页面....您可以在这里看到一个示例JSP登录页面(和注销链接)当用户登录时,将加载GWT应用程序。要注销,只需执行如下操作:

RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, "/j_spring_security_logout");
try {
    rb.sendRequest(null, new RequestCallback() {
        public void onResponseReceived(Request request, Response response) {
            GWT.log("Logged user out: " + response.getStatusText());
        }
        public void onError(Request request, Throwable caught) {
            // try to recover somehow
        }
    });
} catch (RequestException re) {
    someOtherLogoutMechanism();
}

配置如下:我使用一个GWT应用程序

在我的jsp

<form name="login" action="<c:url value="j_spring_security_check"/>" method="POST">

in my web.xml

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

in my welcome.jsp

<sec:authorize ifAnyGranted="<%=gRoles%>">      
  <meta http-equiv="REFRESH" content="0;  url=demoApp/demoApp.jsp">
</sec:authorize>

spring-security.xml

    <http auto-config="false" access-denied-page="/login.jsp?error=Access%20Denied">
    <intercept-url pattern="/login.jsp*" filters="none" />
    <intercept-url pattern="/demoApp/**" access="${app.roles}" />
    <form-login login-page="/login.jsp"
                default-target-url="/welcome.jsp" 
                always-use-default-target="true" 
                authentication-failure-url="/login.jsp?error=true" />
    <logout logout-success-url="/login.jsp"/>   
    <anonymous/>

注意,我在这里使用基于表单的安全性示例。您可以使用security:jdbc-user-service连接到数据库进行身份验证。

查看这里的示例

最新更新