【Spring Boot 8】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.squareup.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;
评论