getting-spring-security.adoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. [[get-spring-security]]
  2. = Getting Spring Security
  3. This section discusses all you need to know about getting the Spring Security binaries.
  4. Please refer to <<community-source>> 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 which are to fix bugs
  11. [[maven]]
  12. == Usage with Maven
  13. Like most open source projects, Spring Security deploys its dependencies as Maven artifacts.
  14. The following sections provide details on how to consume Spring Security when using Maven.
  15. === Spring Boot with Maven
  16. Spring Boot provides a spring-boot-starter-security starter which aggregates Spring Security related dependencies together.
  17. The simplest and preferred method to leverage the starter is to use https://docs.spring.io/initializr/docs/current/reference/htmlsingle/[Spring Initializr] using an IDE integration (http://joshlong.com/jl/blogPost/tech_tip_geting_started_with_spring_boot.html[Eclipse], 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.
  18. Alternatively, the starter can be added manually:
  19. .pom.xml
  20. [source,xml]
  21. [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, there is no need to specify a version.
  32. If you wish to override the Spring Security version, you may do so by providing a Maven property:
  33. .pom.xml
  34. [source,xml]
  35. [subs="verbatim,attributes"]
  36. ----
  37. <properties>
  38. <!-- ... -->
  39. <spring-security.version>{spring-security-version}</spring.security.version>
  40. </dependencies>
  41. ----
  42. Since Spring Security only makes breaking changes in major releases, it is safe to use a newer version of Spring Security with Spring Boot.
  43. However, at times it may be necessary to update the version of Spring Framework as well.
  44. This can easily be done by adding a Maven property as well:
  45. .pom.xml
  46. [source,xml]
  47. [subs="verbatim,attributes"]
  48. ----
  49. <properties>
  50. <!-- ... -->
  51. <spring.version>{spring-version}</spring.version>
  52. </dependencies>
  53. ----
  54. If you are using additional features like LDAP, OpenID, etc. you will need to also include the appropriate <<modules>>.
  55. === Maven Without Spring Boot
  56. When using Spring Security without Spring Boot, the preferred way is to leverage Spring Security's BOM to ensure a consistent version of Spring Security is used throughout the entire project.
  57. .pom.xml
  58. [source,xml]
  59. [subs="verbatim,attributes"]
  60. ----
  61. <dependencyManagement>
  62. <dependencies>
  63. <!-- ... other dependency elements ... -->
  64. <dependency>
  65. <groupId>org.springframework.security</groupId>
  66. <artifactId>spring-security-bom</artifactId>
  67. <version>{spring-security-version}</version>
  68. <type>pom</type>
  69. <scope>import</scope>
  70. </dependency>
  71. </dependencies>
  72. </dependencyManagement>
  73. ----
  74. A minimal Spring Security Maven set of dependencies typically looks like the following:
  75. .pom.xml
  76. [source,xml]
  77. [subs="verbatim,attributes"]
  78. ----
  79. <dependencies>
  80. <!-- ... other dependency elements ... -->
  81. <dependency>
  82. <groupId>org.springframework.security</groupId>
  83. <artifactId>spring-security-web</artifactId>
  84. </dependency>
  85. <dependency>
  86. <groupId>org.springframework.security</groupId>
  87. <artifactId>spring-security-config</artifactId>
  88. </dependency>
  89. </dependencies>
  90. ----
  91. If you are using additional features like LDAP, OpenID, etc. you will need to also include the appropriate <<modules>>.
  92. Spring Security builds against Spring Framework {spring-version}, but should generally work with any newer version of Spring Framework 5.x
  93. The problem that many users will have is that Spring Security's transitive dependencies resolve Spring Framework {spring-version} which can cause strange classpath problems.
  94. The easiest way to resolve this is to use the `spring-framework-bom` within your `<dependencyManagement>` section of your `pom.xml` as shown below:
  95. .pom.xml
  96. [source,xml]
  97. [subs="verbatim,attributes"]
  98. ----
  99. <dependencyManagement>
  100. <dependencies>
  101. <!-- ... other dependency elements ... -->
  102. <dependency>
  103. <groupId>org.springframework</groupId>
  104. <artifactId>spring-framework-bom</artifactId>
  105. <version>{spring-version}</version>
  106. <type>pom</type>
  107. <scope>import</scope>
  108. </dependency>
  109. </dependencies>
  110. </dependencyManagement>
  111. ----
  112. This will ensure that all the transitive dependencies of Spring Security use the Spring {spring-version} modules.
  113. NOTE: This approach uses Maven's "bill of materials" (BOM) concept and is only available in Maven 2.0.9+.
  114. For additional details about how dependencies are resolved refer to http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html[Maven's Introduction to the Dependency Mechanism documentation].
  115. [[maven-repositories]]
  116. === Maven Repositories
  117. All GA releases (i.e. versions ending in .RELEASE) are deployed to Maven Central, so no additional Maven repositories need to be declared in your pom.
  118. If you are using a SNAPSHOT version, you will need to ensure you have the Spring Snapshot repository defined as shown below:
  119. .pom.xml
  120. [source,xml]
  121. ----
  122. <repositories>
  123. <!-- ... possibly other repository elements ... -->
  124. <repository>
  125. <id>spring-snapshot</id>
  126. <name>Spring Snapshot Repository</name>
  127. <url>https://repo.spring.io/snapshot</url>
  128. </repository>
  129. </repositories>
  130. ----
  131. If you are using a milestone or release candidate version, you will need to ensure you have the Spring Milestone repository defined as shown below:
  132. .pom.xml
  133. [source,xml]
  134. ----
  135. <repositories>
  136. <!-- ... possibly other repository elements ... -->
  137. <repository>
  138. <id>spring-milestone</id>
  139. <name>Spring Milestone Repository</name>
  140. <url>https://repo.spring.io/milestone</url>
  141. </repository>
  142. </repositories>
  143. ----
  144. [[gradle]]
  145. == Gradle
  146. Like most open source projects, Spring Security deploys its dependencies as Maven artifacts which allows for for first class Gradle support.
  147. The following sections provide details on how to consume Spring Security when using Gradle.
  148. === Spring Boot with Gradle
  149. Spring Boot provides a spring-boot-starter-security starter which aggregates Spring Security related dependencies together.
  150. The simplest and preferred method to leverage the starter is to use https://docs.spring.io/initializr/docs/current/reference/htmlsingle/[Spring Initializr] using an IDE integration (http://joshlong.com/jl/blogPost/tech_tip_geting_started_with_spring_boot.html[Eclipse], 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, the starter can be added manually:
  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, there is no need to specify a version.
  161. If you wish to override the Spring Security version, you may 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 only makes breaking changes in major releases, it is safe to use a newer version of Spring Security with Spring Boot.
  169. However, at times it may be necessary to update the version of Spring Framework as well.
  170. This can easily be done by adding a Gradle property as well:
  171. .build.gradle
  172. [source,groovy]
  173. [subs="verbatim,attributes"]
  174. ----
  175. ext['spring.version']='{spring-version}'
  176. ----
  177. If you are using additional features like LDAP, OpenID, etc. you will need to also include the appropriate <<modules>>.
  178. === Gradle Without Spring Boot
  179. When using Spring Security without Spring Boot, the preferred way is to leverage Spring Security's BOM to ensure a consistent version of Spring Security is used throughout the entire project.
  180. This can be done 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 are using additional features like LDAP, OpenID, etc. you will need to also include the appropriate <<modules>>.
  205. Spring Security builds against Spring Framework {spring-version}, but should generally work with any newer version of Spring Framework 5.x
  206. The problem that many users will have is that Spring Security's transitive dependencies resolve Spring Framework {spring-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 `pom.xml` as shown below:
  208. This can be done 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-version}'
  219. }
  220. }
  221. ----
  222. This will ensure that all the transitive dependencies of Spring Security use the Spring {spring-version} modules.
  223. [[gradle-repositories]]
  224. === Gradle Repositories
  225. All GA releases (i.e. versions ending in .RELEASE) are deployed to Maven Central, so using the mavenCentral() repository is sufficient for GA releases.
  226. .build.gradle
  227. [source,groovy]
  228. ----
  229. repositories {
  230. mavenCentral()
  231. }
  232. ----
  233. If you are using a SNAPSHOT version, you will need to ensure you have the Spring Snapshot repository defined as shown below:
  234. .build.gradle
  235. [source,groovy]
  236. ----
  237. repositories {
  238. maven { url 'https://repo.spring.io/snapshot' }
  239. }
  240. ----
  241. If you are using a milestone or release candidate version, you will need to ensure you have the Spring Milestone repository defined as shown below:
  242. .build.gradle
  243. [source,groovy]
  244. ----
  245. repositories {
  246. maven { url 'https://repo.spring.io/milestone' }
  247. }
  248. ----