Skip to content

[AAD][Boot 3] Allow anonymous access to the client credentials sample API - #869

Draft
Moary Chen (moarychan) wants to merge 2 commits into
spring-boot-3.xfrom
moarychan/backport-868-aad-authentication-spring-boot-3x
Draft

Moary Chen (moarychan) wants to merge 2 commits into
spring-boot-3.xfrom
moarychan/backport-868-aad-authentication-spring-boot-3x

Conversation

@moarychan

Copy link
Copy Markdown
Contributor

Purpose

Backport the final changes from #868 to spring-boot-3.x.

Explicitly allow anonymous access to /webapiB/clientCredential while requiring authentication for other requests and preserving the existing method-level scope checks.

Source: 26407f1893bf688bda4f75eea0013ec5fca5d81a. Only the source PR's final three-file net change is included; its superseded README changes are not ported.

Changes and Boot 3 adaptations

  • Add the same sample-owned AAD resource server security chain as the source PR: permitAll() for the public endpoint, with an authenticated fallback and method security.
  • Use the test-scoped spring-boot-starter-test instead of Boot 4's spring-boot-starter-webmvc-test.
  • Use org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc.
  • Keep @MockitoBean, which is available in the target Spring Framework version.
  • Preserve Spring Boot 3.5.16, Spring Cloud Azure 6.5.0, and Java 17 compilation.

No README, runtime dependency version, controller, mixed Web/API, OBO, or Web application change is included. /api/hello is retained.

Does this introduce a breaking change?

  • Yes
  • No

The public sample endpoint remains callable anonymously. The existing protected REST endpoints retain their authentication and scope requirements.

Pull Request Type

  • Bugfix
  • Feature
  • Code style update
  • Refactoring
  • Documentation content changes
  • Other

How to Test

From the aad-resource-server module:

mvn -B -ntp -Dtest=AadResourceServerConfigTests test

Local validation:

  • All eight integration test cases passed on the target stack: zero failures, errors, or skipped tests.
  • Removing only the public permitAll() rule makes the anonymous-endpoint test fail precisely with Status expected:<200> but was:<401>; restoring it makes the complete class pass.
  • javap verified the target annotation and AAD configurer APIs.
  • git diff --check passed.

What to Check

Request Expected result
Anonymous GET /webapiB/clientCredential 200 with the sample response
Anonymous GET /webapiB, /user, or another non-public path 401
Protected endpoint with the required scope 200
Protected endpoint without the required scope 403

Tests use synthetic properties and a mocked JwtDecoder. They verify authorization, not real Entra token signatures or OBO exchanges. No full-repository build or live tenant validation was performed.

Port the final net changes from #868 (26407f1), adapting the test starter and AutoConfigureMockMvc package to Spring Boot 3. Preserve existing framework versions and method permissions without bringing over superseded README changes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@moarychan
Moary Chen (moarychan) marked this pull request as ready for review November 27, 2026 07:08
@moarychan
Moary Chen (moarychan) requested a review from a team as a code owner November 27, 2026 07:08
Copilot AI balanced review requested due to automatic review settings November 27, 2026 07:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The anonymous-access backport has no active review issues.

No open issues 🎉

What changed in this PR

Backports the anonymous-access fix to the Spring Boot 3 AAD resource-server sample, keeping /webapiB/clientCredential public while protecting other requests.

Changes:

  • Adds a sample-owned security chain with an authenticated fallback and method-level security.
  • Adds eight authorization test cases using MockMvc and a mocked JwtDecoder.
  • Adds the Boot 3-compatible test starter without changing runtime dependency versions.
File Description
aad/​spring-cloud-azure-starter-active-directory/​web-client-access-resource-server/​aad-resource-server/​src/​test/​java/​com/​azure/​spring/​sample/​aad/​security/​AadResourceServerConfigTests.java Tests anonymous access, authentication requirements, and scope enforcement.
aad/​spring-cloud-azure-starter-active-directory/​web-client-access-resource-server/​aad-resource-server/​src/​main/​java/​com/​azure/​spring/​sample/​aad/​security/​AadResourceServerConfig.java Configures the public endpoint exception and protects remaining requests.
aad/​spring-cloud-azure-starter-active-directory/​web-client-access-resource-server/​aad-resource-server/​pom.xml Adds test-scoped spring-boot-starter-test.

Review effort: Balanced

@moarychan

Copy link
Copy Markdown
Contributor Author

Copilot merge main branch to sync code

Copilot AI commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Copilot merge main branch to sync code

The merge preview shows 74 conflicts: main uses Spring Boot 4.1.1, while this backport targets 3.5.16. Should I preserve Boot 3 throughout the merge, or migrate this branch to Boot 4? No merge has been started.

@moarychan

Copy link
Copy Markdown
Contributor Author

Copilot Sorry, merge spring-boot-3.x branch to sync code

…backport-868-aad-authentication-spring-boot-3x

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

Copilot AI commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Copilot Sorry, merge spring-boot-3.x branch to sync code

Merged spring-boot-3.x in 643ca0e. The merge brings in only the upstream README update; the backport is unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants