Spring Security 方法级别安全注释不起作用

发布于 2024-12-16 02:47:50 字数 4118 浏览 0 评论 0原文

我正在使用 Struts 2 + Spring Security 3 制作一个简单的 Web 应用程序。我想使用 Pre-Post Annotations 来实现方法级别的安全性。

但注释不起作用。

这是我的 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 Access Denied 错误。但它能够访问该方法并显示下一页。

所以我认为注释不起作用。

有人知道发生了什么事吗?

I am making a simple web application using Struts 2 + Spring Security 3. And I want to use Pre-Post Annotations for Method Level Security.

But the Annotations are not working.

Here is my 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>

This is my 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>

This is my 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>

Finally This is 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";
    }
}

You can clearly see that I am assigning role to user is ROLE_USER but I want it to be ROLE_ADMIN when accessing that method, so logically there should be error of 403 Access Denied when running this code. But it is able to access that method and shows next page.

So I think the annotations are not working.

Any body knows whats going on ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人如玉 2024-12-23 02:47:50

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

In order for Spring annotations to be meaningful in a Struts 2 action you must use the Struts 2 Spring Plugin. This will use Spring to instantiate Struts 2 objects, allowing annotation processing, injection, etc.

寄意 2024-12-23 02:47:50

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

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

We cannot directly use Spring annotations like this.

There is another plugin Struts 2 - Spring Plugin for the same purpose. You can download the jar file from the link and include it in your project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文