4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
com.chenclass
oauthclienttest
0.0.1-SNAPSHOT
oauthclienttest
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
package com.chenclass.oauthclienttest.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);//ms
factory.setConnectTimeout(15000);//ms
return factory;
}
}
package com.chenclass.oauthclienttest.global;
public class GlobalOuathToken {
public static String authorization ="Basic ZGVtb0NsaWVudDpkZW1vU2VjcmV0";
public static String token ="";
public static String refresh ="";
public static String token_type ="";
public static String POST_URLBAR = "http://127.0.0.1:8080/bar";
public static String POST_URLTOKEN = "http://127.0.0.1:8080/oauth/token";
public static String Get_URLUSER = "http://localhost:8080/admin";
}
package com.chenclass.oauthclienttest.model;
public class ApiToken {
private String access_token;
private String token_type;
private String refresh_token;
private long expires_in;
private String scope;
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public String getToken_type() {
return token_type;
}
public void setToken_type(String token_type) {
this.token_type = token_type;
}
public String getRefresh_token() {
return refresh_token;
}
public void setRefresh_token(String refresh_token) {
this.refresh_token = refresh_token;
}
public long getExpires_in() {
return expires_in;
}
public void setExpires_in(long expires_in) {
this.expires_in = expires_in;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
}
package com.chenclass.oauthclienttest.model;
public class User {
private long userId;
private String userName;
private String password;
public User(long userId, String userName, String password) {
this.userId = userId;
this.userName = userName;
this.password = password;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.chenclass.oauthclienttest;
import com.chenclass.oauthclienttest.global.GlobalOuathToken;
import com.chenclass.oauthclienttest.model.ApiToken;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class OauthclienttestApplicationTests {
@Autowired
private RestTemplate restTemplate;
private static String POST_URLBAR = "http://127.0.0.1:8080/bar";
private static String POST_URLTOKEN = "http://127.0.0.1:8080/oauth/token";
private static String Get_URLUSER = "http://localhost:8080/admin";
@Test
public void contextLoads() {
}
//(4)完整测试--使用token访问数据函数
@Test
public void gettoken() {
//headers
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Authorization", GlobalOuathToken.authorization);
// requestHeaders.add("Authorization", "Basic ZGVtb0NsaWVudDpkZW1vU2VjcmV0");
//body
MultiValueMap requestBody = new LinkedMultiValueMap<>();
requestBody.add("grant_type", "password");
requestBody.add("username", "admin");
requestBody.add("password", "123456");
//HttpEntity
HttpEntity requestEntity = new HttpEntity(requestBody, requestHeaders);
RestTemplate restTemplate = new RestTemplate();
//post
try {
ApiToken apiToken = restTemplate.postForObject(GlobalOuathToken.POST_URLTOKEN, requestEntity, ApiToken.class);
GlobalOuathToken.token = apiToken.getAccess_token();
GlobalOuathToken.refresh = apiToken.getRefresh_token();
GlobalOuathToken.token_type = apiToken.getToken_type();
System.out.println(GlobalOuathToken.token );
}
catch (HttpClientErrorException e) {
e.getResponseBodyAsString();
// System.out.println(e.getRawStatusCode());
System.out.println(e.getStatusCode());
if(e.getRawStatusCode()==401)
{
System.out.println("401 出错 chen");
}
}
}
//(4)完整测试--使用token访问数据
@Test
public void resttesttoken1() {
if(GlobalOuathToken.token.equals(""))
{
gettoken();
}
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", GlobalOuathToken.token_type+" "+GlobalOuathToken.token);
// HttpEntity httpEntity = new HttpEntity(headers);
try {
// ResponseEntity request = restTemplate.getForEntity(Get_URLUSER, String.class, httpEntity);
ResponseEntity request = restTemplate.exchange(Get_URLUSER, HttpMethod.GET, new HttpEntity
server.port=8090