【问题标题】:Spring Security method authorization not workingSpring Security方法授权不起作用
【发布时间】:2012-03-22 19:18:59
【问题描述】:

这里是问题的描述。我正在使用 GWT 开发一个 Web 应用程序。我已经使用以下代码成功地将 spring security 与 gwt 集成以实现身份验证功能。现在我想在我的 Web 应用程序中使用 spring “方法安全性”。所以我按照http://static.springsource.org/spring-security/site/docs/3.1.x/reference/ns-config.html 中所说的做了,

  • 只需在上面提到的应用程序上下文文件中添加<global-method-security secured-annotations="enabled"/>。

     <http>
      <http-basic/>
    
      <intercept-url pattern="/**" access=""/>
    
      <form-login />
      <logout />
     </http> 
    
      <authentication-manager>
        <authentication-provider>
          <user-service>
            <user name="jimis" password="jimispassword" authorities="ROLE_USER,ROLE_ADMIN" />
            <user name="bob" password="bobspassword" authorities="ROLE_ADMIN" />
          </user-service>
        </authentication-provider>
      </authentication-manager>
    
      **<global-method-security secured-annotations="enabled"/>** 
    
  • 然后在我控制访问的函数上方添加注解@Secured("ROLE_ADMIN")

然后我在 web.xml 中添加应用上下文的声明如下:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee">

 <!-- Default page to serve -->

  <welcome-file-list>
    <welcome-file>App.html</welcome-file>
  </welcome-file-list>
  <session-config>
    <session-timeout>10</session-timeout> <!-- in minutes -->
  </session-config>
<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>
  <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.rpc</url-pattern>
  </servlet-mapping>

</web-app>

请注意,我刚刚声明了 gwt-servlet 而不是 spring 调度程序 servlet。

但是,这种配置似乎不起作用。事实上,任何角色都可以拥有访问该功能的权限。 很奇怪。 希望您的回答!

【问题讨论】:

    标签: gwt spring-security


    【解决方案1】:

    使用包含两个属性的单个 global-method-security 元素。

    如果您正在这样做,还请阅读 relevant section of the Spring Security FAQ 关于使用方法安全性和 Web 控制器的问题(您还会发现此处讨论的相同问题)。

    您报告的日志消息不是错误并且不重要,除非您在表达式中使用hasPermission()。

    【讨论】:

    • 感谢第一个评论,我已经完成了 ,
    • 谢谢,第一条评论,我已经完成了,那么,第二条评论好像很有趣,我刚刚阅读了文档。我应该给出背景,因为我将 Spring Security 与 GWT 集成,在 web.xml 中,我声明了 gwt-servlet 而不是 dispatcherServlet;所以如果方法安全应该与 dispatherServlet 一起使用,那么DispatcherServlet 声明的缺失导致了这个dis-function问题。那么你的想法是什么?
    • 很抱歉我没有权利为你的答案投票,但这真的很有帮助
    • 您可能应该编辑您的问题,以提供有关您尝试保护的 ​​bean 实例的定义位置等的更多信息。无论您是使用 GWT 还是任何不相关的东西。如果需要,可以在非 Web 应用程序中使用方法安全性。
    • 我询问了有关“您尝试保护的 ​​bean 实例是在何处定义的”的信息 - 从您的配置来看,您似乎没有保护 Spring bean,因此它不起作用。如果您尝试保护的对象不是由 Spring 管理的,那么 Spring AOP(安全性所依赖的方法)将不会产生任何影响。如果是这种情况,Spring Security 只有在你使用 aspectj 时才能工作。
    猜你喜欢
    • 1970-01-01
    • 2018-06-03
    • 2016-11-13
    • 2014-09-13
    • 2017-09-20
    • 2016-02-28
    • 2019-08-05
    • 2013-03-15
    • 2012-10-19
    相关资源
    最近更新 更多