mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-10-14 08:23:45 +00:00
[Java][resttemplate] Enable access token refresh (#16485)
* use supplier to enable refreshing token * update samples * fix param name and doc * update samples
This commit is contained in:
parent
47a85e880b
commit
09704951f0
@ -62,6 +62,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
import java.util.function.Supplier;
|
||||||
{{#jsr310}}
|
{{#jsr310}}
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
{{/jsr310}}
|
{{/jsr310}}
|
||||||
@ -192,9 +193,18 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
|
|||||||
* @param bearerToken the token
|
* @param bearerToken the token
|
||||||
*/
|
*/
|
||||||
public void setBearerToken(String bearerToken) {
|
public void setBearerToken(String bearerToken) {
|
||||||
|
setBearerToken(() -> bearerToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method to set the token supplier for HTTP bearer authentication.
|
||||||
|
*
|
||||||
|
* @param tokenSupplier the token supplier function
|
||||||
|
*/
|
||||||
|
public void setBearerToken(Supplier<String> tokenSupplier) {
|
||||||
for (Authentication auth : authentications.values()) {
|
for (Authentication auth : authentications.values()) {
|
||||||
if (auth instanceof HttpBearerAuth) {
|
if (auth instanceof HttpBearerAuth) {
|
||||||
((HttpBearerAuth) auth).setBearerToken(bearerToken);
|
((HttpBearerAuth) auth).setBearerToken(tokenSupplier);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,34 @@
|
|||||||
package {{invokerPackage}}.auth;
|
package {{invokerPackage}}.auth;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
|
|
||||||
{{>generatedAnnotation}}
|
{{>generatedAnnotation}}
|
||||||
public class HttpBearerAuth implements Authentication {
|
public class HttpBearerAuth implements Authentication {
|
||||||
private final String scheme;
|
private final String scheme;
|
||||||
private String bearerToken;
|
private Supplier<String> tokenSupplier;
|
||||||
|
|
||||||
public HttpBearerAuth(String scheme) {
|
public HttpBearerAuth(String scheme) {
|
||||||
this.scheme = scheme;
|
this.scheme = scheme;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBearerToken() {
|
public String getBearerToken() {
|
||||||
return bearerToken;
|
return tokenSupplier.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBearerToken(String bearerToken) {
|
public void setBearerToken(String bearerToken) {
|
||||||
this.bearerToken = bearerToken;
|
this.tokenSupplier = () -> bearerToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBearerToken(Supplier<String> tokenSupplier) {
|
||||||
|
this.tokenSupplier = tokenSupplier;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
|
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
|
||||||
|
String bearerToken = Optional.ofNullable(tokenSupplier).map(Supplier::get).orElse(null);
|
||||||
if (bearerToken == null) {
|
if (bearerToken == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
|
|
||||||
import org.openapitools.client.auth.Authentication;
|
import org.openapitools.client.auth.Authentication;
|
||||||
|
@ -1,27 +1,34 @@
|
|||||||
package org.openapitools.client.auth;
|
package org.openapitools.client.auth;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
|
|
||||||
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
||||||
public class HttpBearerAuth implements Authentication {
|
public class HttpBearerAuth implements Authentication {
|
||||||
private final String scheme;
|
private final String scheme;
|
||||||
private String bearerToken;
|
private Supplier<String> tokenSupplier;
|
||||||
|
|
||||||
public HttpBearerAuth(String scheme) {
|
public HttpBearerAuth(String scheme) {
|
||||||
this.scheme = scheme;
|
this.scheme = scheme;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBearerToken() {
|
public String getBearerToken() {
|
||||||
return bearerToken;
|
return tokenSupplier.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBearerToken(String bearerToken) {
|
public void setBearerToken(String bearerToken) {
|
||||||
this.bearerToken = bearerToken;
|
this.tokenSupplier = () -> bearerToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBearerToken(Supplier<String> tokenSupplier) {
|
||||||
|
this.tokenSupplier = tokenSupplier;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
|
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
|
||||||
|
String bearerToken = Optional.ofNullable(tokenSupplier).map(Supplier::get).orElse(null);
|
||||||
if (bearerToken == null) {
|
if (bearerToken == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
|
|
||||||
import org.openapitools.client.auth.Authentication;
|
import org.openapitools.client.auth.Authentication;
|
||||||
|
@ -1,27 +1,34 @@
|
|||||||
package org.openapitools.client.auth;
|
package org.openapitools.client.auth;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
|
|
||||||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
||||||
public class HttpBearerAuth implements Authentication {
|
public class HttpBearerAuth implements Authentication {
|
||||||
private final String scheme;
|
private final String scheme;
|
||||||
private String bearerToken;
|
private Supplier<String> tokenSupplier;
|
||||||
|
|
||||||
public HttpBearerAuth(String scheme) {
|
public HttpBearerAuth(String scheme) {
|
||||||
this.scheme = scheme;
|
this.scheme = scheme;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBearerToken() {
|
public String getBearerToken() {
|
||||||
return bearerToken;
|
return tokenSupplier.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBearerToken(String bearerToken) {
|
public void setBearerToken(String bearerToken) {
|
||||||
this.bearerToken = bearerToken;
|
this.tokenSupplier = () -> bearerToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBearerToken(Supplier<String> tokenSupplier) {
|
||||||
|
this.tokenSupplier = tokenSupplier;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
|
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
|
||||||
|
String bearerToken = Optional.ofNullable(tokenSupplier).map(Supplier::get).orElse(null);
|
||||||
if (bearerToken == null) {
|
if (bearerToken == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
|
|
||||||
import org.openapitools.client.auth.Authentication;
|
import org.openapitools.client.auth.Authentication;
|
||||||
|
@ -1,27 +1,34 @@
|
|||||||
package org.openapitools.client.auth;
|
package org.openapitools.client.auth;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
|
|
||||||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
||||||
public class HttpBearerAuth implements Authentication {
|
public class HttpBearerAuth implements Authentication {
|
||||||
private final String scheme;
|
private final String scheme;
|
||||||
private String bearerToken;
|
private Supplier<String> tokenSupplier;
|
||||||
|
|
||||||
public HttpBearerAuth(String scheme) {
|
public HttpBearerAuth(String scheme) {
|
||||||
this.scheme = scheme;
|
this.scheme = scheme;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBearerToken() {
|
public String getBearerToken() {
|
||||||
return bearerToken;
|
return tokenSupplier.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBearerToken(String bearerToken) {
|
public void setBearerToken(String bearerToken) {
|
||||||
this.bearerToken = bearerToken;
|
this.tokenSupplier = () -> bearerToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBearerToken(Supplier<String> tokenSupplier) {
|
||||||
|
this.tokenSupplier = tokenSupplier;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
|
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
|
||||||
|
String bearerToken = Optional.ofNullable(tokenSupplier).map(Supplier::get).orElse(null);
|
||||||
if (bearerToken == null) {
|
if (bearerToken == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -56,6 +56,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
|
|
||||||
import org.openapitools.client.auth.Authentication;
|
import org.openapitools.client.auth.Authentication;
|
||||||
@ -170,9 +171,18 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
* @param bearerToken the token
|
* @param bearerToken the token
|
||||||
*/
|
*/
|
||||||
public void setBearerToken(String bearerToken) {
|
public void setBearerToken(String bearerToken) {
|
||||||
|
setBearerToken(() -> bearerToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method to set the token supplier for HTTP bearer authentication.
|
||||||
|
*
|
||||||
|
* @param tokenSupplier the token supplier function
|
||||||
|
*/
|
||||||
|
public void setBearerToken(Supplier<String> tokenSupplier) {
|
||||||
for (Authentication auth : authentications.values()) {
|
for (Authentication auth : authentications.values()) {
|
||||||
if (auth instanceof HttpBearerAuth) {
|
if (auth instanceof HttpBearerAuth) {
|
||||||
((HttpBearerAuth) auth).setBearerToken(bearerToken);
|
((HttpBearerAuth) auth).setBearerToken(tokenSupplier);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,34 @@
|
|||||||
package org.openapitools.client.auth;
|
package org.openapitools.client.auth;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
|
|
||||||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
||||||
public class HttpBearerAuth implements Authentication {
|
public class HttpBearerAuth implements Authentication {
|
||||||
private final String scheme;
|
private final String scheme;
|
||||||
private String bearerToken;
|
private Supplier<String> tokenSupplier;
|
||||||
|
|
||||||
public HttpBearerAuth(String scheme) {
|
public HttpBearerAuth(String scheme) {
|
||||||
this.scheme = scheme;
|
this.scheme = scheme;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBearerToken() {
|
public String getBearerToken() {
|
||||||
return bearerToken;
|
return tokenSupplier.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBearerToken(String bearerToken) {
|
public void setBearerToken(String bearerToken) {
|
||||||
this.bearerToken = bearerToken;
|
this.tokenSupplier = () -> bearerToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBearerToken(Supplier<String> tokenSupplier) {
|
||||||
|
this.tokenSupplier = tokenSupplier;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
|
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
|
||||||
|
String bearerToken = Optional.ofNullable(tokenSupplier).map(Supplier::get).orElse(null);
|
||||||
if (bearerToken == null) {
|
if (bearerToken == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
|
|
||||||
import org.openapitools.client.auth.Authentication;
|
import org.openapitools.client.auth.Authentication;
|
||||||
@ -165,9 +166,18 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
* @param bearerToken the token
|
* @param bearerToken the token
|
||||||
*/
|
*/
|
||||||
public void setBearerToken(String bearerToken) {
|
public void setBearerToken(String bearerToken) {
|
||||||
|
setBearerToken(() -> bearerToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method to set the token supplier for HTTP bearer authentication.
|
||||||
|
*
|
||||||
|
* @param tokenSupplier the token supplier function
|
||||||
|
*/
|
||||||
|
public void setBearerToken(Supplier<String> tokenSupplier) {
|
||||||
for (Authentication auth : authentications.values()) {
|
for (Authentication auth : authentications.values()) {
|
||||||
if (auth instanceof HttpBearerAuth) {
|
if (auth instanceof HttpBearerAuth) {
|
||||||
((HttpBearerAuth) auth).setBearerToken(bearerToken);
|
((HttpBearerAuth) auth).setBearerToken(tokenSupplier);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,34 @@
|
|||||||
package org.openapitools.client.auth;
|
package org.openapitools.client.auth;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
|
|
||||||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
||||||
public class HttpBearerAuth implements Authentication {
|
public class HttpBearerAuth implements Authentication {
|
||||||
private final String scheme;
|
private final String scheme;
|
||||||
private String bearerToken;
|
private Supplier<String> tokenSupplier;
|
||||||
|
|
||||||
public HttpBearerAuth(String scheme) {
|
public HttpBearerAuth(String scheme) {
|
||||||
this.scheme = scheme;
|
this.scheme = scheme;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBearerToken() {
|
public String getBearerToken() {
|
||||||
return bearerToken;
|
return tokenSupplier.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBearerToken(String bearerToken) {
|
public void setBearerToken(String bearerToken) {
|
||||||
this.bearerToken = bearerToken;
|
this.tokenSupplier = () -> bearerToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBearerToken(Supplier<String> tokenSupplier) {
|
||||||
|
this.tokenSupplier = tokenSupplier;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
|
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
|
||||||
|
String bearerToken = Optional.ofNullable(tokenSupplier).map(Supplier::get).orElse(null);
|
||||||
if (bearerToken == null) {
|
if (bearerToken == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user