Spring安全方法级别的安全批注不起作用

Spring安全方法级别的安全批注不起作用,spring,struts2,annotations,spring-security,Spring,Struts2,Annotations,Spring Security,我正在使用Struts 2+Spring Security 3制作一个简单的web应用程序。我想使用前后期注释来实现方法级别的安全性 但是注释不起作用 这是我的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:w

我正在使用Struts 2+Spring Security 3制作一个简单的web应用程序。我想使用前后期注释来实现方法级别的安全性

但是注释不起作用

这是我的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>
您可以清楚地看到,我给用户分配的角色是role\u user,但在访问该方法时,我希望它是role\u ADMIN,所以在逻辑上,在运行此代码时应该存在403 Access Denied错误。但它能够访问该方法并显示下一页

所以我认为注释不起作用


任何人都知道发生了什么?

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

我们不能像这样直接使用Spring注释


还有另一个插件用于相同的目的。您可以从链接下载jar文件并将其包含在项目中。

请执行一项测试。创建一个新的类SecuredAction,并将带有
@PreAuthorize
注释的方法放在此类中。然后将该bean注入MyAction,并从showPage方法调用它。我创建了一个新类SecuredAction,并将@PreAuthorize注释放在该类中,然后创建了SecuredAction的对象,并从MyAction:showPage()调用了它的方法,但仍然不起作用。或者我应该尝试使用Spring依赖项注入来注入SecuredAction的对象,但我认为这是一回事。是的,您必须使用注入。-我已经在我的评论中提到了它(“然后在MyAction中注入该bean”)原因是SpringAOP只对Springbeans有效,不适用于普通实例。谢谢您的关注。我唯一缺少的就是注射豆子。现在我用的是弹簧依赖注射,效果很好。没有必要创建新的SecuredAction。您能帮我回答这个问题吗:谢谢。这就是让Spring DI正常工作所缺少的。你能帮我解决这个问题吗:谢谢你提供最新的工作链接,因为旧的链接已经被删除了。
<?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>
<?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>
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";
    }
}