如何集成Spring Security和GWT?

时间:2009-10-22 17:23:15

标签: java security gwt spring-security integration

我正在尝试集成Spring Security和GWT。我也在使用gwt-incubator-security。我按照wiki页面上的描述配置了所有内容。 我设法通过使用intercept-url来获得安全性,但我无法使用注释使其工作。关于问题是什么的任何想法?

P.S。我正在使用Spring 2.5.6,Spring Security 2.0.5和gwt-incubator-security 1.0.1。欢迎任何有用的链接和评论。

以下是我的配置文件

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<global-method-security secured-annotations="enabled"
    jsr250-annotations="disabled" />
<http auto-config="true">
    <!-- <intercept-url pattern="/**/*.rpc" access="ROLE_USER" /> -->
    <intercept-url pattern="/gwt/**" access="ROLE_USER" />
    <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
</http>
<authentication-provider>
    <user-service>
        <user name="rod" password="koala"
            authorities="ROLE_SUPERVISOR, ROLE_USER, ROLE_TELLER" />
        <user name="dianne" password="emu" authorities="ROLE_USER,ROLE_TELLER" />
        <user name="scott" password="wombat" authorities="ROLE_USER" />
        <user name="peter" password="opal" authorities="ROLE_USER" />
    </user-service>
</authentication-provider>
<beans:bean id="greetService" class="com.ct.test.server.GreetingServiceImpl" />

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<!-- Default page to serve -->
<welcome-file-list>
    <welcome-file>Spring_test.html</welcome-file>
</welcome-file-list>
<!--  Spring related configuration  -->
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<!-- Initialise the Spring MVC DispatcherServlet -->
<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<!-- Map the DispatcherServlet to only intercept RPC requests -->
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/spring_test/greet.rpc</url-pattern>
    <!--
        <url-pattern>/org.example.gwtwisdom.GwtWisdom/services/*</url-pattern>
    -->
</servlet-mapping>
<servlet>
    <servlet-name>greetServlet</servlet-name>
    <servlet-class>com.ct.test.server.GreetingServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>greetServlet</servlet-name>
    <url-pattern>/spring_test/greet.rpc</url-pattern>
</servlet-mapping>
<!-- Spring security -->
<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>

弹簧servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!-- The application context definition for the DispatcherServlet -->
<bean id="urlMapping" class="com.gwtincubator.security.server.GWTSecuredHandler">
    <property name="mappings">
        <map>
            <entry key="/spring_test/greet.rpc" value-ref="greetService" />
        </map>
    </property>
</bean>

以下是我尝试与Spring Security集成的示例项目:http://www.filedropper.com/springtest_1

7 个答案:

答案 0 :(得分:1)

我正在使用GWT + Spring安全性。 我发现在你的配置中,存在一些误解。事实上,无论 gwt-incubator-security 如何,都有一种非常简单的方法可以让spring安全与你的gwt一起工作。您只需要在web.xml中声明应用程序上下文。

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext-security.xml</param-value>
  </context-param>

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

  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener> 

你没有在这里声明你的MVC dispatcherServlet!然后一切都运行,因为Spring Security框架机制。

但是这种配置方式并没有声明DispatcherServlet,它很简单,但是如果你需要一些需要DispatcherServlet的安全功能,那就是一个“馅饼”。所以我见过。

然后如果你坚持使用gwt-incubator-security。我用法语阅读了一个非常好的解决方案,但它没有取消选中。 http://hugo.developpez.com/tutoriels/java/gwt/utilisation-gwt-avec-spring-et-hibernate/

  1. 使用GWT-SL在应用程序中集成Spring: 实际上,对于Spring和hibernate的集成,问题是如何正确配置servlet。应该知道 Spring有自己的servlet“DispatcherServlet”,所以gwt的“gwt servlet”。 通常,在GWT RPC教程中,gwt-servlet在 web-xml 中声明,如
  2.  <servlet>
        <servlet-name>appService</servlet-name>
        <servlet-class>com.google.gwt.app.example.server.AppServiceImpl</servlet-class>
      </servlet>
      <servlet-mapping>
         <servlet-name>appService</servlet-name>
         <url-pattern>/app/appService</url-pattern>
       </servlet-mapping>
    

    如果你非常喜欢Spring,并且想要使用DispatcherServlet来分派请求,那么GWT-handler可以帮助你摆脱这个问题。 首先,您在 web.xml 中加载应用程序上下文,如下所示:

    <context-param>
    <param-name> contextConfigLocation </param-name>
        <param-value> classpath:applicationContext_GWT.xml </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>
    

    然后你可以在Spring上下文中声明你的rpc服务: 的 applicationContext_GWT.xml

    <bean id=" appService " 
             class=" com.google.gwt.app.example.server.AppServiceImpl">
    </bean>
    

    但是你不应该忘记在应用程序上下文文件applicationContext_GWT.xml中添加GWTHandler声明
                                                                        最后一件事是在web.xml中声明spring servlet:DispatcherServlet。请注意这是春天适当的servlet而不是GWT-SL的事实。 的web.xml

    <servlet>  
        <servlet-name>handler</servlet-name>
        <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
           <load-on-startup>1</load-on-startup>
    </servlet>  
    <servlet-mapping>
    <servlet-name>handler</servlet-name>
    <url-pattern>*.rpc</url-pattern>
    </servlet-mapping>
    

    Servlet名称很重要,因为DispatcherServlet将搜索由“* -servlet.xml”命名的spring上下文文件。由于servlet名称是handler,它将搜索spring上下文“handler-servlet.xml”。所以在这里我们将解决这样的问题,我们将与DispatcherServlet无关的应用程序上下文放在“applicationContext_GWT.xml”中,然后将依赖于“-servlet.xml”中的DispatcherServlet的应用程序上下文作为servlet放置name是“handler”,然后我们应该有“handler-servlet.xml”,然后将来自applicationContext_GWT.xml的GWT_SL的以下配置放入handler-servlet.xml 处理程序-servlet.xml中

    <bean id="urlProjectMapping" class="org.gwtwidgets.server.spring.GWTHandler">
            <!-- Supply here mappings between URLs and services. Services must implement the RemoteService interface but are not otherwise restricted.-->
            <property name="mappings">
                 <map>
        <!-- Other mappings could follow -->
        <entry key="/app/appService.rpc" value-ref="appService" />
                 </map>
             </property>
    </bean> 
    

    然后在web.xml dans la declaration de servlet中添加以下配置。

    <init-param>
                   <param-name>contextConfigLocation</param-name>
        <param-value> /WEB-INF/handler-servlet.xml </param-value>
    </init-param>
    

    过滤器模式仅涉及带有后缀.rpc的RPC调用 (我没有使用GWT-SL,因此尚未检查上面的集成方法。)

    完成上述所有配置后,在applicationi上下文文件中创建filtreprocessentrypoint。

    希望这可以帮到你!

答案 1 :(得分:0)

您的applicationContext.xml中似乎缺少命名空间配置。

它应该是这样的:

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:sec="http://www.springframework.org/schema/security"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">

答案 2 :(得分:0)

Acris框架也使用Spring Security。他们在维基http://code.google.com/p/acris/wiki/SecurityServer

中对此进行了描述

答案 3 :(得分:0)

我猜你需要在applicationContext.xml中拥有架构,并启用注释:

<context:annotation-config />
<context:component-scan base-package="my.package" />

参考:http://weblogs.java.net/blog/seemarich/archive/2007/11/annotation_base.html

答案 4 :(得分:0)

答案 5 :(得分:0)

您可以使用Putnami Web Toolkit (PWT)框架,这里是一个集成Spring Framework的教程,另一个是Spring Security的集成。

答案 6 :(得分:-1)