getting-started.adoc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. [[oauth2AuthorizationServer-getting-started]]
  2. = Getting Started
  3. If you are just getting started with Spring Security Authorization Server, the following sections walk you through creating your first application.
  4. [[oauth2AuthorizationServer-system-requirements]]
  5. == System Requirements
  6. Spring Security Authorization Server requires a Java 17 or higher Runtime Environment.
  7. [[oauth2AuthorizationServer-installing-spring-security-authorization-server]]
  8. == Installing Spring Security Authorization Server
  9. The easiest way to begin using Spring Security Authorization Server is by creating a https://spring.io/projects/spring-boot[Spring Boot]-based application.
  10. You can use https://start.spring.io[start.spring.io] to generate a basic project or use the https://github.com/spring-projects/spring-authorization-server/tree/main/samples/default-authorizationserver[default authorization server sample] as a guide.
  11. Then add Spring Boot's starter for Spring Security Authorization Server as a dependency:
  12. [tabs]
  13. ======
  14. Maven::
  15. +
  16. [[oauth2AuthorizationServer-spring-boot-maven-dependency]]
  17. [source,xml,role="primary",subs="attributes,verbatim"]
  18. ----
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-oauth2-authorization-server</artifactId>
  22. </dependency>
  23. ----
  24. Gradle::
  25. +
  26. [[oauth2AuthorizationServer-spring-boot-gradle-dependency]]
  27. [source,gradle,role="secondary",subs="attributes,verbatim"]
  28. ----
  29. implementation "org.springframework.boot:spring-boot-starter-oauth2-authorization-server"
  30. ----
  31. ======
  32. TIP: See https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.installing[Installing Spring Boot] for more information on using Spring Boot with Maven or Gradle.
  33. Alternatively, you can add Spring Security Authorization Server without Spring Boot using the following example:
  34. [tabs]
  35. ======
  36. Maven::
  37. +
  38. [[oauth2AuthorizationServer-maven-dependency]]
  39. [source,xml,role="primary",subs="attributes,verbatim"]
  40. ----
  41. <dependency>
  42. <groupId>org.springframework.security</groupId>
  43. <artifactId>spring-security-oauth2-authorization-server</artifactId>
  44. <version>{spring-security-version}</version>
  45. </dependency>
  46. ----
  47. Gradle::
  48. +
  49. [[oauth2AuthorizationServer-gradle-dependency]]
  50. [source,gradle,role="secondary",subs="attributes,verbatim"]
  51. ----
  52. implementation "org.springframework.security:spring-security-oauth2-authorization-server:{spring-security-version}"
  53. ----
  54. ======
  55. [[oauth2AuthorizationServer-developing-your-first-application]]
  56. == Developing Your First Application
  57. To get started, you need the minimum required components defined as a `@Bean`. When using the `spring-boot-starter-oauth2-authorization-server` dependency, define the following properties and Spring Boot will provide the necessary `@Bean` definitions for you:
  58. [[oauth2AuthorizationServer-application-yml]]
  59. .application.yml
  60. [source,yaml]
  61. ----
  62. server:
  63. port: 9000
  64. logging:
  65. level:
  66. org.springframework.security: trace
  67. spring:
  68. security:
  69. user:
  70. name: user
  71. password: password
  72. oauth2:
  73. authorizationserver:
  74. client:
  75. oidc-client:
  76. registration:
  77. client-id: "oidc-client"
  78. client-secret: "{noop}secret"
  79. client-authentication-methods:
  80. - "client_secret_basic"
  81. authorization-grant-types:
  82. - "authorization_code"
  83. - "refresh_token"
  84. redirect-uris:
  85. - "http://127.0.0.1:8080/login/oauth2/code/oidc-client"
  86. post-logout-redirect-uris:
  87. - "http://127.0.0.1:8080/"
  88. scopes:
  89. - "openid"
  90. - "profile"
  91. require-authorization-consent: true
  92. ----
  93. If you want to customize the default `HttpSecurity` configuration, you may override Spring Boot's auto-configuration with the following example:
  94. [[oauth2AuthorizationServer-minimal-sample-gettingstarted]]
  95. .SecurityConfig.java
  96. [source,java]
  97. ----
  98. @Configuration
  99. @EnableWebSecurity
  100. public class SecurityConfig {
  101. @Bean
  102. public SecurityFilterChain securityFilterChain(HttpSecurity http) {
  103. http
  104. .authorizeHttpRequests((authorize) ->
  105. authorize
  106. .anyRequest().authenticated()
  107. )
  108. .formLogin(Customizer.withDefaults())
  109. .oauth2AuthorizationServer((authorizationServer) ->
  110. authorizationServer
  111. .oidc(Customizer.withDefaults()) // Enable OpenID Connect 1.0
  112. );
  113. return http.build();
  114. }
  115. }
  116. ----
  117. TIP: Beyond the Getting Started experience, most users will want to customize the default configuration. The xref:servlet/oauth2/authorization-server/getting-started.adoc#oauth2AuthorizationServer-defining-required-components[next section] demonstrates providing all of the necessary beans yourself.
  118. [[oauth2AuthorizationServer-defining-required-components]]
  119. == Defining Required Components
  120. If you want to customize the default configuration (regardless of whether you're using Spring Boot), you can define the minimum required components as a `@Bean` in a Spring `@Configuration`.
  121. These components can be defined as follows:
  122. [[oauth2AuthorizationServer-sample-gettingstarted]]
  123. .SecurityConfig.java
  124. [source,java]
  125. ----
  126. @Configuration
  127. @EnableWebSecurity
  128. public class SecurityConfig {
  129. @Bean // <1>
  130. @Order(1)
  131. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http)
  132. throws Exception {
  133. // @formatter:off
  134. http
  135. .oauth2AuthorizationServer((authorizationServer) -> {
  136. http.securityMatcher(authorizationServer.getEndpointsMatcher());
  137. authorizationServer
  138. .oidc(Customizer.withDefaults()); // Enable OpenID Connect 1.0
  139. })
  140. .authorizeHttpRequests((authorize) ->
  141. authorize
  142. .anyRequest().authenticated()
  143. )
  144. // Redirect to the login page when not authenticated from the
  145. // authorization endpoint
  146. .exceptionHandling((exceptions) -> exceptions
  147. .defaultAuthenticationEntryPointFor(
  148. new LoginUrlAuthenticationEntryPoint("/login"),
  149. new MediaTypeRequestMatcher(MediaType.TEXT_HTML)
  150. )
  151. );
  152. // @formatter:on
  153. return http.build();
  154. }
  155. @Bean // <2>
  156. @Order(2)
  157. public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http)
  158. throws Exception {
  159. // @formatter:off
  160. http
  161. .authorizeHttpRequests((authorize) -> authorize
  162. .anyRequest().authenticated()
  163. )
  164. // Form login handles the redirect to the login page from the
  165. // authorization server filter chain
  166. .formLogin(Customizer.withDefaults());
  167. // @formatter:on
  168. return http.build();
  169. }
  170. @Bean // <3>
  171. public UserDetailsService userDetailsService() {
  172. // @formatter:off
  173. UserDetails userDetails = User.withDefaultPasswordEncoder()
  174. .username("user")
  175. .password("password")
  176. .roles("USER")
  177. .build();
  178. // @formatter:on
  179. return new InMemoryUserDetailsManager(userDetails);
  180. }
  181. @Bean // <4>
  182. public RegisteredClientRepository registeredClientRepository() {
  183. // @formatter:off
  184. RegisteredClient oidcClient = RegisteredClient.withId(UUID.randomUUID().toString())
  185. .clientId("oidc-client")
  186. .clientSecret("{noop}secret")
  187. .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
  188. .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
  189. .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
  190. .redirectUri("http://127.0.0.1:8080/login/oauth2/code/oidc-client")
  191. .postLogoutRedirectUri("http://127.0.0.1:8080/")
  192. .scope(OidcScopes.OPENID)
  193. .scope(OidcScopes.PROFILE)
  194. .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
  195. .build();
  196. // @formatter:on
  197. return new InMemoryRegisteredClientRepository(oidcClient);
  198. }
  199. @Bean // <5>
  200. public JWKSource<SecurityContext> jwkSource() {
  201. KeyPair keyPair = generateRsaKey();
  202. RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
  203. RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
  204. // @formatter:off
  205. RSAKey rsaKey = new RSAKey.Builder(publicKey)
  206. .privateKey(privateKey)
  207. .keyID(UUID.randomUUID().toString())
  208. .build();
  209. // @formatter:on
  210. JWKSet jwkSet = new JWKSet(rsaKey);
  211. return new ImmutableJWKSet<>(jwkSet);
  212. }
  213. private static KeyPair generateRsaKey() { // <6>
  214. KeyPair keyPair;
  215. try {
  216. KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
  217. keyPairGenerator.initialize(2048);
  218. keyPair = keyPairGenerator.generateKeyPair();
  219. }
  220. catch (Exception ex) {
  221. throw new IllegalStateException(ex);
  222. }
  223. return keyPair;
  224. }
  225. @Bean // <7>
  226. public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
  227. return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
  228. }
  229. @Bean // <8>
  230. public AuthorizationServerSettings authorizationServerSettings() {
  231. return AuthorizationServerSettings.builder().build();
  232. }
  233. }
  234. ----
  235. This is a minimal configuration for getting started quickly. To understand what each component is used for, see the following descriptions:
  236. <1> A Spring Security filter chain for the xref:servlet/oauth2/authorization-server/protocol-endpoints.adoc[Protocol Endpoints].
  237. <2> A Spring Security filter chain for xref:servlet/authentication/index.adoc#servlet-authentication[authentication].
  238. <3> An instance of {security-api-url}/org/springframework/security/core/userdetails/UserDetailsService.html[`UserDetailsService`] for retrieving users to authenticate.
  239. <4> An instance of xref:servlet/oauth2/authorization-server/core-model-components.adoc#oauth2AuthorizationServer-registered-client-repository[`RegisteredClientRepository`] for managing clients.
  240. <5> An instance of `com.nimbusds.jose.jwk.source.JWKSource` for signing access tokens.
  241. <6> An instance of `java.security.KeyPair` with keys generated on startup used to create the `JWKSource` above.
  242. <7> An instance of {security-api-url}/org/springframework/security/oauth2/jwt/JwtDecoder.html[`JwtDecoder`] for decoding signed access tokens.
  243. <8> An instance of xref:servlet/oauth2/authorization-server/configuration-model.adoc#oauth2AuthorizationServer-configuring-authorization-server-settings[`AuthorizationServerSettings`] to configure Spring Security Authorization Server.