spring-projects / spring-projects/spring-security

Resource server examples incorrectly use hyphenated `opaque-token` properties namespace

Open
#13,547 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

in: docs type: bug
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

I just encountered a problem with property overriding when I tried to override the client-id of the opaque-token property of the resourceserver configuration in Spring Security.

In my code I am loading the value as following via an @Value annotation, in one of my configuration classes:

@Value("${spring.security.oauth2.resourceserver.opaque-token.client-id}")

This is in accordance with the documented naming convention for the key as can be seen here:
https://docs.spring.io/spring-security/reference/servlet/oauth2/resource-server/opaque-token.html#oauth2resourceserver-opaque-introspectionuri

Please observe, that both opaque-token and client-id use a hyphen in their name.

If I place the correct value for this key directly into the application.yaml file then everything works as expected. However, I wanted to override this value using the relaxed binding rules for environment variables and used the following variable name:

SPRING_SECURITY_OAUTH2_RESOURCESERVER_OPAQUE_TOKEN_CLIENT_ID

However this did now work, and after some debugging I found that specifically the ReactiveOAuth2ResourceServerOpaqueTokenConfiguration (see https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/reactive/ReactiveOAuth2ResourceServerOpaqueTokenConfiguration.java) was not receiving the overwritten value, and it appears that the reason is, that this class is using a different key format for loading the resource server properties, which is:

@ConditionalOnProperty(name = "spring.security.oauth2.resourceserver.opaquetoken.introspection-uri")

Observe, that there the opaquetoken is not using a hyphen. I then realized, that according to the relaxed binding convention, I should maybe remove the underscores in placed where a hyphen is used and I changed the environment variable name to the following:

SPRING_SECURITY_OAUTH2_RESOURCESERVER_OPAQUETOKEN_CLIENTID

This did not work for two reasons:

  1. The client-id was no longer correctly bound, since I removed the underscore also from CLIENT_ID.
  2. My own annotation was still using the opaque-token format (with a hyphen).

After some experiments with different placements of underscores and hyphens, I found that the only working configuration for me was the following:

  1. In my application.yaml file I had to remove the hyphen from opaque-token and define it as opaquetoken (which is different from the documented format in the link at the top).
  2. In my own configuration classes I had to adjust the value annotation and load the client id via @Value("${spring.security.oauth2.resourceserver.opaquetoken.client-id}")
  3. I had to change the environment variable and use the following format: SPRING_SECURITY_OAUTH2_RESOURCESERVER_OPAQUETOKEN_CLIENT_ID (not that the OPAQUETOKEN part is without underscore, but the CLIENT_ID part uses an underscore, which strictly speaking is not how the relaxed binding convention for environment variables is documented.

The original reason, why I had to look into this, is because I was being hit by the same error as described in this issue, since the client-id was not correctly configured in my resource server:
https://github.com/spring-projects/spring-security/issues/7858

To Reproduce

  1. Configure a resource server with spring security.
  2. Define a default opaque-token.client-id in the application.yaml for the resource server
  3. Then try to override the client-id of the opaque-token.client-id using an environment variable
  4. Observer that the old client-id is being used.

Expected behavior

  1. Overriding works correctly
  2. Consistent use of opaque-token vs. opaquetoken in documentation and internal implementation.

May I request a clarification on the following items:

  1. If the use of opaquetoken over opaque-token in the ReactiveOAuth2ResourceServerOpaqueTokenConfiguration is considered correct.
  2. If the recommended format of the relaxed binding environment variable name should be:
    a) SPRING_SECURITY_OAUTH2_RESOURCESERVER_OPAQUETOKEN_CLIENTID or
    b) SPRING_SECURITY_OAUTH2_RESOURCESERVER_OPAQUETOKEN_CLIENT_ID or
    c) SPRING_SECURITY_OAUTH2_RESOURCESERVER_OPAQUE_TOKEN_CLIENT_ID or
    Only b) worked for me.
  3. If I should follow the documentation and use opaque-token in the application.yaml or if rather opaquetoken should be used.

Contributor guide

Open the contributing guide

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 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/reactive/ReactiveOAuth2ResourceServerOpaqueTokenConfiguration.java and compare its conditional property with the documented opaque-token key. Reproduce the environment-variable override described in the issue and verify that YAML, @Value, and environment-variable naming behave consistently when the correction is complete.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
security
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.