二、springboot2.0和spring5---oauth2--客户端测试2

1、新建项目,选择Create New Project

2、 下一步


3、下一步


 4、下一步

 

 5、下一步


6、 pom.xml暂时不用修改

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.chenclass</groupId>
    <artifactId>oauthclienttest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>oauthclienttest</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

7、创建3个包

8、在config包下添加类RestTemplateConfig

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;
    }
}

 9、在global包下添加类GlobalOuathToken

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";

}

10、在model包下添加类ApiToken和User

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;
    }
}

11、修改TEST

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<String, String> requestBody = new LinkedMultiValueMap<>();
        requestBody.add("grant_type", "password");
        requestBody.add("username", "admin");
        requestBody.add("password", "123456");
        //HttpEntity
        HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(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<String> request = restTemplate.getForEntity(Get_URLUSER, String.class, httpEntity);
            ResponseEntity<String> request = restTemplate.exchange(Get_URLUSER, HttpMethod.GET, new HttpEntity<Object>(headers),  String.class);
            System.out.println(request.getBody());
        }
        catch (HttpClientErrorException e) {
            e.getResponseBodyAsString();
            //   System.out.println(e.getRawStatusCode());
            System.out.println(e.getStatusCode());
            if(e.getRawStatusCode()==401)
            {
                System.out.println("401 出错 chen");
            }
        }

    }
}

12、修改端口 

server.port=8090

12、开始测试

(1)启动前面编写的Oauthserver

(2)测试oauthclient的函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值