Skip to content

Commit 1570a56

Browse files
committed
Polish gh-1264
1 parent e9b7089 commit 1570a56

File tree

4 files changed

+54
-27
lines changed

4 files changed

+54
-27
lines changed
Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,51 @@
11
[[how-to-custom-claims-authorities]]
2-
= How-to: Add authorities as custom claims in JWT-based access tokens
2+
= How-to: Add authorities as custom claims in JWT access tokens
33
:index-link: ../how-to.html
44
:docs-dir: ..
55

66
This guide demonstrates how to add resource owner authorities to a JWT access token.
77
The term "authorities" may represent varying forms such as roles, permissions, or groups of the resource owner.
88

9-
To make resource owners' authorities available to the resource server, we add custom claims to an access token issued by Spring Authorization Server.
10-
The client using the issued token to access protected resources will then have information about the resource owners level of access, among other potential uses and benefits.
9+
To make resource owner's authorities available to the resource server, we add custom claims to the access token.
10+
When the client uses the access token to access a protected resource, the resource server will be able to obtain the information about the resource owner's level of access, among other potential uses and benefits.
1111

1212
* xref:guides/how-to-custom-claims-authorities.adoc#custom-claims[Add custom claims to JWT access tokens]
1313
* xref:guides/how-to-custom-claims-authorities.adoc#custom-claims-authorities[Add authorities as custom claims to JWT access tokens]
1414

1515
[[custom-claims]]
1616
== Add custom claims to JWT access tokens
1717

18-
You may add your own custom claims to an access token using `OAuth2TokenCustomizer<JWTEncodingContext>` bean.
19-
Please note that this bean may only be defined once, and so care must be taken care of to ensure that you are customizing the appropriate token type — an access token in this case.
20-
If you are interested in customizing the identity token, see xref:guides/how-to-userinfo.adoc#customize-user-info-mapper[the UserInfo mapper guide for more information].
18+
You may add your own custom claims to an access token using an `OAuth2TokenCustomizer<JWTEncodingContext>` `@Bean`.
19+
Please note that this `@Bean` may only be defined once, and so care must be taken to ensure that you are customizing the appropriate token type — an access token in this case.
20+
If you are interested in customizing the ID Token, see the xref:guides/how-to-userinfo.adoc#customize-user-info-mapper[User Info Mapper guide] for more information.
2121

2222
The following is an example of adding custom claims to an access token — in other words, every access token that is issued by the authorization server will have the custom claims populated.
2323

24-
[[sample.customClaims]]
24+
[[sample.customclaims]]
2525
[source,java]
2626
----
27-
include::{examples-dir}/main/java/sample/customClaims/CustomClaimsConfiguration.java[]
27+
include::{examples-dir}/main/java/sample/customclaims/CustomClaimsConfiguration.java[]
2828
----
2929

3030
[[custom-claims-authorities]]
3131
== Add authorities as custom claims to JWT access tokens
3232

33-
To add authorities of the resource owner to a JWT-based access token, we can refer to the custom claim mapping method above
34-
and populate custom claims with the authorities of the `Principal`.
33+
To add authorities of the resource owner to a JWT access token, we can refer to the custom claim mapping method above and populate a custom claim with the authorities of the `Principal`.
3534

36-
We define a sample user with a mix of authorities for demonstration purposes, and populate custom claims in an access token
37-
with those authorities.
35+
We define a sample user with a set of authorities for demonstration purposes, and populate a custom claim in the access token with those authorities.
3836

39-
[[sample.customClaims.authorities]]
37+
[[sample.customclaims.authorities]]
4038
[source,java]
4139
----
42-
include::{examples-dir}/main/java/sample/customClaims/authorities/CustomClaimsWithAuthoritiesConfiguration.java[]
40+
include::{examples-dir}/main/java/sample/customclaims/authorities/CustomClaimsWithAuthoritiesConfiguration.java[]
4341
----
4442

45-
<1> Define a sample user `user1` with an in-memory user details service.
46-
<2> Define a few roles for `user1`.
47-
<3> Define `OAuth2TokenCustomizer<JwtEncodingContext>` `@Bean` that allows for customizing JWT token claims.
48-
<4> Check whether the JWT token is an access token.
49-
<5> From the encoding context, modify the claims of the access token.
50-
<6> Extract user roles from the `Principal` object. The role information for internal users is stored as a string prefixed with `ROLE_`, so we strip the prefix here.
51-
<7> Set custom claim `roles` to the set of roles collected from the previous step.
43+
<1> Define a sample user `user1` with an in-memory `UserDetailsService`.
44+
<2> Assign the roles for `user1`.
45+
<3> Define an `OAuth2TokenCustomizer<JwtEncodingContext>` `@Bean` that allows for customizing the JWT claims.
46+
<4> Check whether the JWT is an access token.
47+
<5> Access the default claims via the `JwtEncodingContext`.
48+
<6> Extract the roles from the `Principal` object. The role information is stored as a string prefixed with `ROLE_`, so we strip the prefix here.
49+
<7> Set the custom claim `roles` to the set of roles collected from the previous step.
5250

53-
As a result of this customization, authorities information about the user will be included as a custom claim within the
54-
access token.
51+
As a result of this customization, authorities information about the user will be included as a custom claim in the access token.

docs/modules/ROOT/pages/how-to.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
* xref:guides/how-to-ext-grant-type.adoc[Implement an Extension Authorization Grant Type]
1212
* xref:guides/how-to-userinfo.adoc[Customize the OpenID Connect 1.0 UserInfo response]
1313
* xref:guides/how-to-jpa.adoc[Implement core services with JPA]
14-
* xref:guides/how-to-custom-claims-authorities.adoc[Add authorities as custom claims in JWT-based access tokens]
14+
* xref:guides/how-to-custom-claims-authorities.adoc[Add authorities as custom claims in JWT access tokens]

docs/src/main/java/sample/customClaims/CustomClaimsConfiguration.java renamed to docs/src/main/java/sample/customclaims/CustomClaimsConfiguration.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
package sample.customClaims;
1+
/*
2+
* Copyright 2020-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package sample.customclaims;
217

318
import org.springframework.context.annotation.Bean;
419
import org.springframework.context.annotation.Configuration;

docs/src/main/java/sample/customClaims/authorities/CustomClaimsWithAuthoritiesConfiguration.java renamed to docs/src/main/java/sample/customclaims/authorities/CustomClaimsWithAuthoritiesConfiguration.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
package sample.customClaims.authorities;
1+
/*
2+
* Copyright 2020-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package sample.customclaims.authorities;
217

318
import org.springframework.context.annotation.Bean;
419
import org.springframework.context.annotation.Configuration;
@@ -22,7 +37,7 @@ public UserDetailsService users() {
2237
UserDetails user = User.withDefaultPasswordEncoder()
2338
.username("user1") // <1>
2439
.password("password")
25-
.roles(new String[] { "user", "admin" }) // <2>
40+
.roles("user", "admin") // <2>
2641
.build();
2742
return new InMemoryUserDetailsManager(user);
2843
}

0 commit comments

Comments
 (0)