【问题标题】:How to integrate Spring Security and GWT?如何集成 Spring Security 和 GWT?
【发布时间】:2010-12-09 04:26:48
【问题描述】:

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

附:我正在使用 Spring 2.5.6、Spring Security 2.0.5 和 gwt-incubator-security 1.0.1。欢迎任何有用的链接和cmets。

这是我的配置文件

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>

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

【问题讨论】:

    标签: java security gwt spring-security integration


    【解决方案1】:

    我正在使用 GWT+Spring 安全性。 我发现在您的配置中,存在一些误解。事实上,有一种非常简单的方法可以让 spring security 与您的 gwt 一起工作,而不管 gwt-incubator-security。您只需要在 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. 将 Spring 与 GWT-SL 集成到应用程序中: 其实对于Spring和hibernate的整合,问题在于如何正确配置servlet。应该知道 Spring 有自己的 servlet “DispatcherServlet”,gwt 也有自己的 “gwt servlet”。 通常,在 GWT RPC 教程中,gwt-servlet 在 web-xml 中声明,如
     <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。请注意,这是 spring 的正确 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”中,然后把与DispatcherServlet相关的应用上下文放在“-servlet.xml”中,作为servlet名称是“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。

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      您的 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">
      

      【讨论】:

        【解决方案3】:

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

        上对此进行了描述

        【讨论】:

          【解决方案4】:

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

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

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

          【讨论】:

            【解决方案5】:
            【解决方案6】:

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

            【讨论】:

              【解决方案7】:
              猜你喜欢
              • 2013-05-29
              • 2012-12-29
              • 1970-01-01
              • 1970-01-01
              • 2012-04-07
              • 2013-02-10
              • 1970-01-01
              • 2023-01-30
              • 2014-05-16
              相关资源
              最近更新 更多