Spring Security方法级别的安全注解不起作用。

3
我正在使用Struts 2 + Spring Security 3制作一个简单的Web应用程序,并希望在方法级别上使用Pre-Post注释进行安全控制。但是这些注释无法正常工作。以下是我的web.xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>MyCustomSpringSecurity</display-name>

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

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

  <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>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
  <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

这是我的struts.xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
    <action name="firstPage" class="code.action.MyAction" method="showPage">
        <result name="success">/firstPage.jsp</result>
    </action>
</package>
</struts>

这是我的applicationContext.xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" 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-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <global-method-security pre-post-annotations="enabled" >
    </global-method-security>

    <beans:bean id="myAction" class="code.action.MyAction">
        <intercept-methods>
            <protect access="ROLE_ADMIN" method="showPage"/>
        </intercept-methods>
    </beans:bean>

    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/index.jsp" access="permitAll" />
        <intercept-url pattern="/firstPage" access="hasRole('ROLE_USER')" />
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="user" password="user" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>

最后,这是我的 MyAction.java 文件

package code.action;

import org.springframework.security.access.prepost.PreAuthorize;

public class MyAction {
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String showPage(){
        System.out.println("in showPage");
        return "success";
    }
}

你可以清楚地看到,我正在为用户分配角色ROLE_USER,但当访问该方法时,我希望它是ROLE_ADMIN,所以从逻辑上讲,在运行此代码时应该出现403访问被拒绝的错误。但是它能够访问该方法并显示下一页。
因此,我认为注释不起作用了。
有人知道发生了什么吗?

1
请完成一个测试。创建一个名为SecuredAction的新类,并在其中使用@PreAuthorize注解的方法。然后将该bean注入到MyAction中,并从showPage方法中调用它。 - Ralph
我按照你说的尝试了。我创建了一个新的类SecuredAction,并在该类中放置了我的@PreAuthorize注释,然后创建了SecuredAction对象并从MyAction中调用其方法:showPage(),但仍然无法正常工作。或者我应该尝试使用Spring依赖注入来注入SecuredAction对象,但我认为这将是同样的事情。 - user995009
是的,你必须使用注入。-- 我已经在我的评论中提到了它(“然后将该bean注入到MyAction中”)。-- 原因是Spring AOP仅适用于Spring Beans,而不适用于普通实例。 - Ralph
感谢您的关注。我唯一缺少的是注入Bean。现在我使用了Spring依赖注入,它运行良好。没有必要创建新的SecuredAction。 - user995009
你能帮我解决这个问题吗:http://stackoverflow.com/questions/8208501/struts-2-spring-spring-security-haspermission-not-working - user995009
2个回答

4
为了使Spring注解在Struts 2操作中有意义,您必须使用Struts 2 Spring Plugin。这将使用Spring来实例化Struts 2对象,允许注解处理、注入等操作。

谢谢。这就是我缺少的东西,让Spring DI工作起来。 - user995009
你能帮我解决这个问题吗:http://stackoverflow.com/questions/8208501/struts-2-spring-spring-security-haspermission-not-working - user995009

2

我们不能直接这样使用Spring注解。

有另一个插件Struts 2 - Spring Plugin可以实现同样的目的。您可以从链接下载jar文件并将其包含在项目中。


感谢提供最新的“可用”链接,因为旧链接已被删除。 - user995009

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接