写点什么

【Spring Boot 8】Okhttp 实现 GitHub 第三方登录

  • 2021 年 11 月 11 日
  • 本文字数:1745 字

    阅读完需:约 6 分钟

三、OkHTTP 实现 GitHub 第三方登录


=====================



四、Maven?


========


<dependencies>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-thymeleaf</artifactId>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-web</artifactId>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-devtools</artifactId>


<scope>runtime</scope>


<optional>true</optional>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-test</artifactId>


<scope>test</scope>


<exclusions>


<exclusion>


<groupId>org.junit.vintage</groupId>


<artifactId>junit-vintage-engine</artifactId>


</exclusion>


</exclusions>


</dependency>


<dependency>


<groupId>com.squareup.okhttp3</groupId>


<artifactId>okhttp</artifactId>


<version>4.3.1</version>


</dependency>


<dependency>


<groupId>com.s


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


quareup.okhttp3</groupId>


<artifactId>mockwebserver</artifactId>


<version>4.3.1</version>


</dependency>


<dependency>


<groupId>com.alibaba</groupId>


<artifactId>fastjson</artifactId>


<version>1.2.58</version>


</dependency>


</dependencies>


五、代码实例


=======



1、application.properties




server.port=8080


github.client.id=27dda83xxxxxx36043d4


github.client.secret=01f948axxxxxxef10d9132cfe6083dxxxxxx5f3503


github.redirect.uri=http://localhost:8080/callback


2、GitHubProvider.java




package life.majiang.community.provider;


import life.majiang.community.dto.AccessTokenDTO;


import life.majiang.community.dto.GitHubUser;


import com.alibaba.fastjson.JSON;


import okhttp3.*;


import org.springframework.stereotype.Component;


import java.io.IOException;


@Component


public class GitHubProvider {


private static final MediaType MediaType_JSON


= MediaType.get("application/json; charset=utf-8");


public String getAccessToken(AccessTokenDTO accessTokenDTO){


OkHttpClient client = new OkHttpClient();


RequestBody body = RequestBody.create(JSON.toJSONString(accessTokenDTO), MediaType_JSON);


Request request = new Request.Builder()


.url("https://github.com/login/oauth/access_token")


.post(body)


.build();


try (Response response = client.newCall(request).execute()) {


String resstring = response.body().string();


String token =resstring.split("&")[0]


.split("=")[1];


return token;


}catch (IOException e) {


e.printStackTrace();


}


return null;


}


public GitHubUser getUser(String AccessToken){


OkHttpClient client = new OkHttpClient();


Request request = new Request.Builder()


.url("https://api.github.com/user?access_token="+AccessToken)


.build();


try{


Response response = client.newCall(request).execute();


String res = response.body().string();


GitHubUser gitHubUser = JSON.parseObject(res, GitHubUser.class);


return gitHubUser;


} catch (IOException e) {


e.printStackTrace();


}


return null;


}


}


3、AuthorizeController.java?




package life.majiang.community.controller;


import life.majiang.community.dto.AccessTokenDTO;


import life.majiang.community.dto.GitHubUser;


import life.majiang.community.provider.GitHubProvider;


import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.beans.factory.annotation.Value;


import org.springframework.stereotype.Controller;


import org.springframework.web.bind.annotation.GetMapping;


import org.springframework.web.bind.annotation.RequestParam;


import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;


/**



    */


    @Controller


    public class AuthorizeController {


    @Autowired

    评论

    发布
    暂无评论
    【Spring Boot 8】Okhttp实现GitHub第三方登录