Spring security Spring Security不处理前/后注释

Spring security Spring Security不处理前/后注释,spring-security,Spring Security,我试图让前/后注释与web应用程序一起工作,但由于某些原因,spring安全性没有发生任何变化 web.xml: <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/rvaContext-business.xml /WEB-INF/rvaContext-security.xml

我试图让前/后注释与web应用程序一起工作,但由于某些原因,spring安全性没有发生任何变化

web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/rvaContext-business.xml
        /WEB-INF/rvaContext-security.xml  
    </param-value>
</context-param>

<!-- Spring security filter -->
<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>rva</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rva</servlet-name>
    <url-pattern>/rva/*</url-pattern>
 </servlet-mapping>

要在控制器上启用secuity注释,您应该在声明控制器的上下文中声明
,即在
rva servlet.xml中声明

实际上,您需要在配置文件中重新定义,该文件也用于控制器。在springroo中,这是webmvc config.xml。使用Roo配置安全性时,配置文件applicationContext-security.xml最初配置为启用这些注释。这有点让人困惑…

请看(我的重点)

在SpringWeb应用程序中,保存 DispatcherServlet的SpringMVCbean通常与 主应用程序上下文。它通常在一个名为 myapp-servlet.xml,其中“myapp”是分配给Spring的名称 web.xml中的DispatcherServlet。一个应用程序可以有多个 DispatcherServlets,每个都有自己的独立应用程序上下文。 这些“子”上下文中的bean对其余部分不可见 应用“父”应用程序上下文由 您在web.xml中定义的ContextLoaderListener对所有人都可见 孩子们在不同的环境中成长。此父上下文通常是您定义的位置 您的安全配置,包括 元素)。因此,应用于中的方法的任何安全约束 由于无法看到这些bean,因此不会强制执行这些Webbean 从DispatcherServlet上下文。您需要移动 声明到web上下文或移动 您希望在主应用程序上下文中保护的bean

通常,我们建议在服务中应用方法安全性 层而不是单个web控制器上。


如果您将切入点应用于服务层,则只需在应用程序的安全上下文中设置

您为我节省了几个小时的调试时间。。。谢谢
<?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.0.xsd">

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

<http use-expressions="true">
    <form-login login-page="/login" />
    <logout />
    <remember-me />
</http>
...
@Controller
@RequestMapping("/login")
public class LoginController {

    @RequestMapping(method = RequestMethod.GET)
    public String login(ModelMap map){
        map.addAttribute("title", "Login: AD Credentials");
        return("login");
    }

    @RequestMapping("/secure")
    @PreAuthorize("hasRole('ROLE_USER')")
    public String secure(ModelMap map){
        return("secure");
    }
}