【问题标题】:Integrating security to Spring MVC application (applicationContext Issue)将安全性集成到 Spring MVC 应用程序(applicationContext 问题)
【发布时间】:2012-02-20 10:27:32
【问题描述】:

我已经构建了 Spring MVC 应用程序并将其提升到高级水平。

现在我的任务是将 Spring Security 集成到其中。我现在关注this tutorial 以获取春季安全。现在的问题是,在我的 MVC 应用程序中,已经有一个 applicationContext.xml,它是 MVC 应用程序的父应用程序上下文定义。

正如安全教程中提到的,我是否必须为安全添加一个新的应用程序上下文 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-2.5.xsd
                        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">

    <global-method-security secured-annotations="disabled">
    </global-method-security>

    <http auto-config="true">
        <intercept-url pattern="/**" access="ROLE_USER" />
    </http>

    <authentication-provider>
        <user-service id="userDetailsService">
            <user name="username" password="password" authorities="ROLE_USER, ROLE_ADMIN" />
            <user name="test" password="test" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</beans:beans>

提前致谢。

附: / 编辑:还是应该将其复制到我的 MVC 应用程序的原始/现有 applicationContext.xml 文件中?

【问题讨论】:

    标签: xml spring spring-security


    【解决方案1】:

    与您的其他上下文没有问题或冲突。 例如,您可以将安全配置写入 context/applicationContext-security.xml,然后从 web.xml 加载所有上下文文件。

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:context/applicationContext*.xml</param-value>
        </context-param>
    

    【讨论】:

    • 非常感谢。我还有一个问题,这是我的 MVC 应用程序的 web.xml 中的一个 taglib。知道这是干什么用的吗?
    • 1) 你在说什么标签库? 2) 正如 Ralph 所指出的,本教程已过时,您应该使用 Spring 3.0 的教程。
    【解决方案2】:

    最好的做法是为普通 bean 配置和另一个用于安全性的 spring 配置文件分开。

    但是名字并不重要! 您可以命名一个,例如 applicationContext.xml,然后另一个命名为 applicationContext-security.xml

    顺便说一句:本教程是 Spring 2 教程,您应该看看使用 Spring 3.0 和 Spring Security 3.0 的教程! -- Spring Security 3.0 与 Spring Security 2.0 有很大不同!

    【讨论】:

    • 非常感谢您的好意。
    • 这是否意味着 spring 2 与 spring security 3 不兼容?如果我使用 Spring 2.0.4 而不是 v 3.0 会怎样?
    • @AbdulAziz:这不是一个真正的问题,对,你已经知道答案了。
    • 对不起。我明白你的意思了。这是因为我完成任务的时间非常有限,我很担心。谢谢
    猜你喜欢
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 2021-04-01
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 2021-11-19
    相关资源
    最近更新 更多