spring-projects / spring-projects/spring-security-samples

Non-Standard Scope Claim Name in servlet/spring-boot/java/jwt/login/src/main/java/example/web/TokenController.java

Open Beginner friendly
#408 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.8k
Forks
797
Avg merge
3m
Merged PRs (30d)
5

Description

Unless I am understanding the code and the intent wrong, I believe there is a bug in one of the examples.

In the class servlet/spring-boot/java/jwt/login/src/main/java/example/web/TokenController.java the name of the "scope" claim is not the standard scp, but scope -- see line 50:

		JwtClaimsSet claims = JwtClaimsSet.builder()
				.issuer("self")
				.issuedAt(now)
				.expiresAt(now.plusSeconds(expiry))
				.subject(authentication.getName())
				.claim("scope", scope)
				.build();

This would create a token such as:

{
  "iss": "self",
  "sub": "etrovador",
  "scope": "LIST_CUSTOMERS READ_CUSTOMER WRITE_CUSTOMER FACTOR_PASSWORD",
  "exp": 1783060519,
  "iat": 1783060099
}

This claim name makes the Spring Security annotation like @PreAuthorize("hasAuthority('SCOPE_LIST_CUSTOMERS')") fail, as it works by splitting the scp content (missing) and then attaching to all of the substrings the prefix SCOPE_.

By changing the scope name to scp, an annotation like the above would work as it will receive the following token:

{
  "iss": "self",
  "sub": "etrovador",
  "scp": "LIST_CUSTOMERS READ_CUSTOMER WRITE_CUSTOMER FACTOR_PASSWORD",
  "exp": 1783060519,
  "iat": 1783060099
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with servlet/spring-boot/java/jwt/login/src/main/java/example/web/TokenController.java, especially the scope claim construction at line 50, and compare it with the sample's @PreAuthorize usage. Done means the generated JWT uses the claim name expected by Spring Security and the example's scope-based authorization behavior is verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
authentication, authorization
Issue type
Bug
Difficulty
1/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
86/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.