Skip to content

Commit e99ea03

Browse files
committed
Integrate Spring Authorization Server ref docs
Issue gh-17880
1 parent 93742a4 commit e99ea03

File tree

9 files changed

+417
-308
lines changed

9 files changed

+417
-308
lines changed

docs/modules/ROOT/nav.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@
8989
**** xref:servlet/oauth2/resource-server/multitenancy.adoc[Multitenancy]
9090
**** xref:servlet/oauth2/resource-server/bearer-tokens.adoc[Bearer Tokens]
9191
**** xref:servlet/oauth2/resource-server/dpop-tokens.adoc[DPoP-bound Access Tokens]
92+
*** xref:servlet/oauth2/authorization-server/index.adoc[OAuth2 Authorization Server]
93+
**** xref:servlet/oauth2/authorization-server/getting-started.adoc[Getting Started]
94+
**** xref:servlet/oauth2/authorization-server/configuration-model.adoc[Configuration Model]
95+
**** xref:servlet/oauth2/authorization-server/core-model-components.adoc[Core Model / Components]
96+
**** xref:servlet/oauth2/authorization-server/protocol-endpoints.adoc[Protocol Endpoints]
9297
** xref:servlet/saml2/index.adoc[SAML2]
9398
*** xref:servlet/saml2/login/index.adoc[SAML2 Log In]
9499
**** xref:servlet/saml2/login/overview.adoc[SAML2 Log In Overview]

docs/modules/ROOT/pages/servlet/oauth2/authorization-server/configuration-model.adoc

Lines changed: 47 additions & 47 deletions
Large diffs are not rendered by default.

docs/modules/ROOT/pages/servlet/oauth2/authorization-server/core-model-components.adoc

Lines changed: 39 additions & 42 deletions
Large diffs are not rendered by default.

docs/modules/ROOT/pages/servlet/oauth2/authorization-server/getting-help.adoc

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 182 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11

2-
[[getting-started]]
2+
[[oauth2AuthorizationServer-getting-started]]
33
= Getting Started
44

5-
If you are just getting started with Spring Authorization Server, the following sections walk you through creating your first application.
5+
If you are just getting started with Spring Security Authorization Server, the following sections walk you through creating your first application.
66

7-
[[system-requirements]]
7+
[[oauth2AuthorizationServer-system-requirements]]
88
== System Requirements
99

10-
Spring Authorization Server requires a Java 17 or higher Runtime Environment.
10+
Spring Security Authorization Server requires a Java 17 or higher Runtime Environment.
1111

12-
[[installing-spring-authorization-server]]
13-
== Installing Spring Authorization Server
12+
[[oauth2AuthorizationServer-installing-spring-security-authorization-server]]
13+
== Installing Spring Security Authorization Server
1414

15-
Spring Authorization Server can be used anywhere you already use https://docs.spring.io/spring-security/reference/prerequisites.html[Spring Security].
16-
17-
The easiest way to begin using Spring Authorization Server is by creating a https://spring.io/projects/spring-boot[Spring Boot]-based application.
15+
The easiest way to begin using Spring Security Authorization Server is by creating a https://spring.io/projects/spring-boot[Spring Boot]-based application.
1816
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.
19-
Then add Spring Boot's starter for Spring Authorization Server as a dependency:
17+
Then add Spring Boot's starter for Spring Security Authorization Server as a dependency:
2018

2119
[tabs]
2220
======
2321
Maven::
2422
+
25-
[[spring-boot-maven-dependency]]
23+
[[oauth2AuthorizationServer-spring-boot-maven-dependency]]
2624
[source,xml,role="primary",subs="attributes,verbatim"]
2725
----
2826
<dependency>
@@ -33,7 +31,7 @@ Maven::
3331
3432
Gradle::
3533
+
36-
[[spring-boot-gradle-dependency]]
34+
[[oauth2AuthorizationServer-spring-boot-gradle-dependency]]
3735
[source,gradle,role="secondary",subs="attributes,verbatim"]
3836
----
3937
implementation "org.springframework.boot:spring-boot-starter-oauth2-authorization-server"
@@ -42,66 +40,220 @@ implementation "org.springframework.boot:spring-boot-starter-oauth2-authorizatio
4240

4341
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.
4442

45-
Alternatively, you can add Spring Authorization Server without Spring Boot using the following example:
43+
Alternatively, you can add Spring Security Authorization Server without Spring Boot using the following example:
4644

4745
[tabs]
4846
======
4947
Maven::
5048
+
51-
[[maven-dependency]]
49+
[[oauth2AuthorizationServer-maven-dependency]]
5250
[source,xml,role="primary",subs="attributes,verbatim"]
5351
----
5452
<dependency>
5553
<groupId>org.springframework.security</groupId>
5654
<artifactId>spring-security-oauth2-authorization-server</artifactId>
57-
<version>{spring-authorization-server-version}</version>
55+
<version>{spring-security-version}</version>
5856
</dependency>
5957
----
6058
6159
Gradle::
6260
+
63-
[[gradle-dependency]]
61+
[[oauth2AuthorizationServer-gradle-dependency]]
6462
[source,gradle,role="secondary",subs="attributes,verbatim"]
6563
----
66-
implementation "org.springframework.security:spring-security-oauth2-authorization-server:{spring-authorization-server-version}"
64+
implementation "org.springframework.security:spring-security-oauth2-authorization-server:{spring-security-version}"
6765
----
6866
======
6967

70-
[[developing-your-first-application]]
68+
[[oauth2AuthorizationServer-developing-your-first-application]]
7169
== Developing Your First Application
7270

7371
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:
7472

75-
[[application-yml]]
73+
[[oauth2AuthorizationServer-application-yml]]
7674
.application.yml
7775
[source,yaml]
7876
----
79-
include::{docs-java}/sample/gettingstarted/application.yml[]
77+
server:
78+
port: 9000
79+
80+
logging:
81+
level:
82+
org.springframework.security: trace
83+
84+
spring:
85+
security:
86+
user:
87+
name: user
88+
password: password
89+
oauth2:
90+
authorizationserver:
91+
client:
92+
oidc-client:
93+
registration:
94+
client-id: "oidc-client"
95+
client-secret: "{noop}secret"
96+
client-authentication-methods:
97+
- "client_secret_basic"
98+
authorization-grant-types:
99+
- "authorization_code"
100+
- "refresh_token"
101+
redirect-uris:
102+
- "http://127.0.0.1:8080/login/oauth2/code/oidc-client"
103+
post-logout-redirect-uris:
104+
- "http://127.0.0.1:8080/"
105+
scopes:
106+
- "openid"
107+
- "profile"
108+
require-authorization-consent: true
80109
----
81110

82-
TIP: Beyond the Getting Started experience, most users will want to customize the default configuration. The xref:getting-started.adoc#defining-required-components[next section] demonstrates providing all of the necessary beans yourself.
111+
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.
83112

84-
[[defining-required-components]]
113+
[[oauth2AuthorizationServer-defining-required-components]]
85114
== Defining Required Components
86115

87116
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`.
88117

89118
These components can be defined as follows:
90119

91-
[[sample.gettingstarted]]
120+
[[oauth2AuthorizationServer-sample-gettingstarted]]
92121
.SecurityConfig.java
93122
[source,java]
94123
----
95-
include::{docs-java}/sample/gettingstarted/SecurityConfig.java[]
124+
@Configuration
125+
@EnableWebSecurity
126+
public class SecurityConfig {
127+
128+
@Bean // <1>
129+
@Order(1)
130+
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http)
131+
throws Exception {
132+
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
133+
OAuth2AuthorizationServerConfigurer.authorizationServer();
134+
135+
// @formatter:off
136+
http
137+
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
138+
.with(authorizationServerConfigurer, (authorizationServer) ->
139+
authorizationServer
140+
.oidc(Customizer.withDefaults()) // Enable OpenID Connect 1.0
141+
)
142+
.authorizeHttpRequests((authorize) ->
143+
authorize
144+
.anyRequest().authenticated()
145+
)
146+
// Redirect to the login page when not authenticated from the
147+
// authorization endpoint
148+
.exceptionHandling((exceptions) -> exceptions
149+
.defaultAuthenticationEntryPointFor(
150+
new LoginUrlAuthenticationEntryPoint("/login"),
151+
new MediaTypeRequestMatcher(MediaType.TEXT_HTML)
152+
)
153+
);
154+
// @formatter:on
155+
156+
return http.build();
157+
}
158+
159+
@Bean // <2>
160+
@Order(2)
161+
public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http)
162+
throws Exception {
163+
// @formatter:off
164+
http
165+
.authorizeHttpRequests((authorize) -> authorize
166+
.anyRequest().authenticated()
167+
)
168+
// Form login handles the redirect to the login page from the
169+
// authorization server filter chain
170+
.formLogin(Customizer.withDefaults());
171+
// @formatter:on
172+
173+
return http.build();
174+
}
175+
176+
@Bean // <3>
177+
public UserDetailsService userDetailsService() {
178+
// @formatter:off
179+
UserDetails userDetails = User.withDefaultPasswordEncoder()
180+
.username("user")
181+
.password("password")
182+
.roles("USER")
183+
.build();
184+
// @formatter:on
185+
186+
return new InMemoryUserDetailsManager(userDetails);
187+
}
188+
189+
@Bean // <4>
190+
public RegisteredClientRepository registeredClientRepository() {
191+
// @formatter:off
192+
RegisteredClient oidcClient = RegisteredClient.withId(UUID.randomUUID().toString())
193+
.clientId("oidc-client")
194+
.clientSecret("{noop}secret")
195+
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
196+
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
197+
.authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
198+
.redirectUri("http://127.0.0.1:8080/login/oauth2/code/oidc-client")
199+
.postLogoutRedirectUri("http://127.0.0.1:8080/")
200+
.scope(OidcScopes.OPENID)
201+
.scope(OidcScopes.PROFILE)
202+
.clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
203+
.build();
204+
// @formatter:on
205+
206+
return new InMemoryRegisteredClientRepository(oidcClient);
207+
}
208+
209+
@Bean // <5>
210+
public JWKSource<SecurityContext> jwkSource() {
211+
KeyPair keyPair = generateRsaKey();
212+
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
213+
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
214+
// @formatter:off
215+
RSAKey rsaKey = new RSAKey.Builder(publicKey)
216+
.privateKey(privateKey)
217+
.keyID(UUID.randomUUID().toString())
218+
.build();
219+
// @formatter:on
220+
JWKSet jwkSet = new JWKSet(rsaKey);
221+
return new ImmutableJWKSet<>(jwkSet);
222+
}
223+
224+
private static KeyPair generateRsaKey() { // <6>
225+
KeyPair keyPair;
226+
try {
227+
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
228+
keyPairGenerator.initialize(2048);
229+
keyPair = keyPairGenerator.generateKeyPair();
230+
}
231+
catch (Exception ex) {
232+
throw new IllegalStateException(ex);
233+
}
234+
return keyPair;
235+
}
236+
237+
@Bean // <7>
238+
public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
239+
return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
240+
}
241+
242+
@Bean // <8>
243+
public AuthorizationServerSettings authorizationServerSettings() {
244+
return AuthorizationServerSettings.builder().build();
245+
}
246+
247+
}
96248
----
97249

98250
This is a minimal configuration for getting started quickly. To understand what each component is used for, see the following descriptions:
99251

100-
<1> A Spring Security filter chain for the xref:protocol-endpoints.adoc[Protocol Endpoints].
101-
<2> A Spring Security filter chain for https://docs.spring.io/spring-security/reference/servlet/authentication/index.html[authentication].
102-
<3> An instance of {spring-security-api-base-url}/org/springframework/security/core/userdetails/UserDetailsService.html[`UserDetailsService`] for retrieving users to authenticate.
103-
<4> An instance of xref:core-model-components.adoc#registered-client-repository[`RegisteredClientRepository`] for managing clients.
252+
<1> A Spring Security filter chain for the xref:servlet/oauth2/authorization-server/protocol-endpoints.adoc[Protocol Endpoints].
253+
<2> A Spring Security filter chain for xref:servlet/authentication/index.adoc#servlet-authentication[authentication].
254+
<3> An instance of {security-api-url}/org/springframework/security/core/userdetails/UserDetailsService.html[`UserDetailsService`] for retrieving users to authenticate.
255+
<4> An instance of xref:servlet/oauth2/authorization-server/core-model-components.adoc#oauth2AuthorizationServer-registered-client-repository[`RegisteredClientRepository`] for managing clients.
104256
<5> An instance of `com.nimbusds.jose.jwk.source.JWKSource` for signing access tokens.
105257
<6> An instance of `java.security.KeyPair` with keys generated on startup used to create the `JWKSource` above.
106-
<7> An instance of {spring-security-api-base-url}/org/springframework/security/oauth2/jwt/JwtDecoder.html[`JwtDecoder`] for decoding signed access tokens.
107-
<8> An instance of xref:configuration-model#configuring-authorization-server-settings[`AuthorizationServerSettings`] to configure Spring Authorization Server.
258+
<7> An instance of {security-api-url}/org/springframework/security/oauth2/jwt/JwtDecoder.html[`JwtDecoder`] for decoding signed access tokens.
259+
<8> An instance of xref:servlet/oauth2/authorization-server/configuration-model.adoc#oauth2AuthorizationServer-configuring-authorization-server-settings[`AuthorizationServerSettings`] to configure Spring Security Authorization Server.

0 commit comments

Comments
 (0)