Skip to content

Commit 44de65e

Browse files
authored
Support setting redirect uri. (Azure#21249)
1 parent caabe05 commit 44de65e

File tree

7 files changed

+52
-14
lines changed

7 files changed

+52
-14
lines changed

sdk/spring/azure-spring-boot-starter-active-directory/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
### New Features
55
- Upgrade to [spring-boot-dependencies:2.4.5](https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-dependencies/2.4.5/spring-boot-dependencies-2.4.5.pom).
66
- Upgrade to [spring-cloud-dependencies:2020.0.2](https://repo.maven.apache.org/maven2/org/springframework/cloud/spring-cloud-dependencies/2020.0.2/spring-cloud-dependencies-2020.0.2.pom).
7+
- Enable property azure.activedirectory.redirect-uri-template.([#21116](https://github.com/Azure/azure-sdk-for-java/issues/21116))
78

89

910

sdk/spring/azure-spring-boot-starter-active-directory/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,49 @@ In [Resource server visiting other resource server] scenario(For better descript
570570
return "Response from WebApiB.";
571571
}
572572
```
573+
574+
#### Support setting redirect-uri-template.
575+
576+
Developers can customize the redirect-uri.
577+
578+
![redirect-uri](resource/redirect-uri.png)
579+
580+
* Step 1: Add `redirect-uri-template` properties in application.yml.
581+
```yaml
582+
azure:
583+
activedirectory:
584+
redirect-uri-template: --your-redirect-uri-template--
585+
```
586+
587+
* Step 2: Update the configuration of the azure cloud platform in the portal.
588+
589+
We need to configure the same redirect-uri as application.yml:
590+
591+
![web-application-config-redirect-uri](resource/web-application-config-redirect-uri.png)
592+
593+
* Step 3: Write your Java code:
594+
595+
After we set redirect-uri-template, we need to update `SecurityConfigurerAdapter`:
596+
597+
```java
598+
@EnableWebSecurity
599+
@EnableGlobalMethodSecurity(prePostEnabled = true)
600+
public class AADOAuth2LoginSecurityConfig extends AADWebSecurityConfigurerAdapter {
601+
/**
602+
* Add configuration logic as needed.
603+
*/
604+
@Override
605+
protected void configure(HttpSecurity http) throws Exception {
606+
super.configure(http);
607+
http.oauth2Login()
608+
.loginProcessingUrl("/{your-redirect-uri}")
609+
.and()
610+
.authorizeRequests()
611+
.anyRequest().authenticated();
612+
}
613+
}
614+
```
615+
573616
## Examples
574617

575618
### Web application visiting resource servers
63.9 KB
Loading
140 KB
Loading

sdk/spring/azure-spring-boot/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 3.5.0-beta.1 (Unreleased)
44
### New Features
55
- Add `AADB2CTrustedIssuerRepository` to manage the trusted issuer in AAD B2C.
6+
- Enable property azure.activedirectory.redirect-uri-template. ([#21116](https://github.com/Azure/azure-sdk-for-java/issues/21116))
67

78
### Key Bug Fixes
89
- Fix the issue [#21036](https://github.com/Azure/azure-sdk-for-java/issues/21036) where the AAD B2C starter cannot fetch the OpenID Connect metadata document via issuer.

sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapp/AADWebAppConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private ClientRegistration.Builder createClientBuilder(String id, AADAuthorizati
182182
.orElse(AuthorizationGrantType.AUTHORIZATION_CODE);
183183
result.authorizationGrantType(authorizationGrantType);
184184

185-
result.redirectUri("{baseUrl}/login/oauth2/code/");
185+
result.redirectUri(properties.getRedirectUriTemplate());
186186
result.userNameAttributeName(properties.getUserNameAttribute());
187187

188188
result.clientId(properties.getClientId());

sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/autoconfigure/aad/AADAuthenticationProperties.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,9 @@ public class AADAuthenticationProperties implements InitializingBean {
5454
private String userNameAttribute;
5555

5656
/**
57-
* @deprecated Now the redirect-url-template is not configurable.
58-
* <p>
59-
* Redirect URI always equal to "{baseUrl}/login/oauth2/code/".
60-
* </p>
61-
* <p>
62-
* User should set "Redirect URI" to "{baseUrl}/login/oauth2/code/" in Azure Portal.
63-
* </p>
64-
*
65-
* @see <a href="https://github.com/Azure/azure-sdk-for-java/tree/c27ee4421309cec8598462b419e035cf091429da/sdk/spring/azure-spring-boot-starter-active-directory#accessing-a-web-application">aad-starter readme.</a>
66-
* @see com.azure.spring.aad.webapp.AADWebAppConfiguration#clientRegistrationRepository()
57+
* Redirection Endpoint: Used by the authorization server to return responses containing authorization credentials
58+
* to the client via the resource owner user-agent.
6759
*/
68-
@Deprecated
6960
private String redirectUriTemplate;
7061

7162
/**
@@ -236,12 +227,10 @@ public void setUserNameAttribute(String userNameAttribute) {
236227
this.userNameAttribute = userNameAttribute;
237228
}
238229

239-
@Deprecated
240230
public String getRedirectUriTemplate() {
241231
return redirectUriTemplate;
242232
}
243233

244-
@Deprecated
245234
public void setRedirectUriTemplate(String redirectUriTemplate) {
246235
this.redirectUriTemplate = redirectUriTemplate;
247236
}
@@ -383,6 +372,10 @@ public void afterPropertiesSet() throws Exception {
383372
baseUri = addSlash(baseUri);
384373
}
385374

375+
if (!StringUtils.hasText(redirectUriTemplate)) {
376+
redirectUriTemplate = "{baseUrl}/login/oauth2/code/";
377+
}
378+
386379
if (!StringUtils.hasText(graphBaseUri)) {
387380
graphBaseUri = "https://graph.microsoft.com/";
388381
} else {

0 commit comments

Comments
 (0)