Reputation: 3
i want get data from another application by calling resttempalte in springboot .. how to set my header for security .. how to handel security of another application
RestTemplate restTemplate = new RestTemplate(); String result = restTemplate.getForObject(uri, String.class);
System.out.println("Rest--->"+result);
Upvotes: 0
Views: 89
Reputation: 2634
User OAuth2RestTemplate to make authenticated REST requests with the credentials. Following is a sample XML based configuration to get you started:
<bean id="oAuth2RestTemplate" class="org.springframework.security.oauth2.client.OAuth2RestTemplate">
<constructor-arg ref="clientCredentialsResourceDetails"/>
<constructor-arg ref="defaultOAuth2ClientContext"/>
<property name="requestFactory" ref="httpComponentsClientHttpRequestFactory"/>
<property name="accessTokenProvider">
<bean class="org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider">
<property name="requestFactory" ref="httpComponentsClientHttpRequestFactory"/>
</bean>
</property>
</bean>
<bean id="httpComponentsClientHttpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
<constructor-arg ref="httpsClientFactory"/>
</bean>
<bean id="httpsClientFactory" class="com.finity.ssl.HttpsClientFactory">
<property name="sslKeyStorePath" value="${server.ssl.key-store:}"/>
<property name="sslKeyStorePassword" value="${server.ssl.key-store-password}"/>
<property name="sslKeyPassword" value="${server.ssl.key-password}"/>
<property name="sslTrustStorePath" value="${server.ssl.trust-store:}"/>
<property name="sslTrustStorePassword" value="${server.ssl.trust-store-password}"/>
</bean>
<bean id="clientCredentialsResourceDetails" class="org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails">
<property name="accessTokenUri" value="${auth.service.url:https://localhost:8443/auth-service}/oauth/token"/>
<property name="clientId" value="${auth.service.client.id:DUMMY_CLIENT_ID}"/>
<property name="clientSecret" value="${auth.service.client.secret:password}"/>
</bean>
<bean id="defaultOAuth2ClientContext" class="org.springframework.security.oauth2.client.DefaultOAuth2ClientContext"/>
Upvotes: 0
Reputation: 523
You can use like:
final String uri = "http://localhost:9090/TicketingSystem/caselogcontroller/getAppIds";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("authorization", <your tokens>));
String result = restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(headers) String.class);
System.out.println("Rest--->"+result);
Upvotes: 0