[译] 学习 Spring Security(一):Maven

www.baeldung.com/spring-secu…
作者:Eugen Paraschiv
译者:oopsguy.com

1、概述

本文将介绍如何使用 Maven 配置 Spring Security 和介绍使用 Spring Security 依赖的具体用例。最新的 Spring Security 版本可以在 Maven Central 上获取。

2、Spring Security 与 Maven

2.1、spring-security-core

Spring Security 核心支持(spring-security-core)包含身份验证和访问控制功能,并支持独立(非 Web)应用程序、方法级别安全和 JDBC:

<properties>
    <org.springframework.security.version>3.2.3.RELEASE</org.springframework.security.version>
    <org.springframework.version>4.0.4.RELEASE</org.springframework.version>
</properties>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
    <version>${org.springframework.security.version}</version>
</dependency>复制代码

请注意,我们使用了 Spring Security 的 3.2.x.RELEASE 版本 — Spring 和 Spring Security 的发行时间不同,因此他们的版本号不存在对应关系。

如果您使用的是旧版本的 Spring,那么非常重要的一点就是:Spring Security 3.1.x 不依赖于 Spring 3.1.x 发行版!这是因为 Spring Security 3.1.x 早于 Spring 3.1 发布。未来这些依赖的版本将更加靠近对齐 — 有关更多详细信息,请参阅此 JIRA — 但在目前看来,这还是有实际意义的,我们将在下面讨论。

2.2、spring-security-web

要为 Spring Security 添加 Web 支持,需要添加 spring-security-web 依赖:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>${org.springframework.security.version}</version>
</dependency>复制代码

以上包含了过滤器和相关的 Web 安全基础设施,可在 Servlet 环境中启用 URL 访问控制。

2.3、Spring Security 与较早的 Spring Core 依赖问题

这个新依赖跟 Maven 依赖图存在一个问题 — 如上所述,Spring Security jar 不依赖于最新的 Spring core jar(但在之前的版本不是这样)。这可能会导致这些较旧的依赖被使用,而不使用较新的 4.x Spring 工件(artifact)。

为了了解为什么会发生这种情况,我们需要看看 Maven 是如何解决冲突 — 在版本冲突的情况下,Maven 会选择最靠近树根节点的 jar。在我们的例子中,spring-core 由 spring-orm(4.x.RELEASE 版本)定义,也由 spring-security-core(旧的 3.2.8.RELEASE 版本)定义 — 所以在这两种情况下,spring-jdbc 在我们项目的根 pom 定义为深度 1 。因此,在我们自己的 pom 中定义的 spring-orm 和 spring-security-core 是什么使用顺序呢 — 将优先考虑第一个放置,因此我们可能会在 classpath 中使用任一版本。

为了解决这个问题,我们在 pom 中必须明确定义一些 Spring 依赖,而不是依赖于隐式的 Maven 依赖解析机制 — 这样做会使我们 pom 中的特定依赖深度为 0 (因为它本身定义在 pom 中),因此它会优先被考虑。以下所有都属于同一类别,都需要明确定义,对于多模块项目,需要在父级的 dependencyManagement 元素中定义:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${org.springframework.version}</version>
</dependency>复制代码

2.4、spring-security-config 等

要想使用 Spring Security XML 命名空间,需要添加 spring-security-config 依赖:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    <version>${org.springframework.security.version}</version>
    <scope>runtime</scope>
</dependency>复制代码

没有应用程序代码需要到该依赖进行编译,因此它应该作为 runtime 作用域。

LDAP、ACL、CAS 和 OpenID 支持在 Spring Security 中也存在相关的依赖:spring-security-ldap、spring-security-acl、spring-security-cas 和 spring-security-openid。

3、使用 Snapshots 和 Milestones

Spring 在自定义 Maven 仓库中提供了 Spring Security milestones(里程碑版本) 和 snapshots(快照版本),有关如何配置这些内容的更多详细信息,请参阅如何使用 milestones 和 snapshots

4、结论

本文讨论了使用 Spring Security 与 Maven 的细节。这里提到的 Maven 依赖只是一些主要的常用依赖。但这也为您在 Maven 项目中使用 Spring 提供了很好的起点。

文中相关链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值