|
1 | 1 | /*
|
2 |
| - * Copyright 2020-2022 the original author or authors. |
| 2 | + * Copyright 2020-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 | import java.io.IOException;
|
19 | 19 | import java.nio.charset.StandardCharsets;
|
20 | 20 | import java.util.Arrays;
|
21 |
| -import java.util.HashMap; |
22 | 21 | import java.util.HashSet;
|
23 |
| -import java.util.Map; |
24 | 22 | import java.util.Set;
|
25 | 23 |
|
26 | 24 | import jakarta.servlet.FilterChain;
|
|
67 | 65 | import org.springframework.util.StringUtils;
|
68 | 66 | import org.springframework.web.filter.OncePerRequestFilter;
|
69 | 67 | import org.springframework.web.util.UriComponentsBuilder;
|
| 68 | +import org.springframework.web.util.UriUtils; |
70 | 69 |
|
71 | 70 | /**
|
72 | 71 | * A {@code Filter} for the OAuth 2.0 Authorization Code Grant,
|
@@ -301,13 +300,11 @@ private void sendAuthorizationResponse(HttpServletRequest request, HttpServletRe
|
301 | 300 | .queryParam(OAuth2ParameterNames.CODE, authorizationCodeRequestAuthentication.getAuthorizationCode().getTokenValue());
|
302 | 301 | String redirectUri;
|
303 | 302 | if (StringUtils.hasText(authorizationCodeRequestAuthentication.getState())) {
|
304 |
| - uriBuilder.queryParam(OAuth2ParameterNames.STATE, "{state}"); |
305 |
| - Map<String, String> queryParams = new HashMap<>(); |
306 |
| - queryParams.put(OAuth2ParameterNames.STATE, authorizationCodeRequestAuthentication.getState()); |
307 |
| - redirectUri = uriBuilder.build(queryParams).toString(); |
308 |
| - } else { |
309 |
| - redirectUri = uriBuilder.toUriString(); |
| 303 | + uriBuilder.queryParam( |
| 304 | + OAuth2ParameterNames.STATE, |
| 305 | + UriUtils.encode(authorizationCodeRequestAuthentication.getState(), StandardCharsets.UTF_8)); |
310 | 306 | }
|
| 307 | + redirectUri = uriBuilder.build(true).toUriString(); // build(true) -> Components are explicitly encoded |
311 | 308 | this.redirectStrategy.sendRedirect(request, response, redirectUri);
|
312 | 309 | }
|
313 | 310 |
|
@@ -341,13 +338,11 @@ private void sendErrorResponse(HttpServletRequest request, HttpServletResponse r
|
341 | 338 | }
|
342 | 339 | String redirectUri;
|
343 | 340 | if (StringUtils.hasText(authorizationCodeRequestAuthentication.getState())) {
|
344 |
| - uriBuilder.queryParam(OAuth2ParameterNames.STATE, "{state}"); |
345 |
| - Map<String, String> queryParams = new HashMap<>(); |
346 |
| - queryParams.put(OAuth2ParameterNames.STATE, authorizationCodeRequestAuthentication.getState()); |
347 |
| - redirectUri = uriBuilder.build(queryParams).toString(); |
348 |
| - } else { |
349 |
| - redirectUri = uriBuilder.toUriString(); |
| 341 | + uriBuilder.queryParam( |
| 342 | + OAuth2ParameterNames.STATE, |
| 343 | + UriUtils.encode(authorizationCodeRequestAuthentication.getState(), StandardCharsets.UTF_8)); |
350 | 344 | }
|
| 345 | + redirectUri = uriBuilder.build(true).toUriString(); // build(true) -> Components are explicitly encoded |
351 | 346 | this.redirectStrategy.sendRedirect(request, response, redirectUri);
|
352 | 347 | }
|
353 | 348 |
|
|
0 commit comments