下面列出了org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication#org.springframework.web.client.RestOperations 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static Iterable<Function<RestOperations, User>> execute() {
final ObjectMapper mapper = new ObjectMapper();
final RequestCallback callback = request -> {
request.getHeaders().add("Test", "true");
request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
mapper.writeValue(request.getBody(), new User("D. Fault", "1984-09-13"));
};
final ResponseExtractor<User> extractor = response ->
mapper.readValue(response.getBody(), User.class);
return Arrays.asList(
unit -> unit.execute("/departments/{id}/users", POST, callback, extractor, 1),
unit -> unit.execute("/departments/{id}/users", POST, callback, extractor, singletonMap("id", 1)),
unit -> unit.execute(URI.create("/departments/1/users"), POST, callback, extractor)
);
}
@Override
public VaultHealth doWithRestOperations(RestOperations restOperations) {
try {
ResponseEntity<VaultHealthImpl> healthResponse = restOperations.exchange("sys/health", HttpMethod.GET,
emptyNamespace(null), VaultHealthImpl.class);
return healthResponse.getBody();
}
catch (RestClientResponseException responseError) {
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(responseError.getResponseBodyAsString(), VaultHealthImpl.class);
}
catch (Exception jsonError) {
throw responseError;
}
}
}
public static RestOperations createCommonsHttpRestTemplate(int maxConnPerRoute, int maxConnTotal,
int connectTimeout, int soTimeout, int retryTimes, RetryPolicyFactory retryPolicyFactory) {
HttpClient httpClient = HttpClientBuilder.create()
.setMaxConnPerRoute(maxConnPerRoute)
.setMaxConnTotal(maxConnTotal)
.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(soTimeout).build())
.setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(connectTimeout).build())
.build();
ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
//set jackson mapper
for (HttpMessageConverter<?> hmc : restTemplate.getMessageConverters()) {
if (hmc instanceof MappingJackson2HttpMessageConverter) {
ObjectMapper objectMapper = createObjectMapper();
MappingJackson2HttpMessageConverter mj2hmc = (MappingJackson2HttpMessageConverter) hmc;
mj2hmc.setObjectMapper(objectMapper);
}
}
return (RestOperations) Proxy.newProxyInstance(RestOperations.class.getClassLoader(),
new Class[]{RestOperations.class},
new RetryableRestOperationsHandler(restTemplate, retryTimes, retryPolicyFactory));
}
@Test
public void retryableRestOperationsFailAndRetrySuccessTest() throws InterruptedException {
ctx.close();
RestOperations restOperations = RestTemplateFactory.createCommonsHttpRestTemplate(10, 100, 5000, 5000, 30,
RetryPolicyFactories.newRestOperationsRetryPolicyFactory(100));
Thread appStartThread = new Thread(new Runnable() {
@Override
public void run() {
logger.info(remarkableMessage("New SpringApplication"));
SpringApplication app2 = new SpringApplication(SimpleTestSpringServer.class);
app2.setBannerMode(Mode.OFF);
ctx = app2.run("");
ctx.start();
}
});
appStartThread.start();
String response = restOperations.getForObject(generateRequestURL("/test"), String.class);
assertEquals(targetResponse, response);
appStartThread.join();
}
/**
* Perform a read action within a callback that gets access to a session-bound
* {@link RestOperations} object. {@link HttpStatusCodeException} with
* {@link HttpStatus#NOT_FOUND} are translated to a {@literal null} response.
* @param callback must not be {@literal null}.
* @return can be {@literal null}.
*/
@Nullable
<T> T doRead(Function<RestOperations, ResponseEntity<T>> callback) {
return this.vaultOperations.doWithSession((restOperations) -> {
try {
return callback.apply(restOperations).getBody();
}
catch (HttpStatusCodeException e) {
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
return null;
}
throw VaultResponses.buildException(e, this.path);
}
});
}
@Test
public void expiredTokenTests() throws Exception {
JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).keyID("one").build();
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
.subject("test-subject")
.expirationTime(Date.from(Instant.now().minusSeconds(3600)))
.build();
SignedJWT signedJWT = signedJwt(keyGeneratorUtils.getPrivateKey(), header, claimsSet);
List<OAuth2TokenValidator<Jwt>> validators = new ArrayList<>();
validators.add(new JwtTimestampValidator());
DelegatingOAuth2TokenValidator<Jwt> validator = new DelegatingOAuth2TokenValidator<Jwt>(validators);
RestOperations operations = mockRestOperations();
FirebaseJwtTokenDecoder decoder = new FirebaseJwtTokenDecoder(operations, "https://spring.local", validator);
assertThatExceptionOfType(JwtException.class)
.isThrownBy(() -> decoder.decode(signedJWT.serialize()))
.withMessageStartingWith("An error occurred while attempting to decode the Jwt: Jwt expired at");
}
@Test
public void validTokenTests() throws Exception {
JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).keyID("one").build();
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
.subject("test-subject")
.audience("123456")
.expirationTime(Date.from(Instant.now().plusSeconds(36000)))
.issuer("https://securetoken.google.com/123456")
.issueTime(Date.from(Instant.now().minusSeconds(3600)))
.claim("auth_time", Instant.now().minusSeconds(3600).getEpochSecond())
.build();
SignedJWT signedJWT = signedJwt(keyGeneratorUtils.getPrivateKey(), header, claimsSet);
List<OAuth2TokenValidator<Jwt>> validators = new ArrayList<>();
validators.add(new JwtTimestampValidator());
validators.add(new JwtIssuerValidator("https://securetoken.google.com/123456"));
validators.add(new FirebaseTokenValidator("123456"));
DelegatingOAuth2TokenValidator<Jwt> validator = new DelegatingOAuth2TokenValidator<Jwt>(validators);
RestOperations operations = mockRestOperations();
FirebaseJwtTokenDecoder decoder = new FirebaseJwtTokenDecoder(operations, "https://spring.local", validator);
Jwt jwt = decoder.decode(signedJWT.serialize());
assertThat(jwt.getClaims()).isNotEmpty();
}
@Test
public void invalidIssuedAt() throws Exception {
JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).keyID("one").build();
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
.subject("test-subject")
.audience("123456")
.expirationTime(Date.from(Instant.now().plusSeconds(36000)))
.issuer("https://securetoken.google.com/123456")
.issueTime(Date.from(Instant.now().plusSeconds(3600)))
.claim("auth_time", Instant.now().minusSeconds(3600).getEpochSecond())
.build();
SignedJWT signedJWT = signedJwt(keyGeneratorUtils.getPrivateKey(), header, claimsSet);
List<OAuth2TokenValidator<Jwt>> validators = new ArrayList<>();
validators.add(new JwtTimestampValidator());
validators.add(new JwtIssuerValidator("https://securetoken.google.com/123456"));
validators.add(new FirebaseTokenValidator("123456"));
DelegatingOAuth2TokenValidator<Jwt> validator = new DelegatingOAuth2TokenValidator<Jwt>(validators);
RestOperations operations = mockRestOperations();
FirebaseJwtTokenDecoder decoder = new FirebaseJwtTokenDecoder(operations, "https://spring.local", validator);
assertThatExceptionOfType(JwtException.class)
.isThrownBy(() -> decoder.decode(signedJWT.serialize()))
.withMessageStartingWith("An error occurred while attempting to decode the Jwt: iat claim header must be in the past");
}
@Test
public void invalidSubject() throws Exception {
JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).keyID("one").build();
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
.audience("123456")
.expirationTime(Date.from(Instant.now().plusSeconds(36000)))
.issuer("https://securetoken.google.com/123456")
.issueTime(Date.from(Instant.now().minusSeconds(3600)))
.claim("auth_time", Instant.now().minusSeconds(3600).getEpochSecond())
.build();
SignedJWT signedJWT = signedJwt(keyGeneratorUtils.getPrivateKey(), header, claimsSet);
List<OAuth2TokenValidator<Jwt>> validators = new ArrayList<>();
validators.add(new JwtTimestampValidator());
validators.add(new JwtIssuerValidator("https://securetoken.google.com/123456"));
validators.add(new FirebaseTokenValidator("123456"));
DelegatingOAuth2TokenValidator<Jwt> validator = new DelegatingOAuth2TokenValidator<Jwt>(validators);
RestOperations operations = mockRestOperations();
FirebaseJwtTokenDecoder decoder = new FirebaseJwtTokenDecoder(operations, "https://spring.local", validator);
assertThatExceptionOfType(JwtException.class)
.isThrownBy(() -> decoder.decode(signedJWT.serialize()))
.withMessageStartingWith("An error occurred while attempting to decode the Jwt: sub claim can not be empty");
}
@Test
public void retryableRestOperationsFailTest() {
ctx.close();
int retryTimes = 10;
RetryPolicyFactory mockedRetryPolicyFactory = Mockito.mock(RetryPolicyFactory.class);
RetryPolicy mockedRetryPolicy = Mockito.mock(RetryPolicy.class);
when(mockedRetryPolicyFactory.create()).thenReturn(mockedRetryPolicy);
when(mockedRetryPolicy.retry(any(Throwable.class))).thenReturn(true);
RestOperations restOperations = RestTemplateFactory.createCommonsHttpRestTemplate(10, 100, 5000, 5000,
retryTimes, mockedRetryPolicyFactory);
try {
restOperations.getForObject(generateRequestURL("/test"), String.class);
} catch (Exception e) {
verify(mockedRetryPolicy, times(retryTimes)).retry(any(Throwable.class));
// check the type of original exception
assertTrue(e instanceof ResourceAccessException);
}
}
@SuppressWarnings("unchecked")
@Before
public void setup() throws SQLException {
restOperations = EasyMock.createNiceMock(RestOperations.class);
EasyMock.expect(restOperations.postForObject(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class), EasyMock.anyObject(Class.class)))
.andReturn(VALID_METADATA);
EasyMock.replay(restOperations);
//Replace the real restOperations instance with a mock -- otherwise the call for gadget metadata would fail since
//we don't have a shindig server available to hit.
ReflectionTestUtils.setField(metadataRepository, "restOperations", restOperations);
//Setup a mock authenticated user
final User authUser = new UserImpl(VALID_USER_ID, VALID_USER_NAME);
AbstractAuthenticationToken auth = EasyMock.createNiceMock(AbstractAuthenticationToken.class);
EasyMock.expect(auth.getPrincipal()).andReturn(authUser).anyTimes();
EasyMock.replay(auth);
SecurityContext context = new SecurityContextImpl();
context.setAuthentication(auth);
SecurityContextHolder.setContext(context);
}
@Test
void shouldFailToExpandUriTemplate() {
final RestOperations unit = new HttpOperations(http);
assertThrows(IllegalArgumentException.class, () ->
unit.getForObject("/users/{id}", User.class, singletonMap("user_id", 1)));
}
private ListenableFuture<WebSocketSession> connect(RestOperations restTemplate, ClientHttpResponse... responses)
throws Exception {
RestTemplateXhrTransport transport = new RestTemplateXhrTransport(restTemplate);
transport.setTaskExecutor(new SyncTaskExecutor());
SockJsUrlInfo urlInfo = new SockJsUrlInfo(new URI("https://example.com"));
HttpHeaders headers = new HttpHeaders();
headers.add("h-foo", "h-bar");
TransportRequest request = new DefaultTransportRequest(urlInfo, headers, headers,
transport, TransportType.XHR, CODEC);
return transport.connect(request, this.webSocketHandler);
}
static Iterable<Function<RestOperations, User>> get() {
return Arrays.asList(
unit -> unit.getForObject("/users/{id}", User.class, 1),
unit -> unit.getForObject("/users/{id}", User.class, singletonMap("id", 1)),
unit -> unit.getForObject(URI.create("/users/1"), User.class),
unit -> unit.getForEntity("/users/{id}", User.class, 1).getBody(),
unit -> unit.getForEntity("/users/{id}", User.class, singletonMap("id", 1)).getBody(),
unit -> unit.getForEntity(URI.create("/users/1"), User.class).getBody()
);
}
@Override
public ClientAuthentication getClientAuthentication(
VaultEnvironmentProperties vaultProperties,
RestOperations vaultRestOperations, RestOperations externalRestOperations) {
VaultEnvironmentProperties.AwsEc2Properties awsEc2 = vaultProperties.getAwsEc2();
AwsEc2AuthenticationOptions.Nonce nonce = StringUtils.hasText(awsEc2.getNonce())
? AwsEc2AuthenticationOptions.Nonce
.provided(awsEc2.getNonce().toCharArray())
: AwsEc2AuthenticationOptions.Nonce.generated();
AwsEc2AuthenticationOptions authenticationOptions = AwsEc2AuthenticationOptions
.builder().role(awsEc2.getRole()) //
.path(awsEc2.getAwsEc2Path()) //
.nonce(nonce) //
.identityDocumentUri(URI.create(awsEc2.getIdentityDocument())) //
.build();
return new AwsEc2Authentication(authenticationOptions, vaultRestOperations,
externalRestOperations);
}
/**
* Creates a {@link RestOperations} instance if the application has not yet
* defined any yet.
*
* @return the {@link RestOperations} instance.
*/
@Bean
@ConditionalOnMissingBean
public RestOperations xsuaaRestOperations() {
logger.info("auto-configures RestOperations for xsuaa requests)");
return new RestTemplate();
}
/**
* Creates a new {@link XsuaaTokenFlows} bean that applications can auto-wire
* into their controllers to perform a programmatic token flow exchange.
*
* @param xsuaaRestOperations
* - the {@link RestOperations} to use for the token flow exchange.
* @param xsuaaServiceConfiguration
* - the {@link XsuaaServiceConfiguration} to configure the Xsuaa
* Base Url.
* @return the {@link XsuaaTokenFlows} API.
*/
@Bean
@ConditionalOnBean({ XsuaaServiceConfiguration.class, RestOperations.class })
@ConditionalOnMissingBean
public XsuaaTokenFlows xsuaaTokenFlows(RestOperations xsuaaRestOperations,
XsuaaServiceConfiguration xsuaaServiceConfiguration) {
logger.debug("auto-configures XsuaaTokenFlows using restOperations of type: {}", xsuaaRestOperations);
OAuth2ServiceEndpointsProvider endpointsProvider = new XsuaaDefaultEndpoints(
xsuaaServiceConfiguration.getUaaUrl());
ClientCredentials clientCredentials = new ClientCredentials(xsuaaServiceConfiguration.getClientId(),
xsuaaServiceConfiguration.getClientSecret());
OAuth2TokenService oAuth2TokenService = new XsuaaOAuth2TokenService(xsuaaRestOperations);
return new XsuaaTokenFlows(oAuth2TokenService, endpointsProvider, clientCredentials);
}
static Iterable<Consumer<RestOperations>> delete() {
return Arrays.asList(
unit -> unit.delete("/users/{id}", 1),
unit -> unit.delete("/users/{id}", singletonMap("id", 1)),
unit -> unit.delete(URI.create("/users/1"))
);
}
/**
* Create a new {@link VaultKvAccessStrategy} given {@link RestOperations},
* {@code baseUrl}, and {@code version}.
* @param rest must not be {@literal null}.
* @param baseUrl the Vault base URL.
* @param version version of the Vault key-value backend.
* @return the access strategy.
*/
public static VaultKvAccessStrategy forVersion(RestOperations rest, String baseUrl,
int version) {
switch (version) {
case 1:
return new V1VaultKvAccessStrategy(baseUrl, rest);
case 2:
return new V2VaultKvAccessStrategy(baseUrl, rest);
default:
throw new IllegalArgumentException(
"No support for given Vault k/v backend version " + version);
}
}
public PagingIterator(RestOperations restOperations, URI uri, Class<E> responseType) {
Assert.notNull(restOperations, "restOperations should not be null");
Assert.notNull(uri, "URI should not be null");
Assert.notNull(responseType, "ResponseType should never be null");
this.restOperations = restOperations;
this.uri = uri;
this.responseType = responseType;
}
@Bean(name = FILE_REST_OPERATIONS)
public RestOperations fileRestOperations() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setConnectTimeout(20000);
factory.setReadTimeout(20000);
RestTemplate restTemplate = (RestTemplate) RestTemplateFactory.getRestOperations(factory);
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
return restTemplate;
}
@Override
public CredentialProvider create(final SocialProperties properties) {
if (properties instanceof UnconfiguredProperties) {
return new OAuth2CredentialProvider<>(OAUTH_2);
}
if (!(properties instanceof OAuth2ConnectorProperties)) {
throw new IllegalArgumentException(String.format("Unsupported social properties instance - " +
"expected properties of type %s, but found %s", OAuth2ConnectorProperties.class, properties.getClass()));
}
final OAuth2ConnectorProperties oauth2Properties = (OAuth2ConnectorProperties) properties;
final String appId = oauth2Properties.getAppId();
final String appSecret = oauth2Properties.getAppSecret();
final String authorizationUrl = oauth2Properties.getAuthorizationUrl();
final String authenticationUrl = oauth2Properties.getAuthenticationUrl();
final String accessTokenUrl = oauth2Properties.getAccessTokenUrl();
final boolean useParametersForClientCredentials = oauth2Properties.isUseParametersForClientCredentials();
final TokenStrategy tokenStrategy = oauth2Properties.getTokenStrategy();
final String scope = oauth2Properties.getScope();
final OAuth2ServiceProvider<RestOperations> serviceProvider = new GenericOAuth2ServiceProvider(appId, appSecret, authorizationUrl,
authenticationUrl, accessTokenUrl, useParametersForClientCredentials, tokenStrategy);
final OAuth2ConnectionFactory<RestOperations> connectionFactory = new OAuth2ConnectionFactory<>(OAUTH_2, serviceProvider, null);
connectionFactory.setScope(scope);
final OAuth2Applicator applicator = new OAuth2Applicator(properties);
applicator.setAccessTokenProperty("accessToken");
applicator.setAccessTokenExpiresAtProperty("accessTokenExpiresAt");
applicator.setRefreshTokenProperty("refreshToken");
applicator.setClientIdProperty("clientId");
applicator.setClientSecretProperty("clientSecret");
return new OAuth2CredentialProvider<>(OAUTH_2, connectionFactory, applicator, oauth2Properties.getAdditionalQueryParameters());
}
@Test
public void unsignedTokenTests() {
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
.subject("test-subject")
.expirationTime(Date.from(Instant.now().plusSeconds(60)))
.build();
PlainJWT plainJWT = new PlainJWT(claimsSet);
FirebaseJwtTokenDecoder decoder = new FirebaseJwtTokenDecoder(mock(RestOperations.class), "https://spring.local", mock(OAuth2TokenValidator.class));
assertThatExceptionOfType(JwtException.class)
.isThrownBy(() -> decoder.decode(plainJWT.serialize()))
.withMessageStartingWith("An error occurred while attempting to decode the Jwt");
}
@ParameterizedTest
@MethodSource("head")
void shouldHead(final Function<RestOperations, HttpHeaders> test) {
driver.addExpectation(onRequestTo("/users/1").withMethod(Method.HEAD),
giveEmptyResponse().withHeader("Test", "true"));
final HttpHeaders headers = test.apply(new HttpOperations(http));
assertEquals("true", headers.getFirst("Test"));
}
private RestOperations buildOAuth2RestTemplate(final String tokenName, final ResponseErrorHandler errorHandler) {
final RestTemplate restTemplate = new StupsOAuth2RestTemplate(
new StupsTokensAccessTokenProvider(tokenName, accessTokens),
new HttpComponentsClientHttpRequestFactory());
if (errorHandler != null) {
restTemplate.setErrorHandler(errorHandler);
}
return restTemplate;
}
private RestOperations mockRestOperations() throws Exception {
Map<String, String> payload = new HashMap<>();
payload.put("one", keyGeneratorUtils.getPublicKeyCertificate());
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CACHE_CONTROL, CacheControl.maxAge(3600L, TimeUnit.SECONDS).getHeaderValue());
ResponseEntity<Map<String, String>> response = new ResponseEntity<>(payload, headers, HttpStatus.OK);
return mockRestOperations(response);
}
private RestOperations mockRestOperations(ResponseEntity<Map<String, String>> response) {
RestOperations mock = mock(RestOperations.class);
when(mock.exchange(eq("https://spring.local"),
eq(HttpMethod.GET),
isNull(),
eq(new ParameterizedTypeReference<Map<String, String>>() { })))
.thenReturn(response);
return mock;
}
private ListenableFuture<WebSocketSession> connect(RestOperations restTemplate, ClientHttpResponse... responses)
throws Exception {
RestTemplateXhrTransport transport = new RestTemplateXhrTransport(restTemplate);
transport.setTaskExecutor(new SyncTaskExecutor());
SockJsUrlInfo urlInfo = new SockJsUrlInfo(new URI("http://example.com"));
HttpHeaders headers = new HttpHeaders();
headers.add("h-foo", "h-bar");
TransportRequest request = new DefaultTransportRequest(urlInfo, headers, headers,
transport, TransportType.XHR, CODEC);
return transport.connect(request, this.webSocketHandler);
}
@Override
public ClientAuthentication getClientAuthentication(
VaultEnvironmentProperties vaultProperties,
RestOperations vaultRestOperations, RestOperations externalRestOperations) {
Assert.hasText(vaultProperties.getToken(),
missingPropertyForAuthMethod("token", AuthenticationMethod.TOKEN));
return new TokenAuthentication(vaultProperties.getToken());
}
@Override
public ClientAuthentication getClientAuthentication(
VaultEnvironmentProperties vaultProperties,
RestOperations vaultRestOperations, RestOperations externalRestOperations) {
VaultEnvironmentProperties.PcfProperties pcfProperties = vaultProperties.getPcf();
assertClassPresent("org.bouncycastle.crypto.signers.PSSSigner",
missingClassForAuthMethod("BouncyCastle", "bcpkix-jdk15on",
AuthenticationMethod.PCF));
Assert.hasText(pcfProperties.getRole(),
missingPropertyForAuthMethod("pcf.role", AuthenticationMethod.PCF));
PcfAuthenticationOptions.PcfAuthenticationOptionsBuilder builder = PcfAuthenticationOptions
.builder().role(pcfProperties.getRole()).path(pcfProperties.getPcfPath());
if (pcfProperties.getInstanceCertificate() != null) {
builder.instanceCertificate(new ResourceCredentialSupplier(
pcfProperties.getInstanceCertificate()));
}
if (pcfProperties.getInstanceKey() != null) {
builder.instanceKey(
new ResourceCredentialSupplier(pcfProperties.getInstanceKey()));
}
return new PcfAuthentication(builder.build(), vaultRestOperations);
}