Spring Security + MVC:关于上下文定义和Bean范围的问题

11 浏览
0 Comments

Spring Security + MVC:关于上下文定义和Bean范围的问题

我正在尝试理解在Spring-MVC应用程序中定义Spring Security的推荐方法,其中bean定义在多个父/子上下文中分割。

例如,我当前应用程序的web.xml如下所示(我理解这是相当标准的):


    contextConfigLocation
    
    classpath:applicationContext.xml
    /WEB-INF/securityContext.xml
    


    org.springframework.web.context.ContextLoaderListener


    springSecurityFilterChain
    org.springframework.web.filter.DelegatingFilterProxy


    springSecurityFilterChain
    /*


    spring-mvc
    org.springframework.web.servlet.DispatcherServlet
    1


    spring-mvc
    /app/*

所以,我在/定义了一个标准的ContextLoaderListener,它加载了我的全局配置- applicationContext.xmlsecurityContext.xml

我还在/app/定义了spring mvc DispatcherServlet,它从spring-mvc-servlet.xml加载自己的bean。

据我所知,spring-mvc-servlet.xml中定义的配置对于顶级上下文文件中定义的配置是不可见的。

那么,最好在哪里定义应用程序级别的安全概念?例如,我想添加以下过滤器。


    

这样,对于/app/oauth/token的请求将通过此过滤器,并进行基本身份验证处理。

因为这直接涉及到Spring-MVC应用程序的问题,所以我最初在spring-mvc-context.xml中定义了它(这就是为什么url中不包含app的原因)。

然而,这意味着它对于securityContext.xml中定义的安全配置不可见,因此被忽略。

所以,我将它移到了securityContext.xml中,但这样做也必须移动所有的依赖项。

我很快就将所有内容移到了applicationContext.xml中,这样spring-mvc-context.xml几乎变成了空的。

这是常见的吗?在顶级上下文中定义了什么,以及在子上下文中定义了什么的推荐分割是什么?

考虑到spring-mvc定义了一系列的控制器,我想将其标记为@Secured,如果控制器对安全上下文不可见,这些控制器将如何处理?

我需要将我的servlet.xml移动到全局applicationContext.xml吗?

我需要在spring-mvc-servlet.xml中进行其他配置,以告诉它参与Spring Security吗?

我已阅读过关于Spring-MVC的文档,但对如何配置这个问题的具体内容非常少。

此外,Spring OAuth示例似乎在单个配置文件中定义了所有内容,这似乎不太现实,并且似乎与我阅读过的其他示例相矛盾。

0
0 Comments

在Spring Security和Spring MVC中,出现了有关上下文定义和bean作用域的问题。问题的原因是在applicationContext.xml(由ContextLoaderListener定义)中定义的bean无法访问spring-mvc-servlet.xml(由DispatcherServlet定义)中定义的bean,但反过来是可以的。

问题1:如果控制器对安全上下文不可见,那么在Spring MVC中定义的一系列控制器(需要标记为@Controller)将如何处理?

这个问题没有问题,因为控制器必须在spring-mvc-servlet.xml中定义,所以它们可以“看到”在applicationContext.xml中定义的Spring Security相关内容。

问题2:我需要将从servlet.xml移动到全局的applicationContext.xml吗?

不需要。

问题3:我需要在spring-mvc-servlet.xml中进行其他配置来告诉它参与Spring Security吗?

不需要。

问题4:... 这样会使得spring-mvc-context.xml几乎为空。这种情况常见吗?

spring-mvc-context.xml应该包含与Web相关的所有内容(除了安全性)。所以spring-mvc-context.xml的常见部分包括组件扫描、一些拦截器、资源处理、默认Servlet处理、视图控制器、可重载资源绑定消息源ReloadableResourceBundleMessageSource、Cookie区域解析器CookieLocaleResolver、简单的映射异常解析器SimpleMappingExceptionResolver等。

另外还有一个相关问题可以参考:ContextLoaderListener or not? 这个问题从另一个角度讨论了这个主题。

0