2010-03-26 57 views
2

我试图获得使用Web应用程序的前/后注释,但由于某种原因,Spring-Security没有发生任何事情。Spring Security未处理前/后注释

的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> 

rvaContext-security.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.0.xsd"> 

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

<http use-expressions="true"> 
    <form-login login-page="/login" /> 
    <logout /> 
    <remember-me /> 
</http> 
... 

的LoginController类:

@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"); 
    } 
} 

回答

7

要启用控制器secuity注释,你应该在声明<security:global-method-security .../>声明控制器的上下文,即rva-servlet.xml

+0

你为我节省了几个小时的有用性调试......谢谢 – antoine 2015-04-27 13:41:40

1

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

0

请参阅Spring Security FAQ(强调我的)。

在一个Spring web应用程序,其保持 Spring MVC的豆用于调度的servlet应用上下文是常从 主应用程序上下文分离。它通常在名为 myapp-servlet.xml的文件中定义,其中“myapp”是分配给web.xml中的Spring DispatcherServlet的名称。一个应用程序可以有多个 DispatcherServlet,每个都有自己独立的应用程序上下文。 这些“子”上下文中的bean对于 应用程序的其余部分不可见。 “父”应用程序上下文由您在web.xml中定义的ContextLoaderListener加载,并且所有 子上下文都可见。此父上下文通常是您定义 安全配置的位置,包括 元素)。因此,应用于 中的方法的任何安全限制都将不会被强制执行,因为从DispatcherServlet上下文中无法看到该Bean的 。您需要将 声明移至Web上下文,或将需要保护的bean移至主应用程序上下文中。

通常我们会建议在服务层 上应用方法安全性,而不是在单独的Web控制器上。

如果你申请的切入点,以服务层,你只需要设置<global-method-security>在应用程序的安全上下文。