getting-spring-security.adoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. [[getting]]
  2. = Getting Spring Security
  3. This section describes how to get the Spring Security binaries.
  4. See xref:community.adoc#community-source[Source Code] for how to obtain the source code.
  5. == Release Numbering
  6. Spring Security versions are formatted as MAJOR.MINOR.PATCH such that:
  7. * MAJOR versions may contain breaking changes.
  8. Typically, these are done to provide improved security to match modern security practices.
  9. * MINOR versions contain enhancements but are considered passive updates.
  10. * PATCH level should be perfectly compatible, forwards and backwards, with the possible exception of changes that fix bugs.
  11. [[maven]]
  12. == Usage with Maven
  13. As most open source projects, Spring Security deploys its dependencies as Maven artifacts.
  14. The topics in this section describe how to consume Spring Security when using Maven.
  15. [[getting-maven-boot]]
  16. === Spring Boot with Maven
  17. Spring Boot provides a `spring-boot-starter-security` starter that aggregates Spring Security-related dependencies.
  18. The simplest and preferred way to use the starter is to use https://docs.spring.io/initializr/docs/current/reference/htmlsingle/[Spring Initializr] by using an IDE integration in (https://joshlong.com/jl/blogPost/tech_tip_geting_started_with_spring_boot.html[Eclipse] or https://www.jetbrains.com/help/idea/spring-boot.html#d1489567e2[IntelliJ], https://github.com/AlexFalappa/nb-springboot/wiki/Quick-Tour[NetBeans]) or through https://start.spring.io.
  19. Alternatively, you can manually add the starter, as the following example shows:
  20. .pom.xml
  21. [source,xml,subs="verbatim,attributes"]
  22. ----
  23. <dependencies>
  24. <!-- ... other dependency elements ... -->
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-security</artifactId>
  28. </dependency>
  29. </dependencies>
  30. ----
  31. Since Spring Boot provides a Maven BOM to manage dependency versions, you do not need to specify a version.
  32. If you wish to override the Spring Security version, you can do so by providing a Maven property:
  33. .pom.xml
  34. [source,xml,subs="verbatim,attributes"]
  35. ----
  36. <properties>
  37. <!-- ... -->
  38. <spring-security.version>{spring-security-version}</spring-security.version>
  39. </properties>
  40. ----
  41. Since Spring Security makes breaking changes only in major releases, you can safely use a newer version of Spring Security with Spring Boot.
  42. However, at times, you may need to update the version of Spring Framework as well.
  43. You can do so by adding a Maven property:
  44. .pom.xml
  45. [source,xml,subs="verbatim,attributes"]
  46. ----
  47. <properties>
  48. <!-- ... -->
  49. <spring.version>{spring-core-version}</spring.version>
  50. </properties>
  51. ----
  52. If you use additional features (such as LDAP, OAuth 2, and others), you need to also include the appropriate xref:modules.adoc#modules[Project Modules and Dependencies].
  53. [[getting-maven-no-boot]]
  54. === Maven Without Spring Boot
  55. When you use Spring Security without Spring Boot, the preferred way is to use Spring Security's BOM to ensure that a consistent version of Spring Security is used throughout the entire project. The following example shows how to do so:
  56. .pom.xml
  57. [source,xml,ubs="verbatim,attributes"]
  58. ----
  59. <dependencyManagement>
  60. <dependencies>
  61. <!-- ... other dependency elements ... -->
  62. <dependency>
  63. <groupId>org.springframework.security</groupId>
  64. <artifactId>spring-security-bom</artifactId>
  65. <version>{spring-security-version}</version>
  66. <type>pom</type>
  67. <scope>import</scope>
  68. </dependency>
  69. </dependencies>
  70. </dependencyManagement>
  71. ----
  72. A minimal Spring Security Maven set of dependencies typically looks like the following example:
  73. .pom.xml
  74. [source,xml,subs="verbatim,attributes"]
  75. ----
  76. <dependencies>
  77. <!-- ... other dependency elements ... -->
  78. <dependency>
  79. <groupId>org.springframework.security</groupId>
  80. <artifactId>spring-security-web</artifactId>
  81. </dependency>
  82. <dependency>
  83. <groupId>org.springframework.security</groupId>
  84. <artifactId>spring-security-config</artifactId>
  85. </dependency>
  86. </dependencies>
  87. ----
  88. If you use additional features (such as LDAP, OAuth 2, and others), you need to also include the appropriate xref:modules.adoc#modules[Project Modules and Dependencies].
  89. Spring Security builds against Spring Framework {spring-core-version} but should generally work with any newer version of Spring Framework 5.x.
  90. Many users are likely to run afoul of the fact that Spring Security's transitive dependencies resolve Spring Framework {spring-core-version}, which can cause strange classpath problems.
  91. The easiest way to resolve this is to use the `spring-framework-bom` within the `<dependencyManagement>` section of your `pom.xml`:
  92. .pom.xml
  93. [source,xml,subs="verbatim,attributes"]
  94. ----
  95. <dependencyManagement>
  96. <dependencies>
  97. <!-- ... other dependency elements ... -->
  98. <dependency>
  99. <groupId>org.springframework</groupId>
  100. <artifactId>spring-framework-bom</artifactId>
  101. <version>{spring-core-version}</version>
  102. <type>pom</type>
  103. <scope>import</scope>
  104. </dependency>
  105. </dependencies>
  106. </dependencyManagement>
  107. ----
  108. The preceding example ensures that all the transitive dependencies of Spring Security use the Spring {spring-core-version} modules.
  109. [NOTE]
  110. ====
  111. This approach uses Maven's "`bill of materials`" (BOM) concept and is only available in Maven 2.0.9+.
  112. For additional details about how dependencies are resolved, see https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html[Maven's Introduction to the Dependency Mechanism documentation].
  113. ====
  114. [[maven-repositories]]
  115. === Maven Repositories
  116. All GA releases (that is, versions ending in .RELEASE) are deployed to Maven Central, so you need not declare additional Maven repositories in your pom.
  117. If you use a SNAPSHOT version, you need to ensure that you have the Spring Snapshot repository defined:
  118. .pom.xml
  119. [source,xml]
  120. ----
  121. <repositories>
  122. <!-- ... possibly other repository elements ... -->
  123. <repository>
  124. <id>spring-snapshot</id>
  125. <name>Spring Snapshot Repository</name>
  126. <url>https://repo.spring.io/snapshot</url>
  127. </repository>
  128. </repositories>
  129. ----
  130. If you use a milestone or release candidate version, you need to ensure that you have the Spring Milestone repository defined, as the following example shows:
  131. .pom.xml
  132. [source,xml]
  133. ----
  134. <repositories>
  135. <!-- ... possibly other repository elements ... -->
  136. <repository>
  137. <id>spring-milestone</id>
  138. <name>Spring Milestone Repository</name>
  139. <url>https://repo.spring.io/milestone</url>
  140. </repository>
  141. </repositories>
  142. ----
  143. [[getting-gradle]]
  144. == Gradle
  145. As most open source projects, Spring Security deploys its dependencies as Maven artifacts, which allows for first-class Gradle support.
  146. The following topics describe how to consume Spring Security when using Gradle.
  147. [[getting-gradle-boot]]
  148. === Spring Boot with Gradle
  149. Spring Boot provides a `spring-boot-starter-security` starter that aggregates Spring Security related dependencies.
  150. The simplest and preferred method to use the starter is to use https://docs.spring.io/initializr/docs/current/reference/htmlsingle/[Spring Initializr] by using an IDE integration in (https://joshlong.com/jl/blogPost/tech_tip_geting_started_with_spring_boot.html[Eclipse] or https://www.jetbrains.com/help/idea/spring-boot.html#d1489567e2[IntelliJ], https://github.com/AlexFalappa/nb-springboot/wiki/Quick-Tour[NetBeans]) or through https://start.spring.io.
  151. Alternatively, you can manually add the starter:
  152. .build.gradle
  153. [source,groovy]
  154. [subs="verbatim,attributes"]
  155. ----
  156. dependencies {
  157. compile "org.springframework.boot:spring-boot-starter-security"
  158. }
  159. ----
  160. Since Spring Boot provides a Maven BOM to manage dependency versions, you need not specify a version.
  161. If you wish to override the Spring Security version, you can do so by providing a Gradle property:
  162. .build.gradle
  163. [source,groovy]
  164. [subs="verbatim,attributes"]
  165. ----
  166. ext['spring-security.version']='{spring-security-version}'
  167. ----
  168. Since Spring Security makes breaking changes only in major releases, you can safely use a newer version of Spring Security with Spring Boot.
  169. However, at times, you may need to update the version of Spring Framework as well.
  170. You can do so by adding a Gradle property:
  171. .build.gradle
  172. [source,groovy]
  173. [subs="verbatim,attributes"]
  174. ----
  175. ext['spring.version']='{spring-core-version}'
  176. ----
  177. If you use additional features (such as LDAP, OAuth 2, and others), you need to also include the appropriate xref:modules.adoc#modules[Project Modules and Dependencies].
  178. === Gradle Without Spring Boot
  179. When you use Spring Security without Spring Boot, the preferred way is to use Spring Security's BOM to ensure a consistent version of Spring Security is used throughout the entire project.
  180. You can do so by using the https://github.com/spring-gradle-plugins/dependency-management-plugin[Dependency Management Plugin]:
  181. .build.gradle
  182. [source,groovy]
  183. [subs="verbatim,attributes"]
  184. ----
  185. plugins {
  186. id "io.spring.dependency-management" version "1.0.6.RELEASE"
  187. }
  188. dependencyManagement {
  189. imports {
  190. mavenBom 'org.springframework.security:spring-security-bom:{spring-security-version}'
  191. }
  192. }
  193. ----
  194. A minimal Spring Security Maven set of dependencies typically looks like the following:
  195. .build.gradle
  196. [source,groovy]
  197. [subs="verbatim,attributes"]
  198. ----
  199. dependencies {
  200. compile "org.springframework.security:spring-security-web"
  201. compile "org.springframework.security:spring-security-config"
  202. }
  203. ----
  204. If you use additional features (such as LDAP, OAuth 2, and others), you need to also include the appropriate xref:modules.adoc#modules[Project Modules and Dependencies].
  205. Spring Security builds against Spring Framework {spring-core-version} but should generally work with any newer version of Spring Framework 5.x.
  206. Many users are likely to run afoul of the fact that Spring Security's transitive dependencies resolve Spring Framework {spring-core-version}, which can cause strange classpath problems.
  207. The easiest way to resolve this is to use the `spring-framework-bom` within your `dependencyManagement` section of your `build.gradle`.
  208. You can do so by using the https://github.com/spring-gradle-plugins/dependency-management-plugin[Dependency Management Plugin]:
  209. .build.gradle
  210. [source,groovy]
  211. [subs="verbatim,attributes"]
  212. ----
  213. plugins {
  214. id "io.spring.dependency-management" version "1.0.6.RELEASE"
  215. }
  216. dependencyManagement {
  217. imports {
  218. mavenBom 'org.springframework:spring-framework-bom:{spring-core-version}'
  219. }
  220. }
  221. ----
  222. The preceding example ensures that all the transitive dependencies of Spring Security use the Spring {spring-core-version} modules.
  223. [[gradle-repositories]]
  224. === Gradle Repositories
  225. All GA releases (that is, versions ending in .RELEASE) are deployed to Maven Central, so using the `mavenCentral()` repository is sufficient for GA releases. The following example shows how to do so:
  226. .build.gradle
  227. [source,groovy]
  228. ----
  229. repositories {
  230. mavenCentral()
  231. }
  232. ----
  233. If you use a SNAPSHOT version, you need to ensure that you have the Spring Snapshot repository defined:
  234. .build.gradle
  235. [source,groovy]
  236. ----
  237. repositories {
  238. maven { url 'https://repo.spring.io/snapshot' }
  239. }
  240. ----
  241. If you use a milestone or release candidate version, you need to ensure that you have the Spring Milestone repository defined:
  242. .build.gradle
  243. [source,groovy]
  244. ----
  245. repositories {
  246. maven { url 'https://repo.spring.io/milestone' }
  247. }
  248. ----