前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OAuth2简易实战(四)-Github社交联合登录

OAuth2简易实战(四)-Github社交联合登录

作者头像
老梁
发布2019-09-10 18:31:24
6410
发布2019-09-10 18:31:24
举报
文章被收录于专栏:Java工程师成长之路

1. OAuth2简易实战(四)-Github社交联合登录

1.1. 用到的第三方插件

代码语言:javascript
复制
https://github.com/spring-projects/spring-social-github

1.2. 测试步骤

1.2.1. 先在github上注册一个OAuth Apps

我的配置内容如下

  1. 需要注意的,这里的最后一个回调地址的配置,格式严格规定,/connect/xxx,最后的github参数对应了特定页面,后面我通过阅读源码来详细解释
  2. 注册完之后,会有一个client id和client secret,这是需要配置到程序中的

1.2.2. 属性配置

  1. applicaton.properties
代码语言:javascript
复制
spring.social.github.app-id=xxxx
spring.social.github.app-secret=xxxx
  1. 属性类
代码语言:javascript
复制
@ConfigurationProperties(prefix = "spring.social.github")
public class GitHubProperties extends SocialProperties {

}

1.2.3. social核心配置

  1. 属性配置导入,建立与github连接
代码语言:javascript
复制
@Configuration
@EnableSocial
@EnableConfigurationProperties(GitHubProperties.class)
public class GitHubConfiguration extends SocialAutoConfigurerAdapter {

    private final GitHubProperties properties;

    public GitHubConfiguration(GitHubProperties properties) {
        this.properties = properties;
    }

    @Bean
    @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
    public GitHub gitHub(ConnectionRepository repository) {
        Connection<GitHub> connection = repository
                .findPrimaryConnection(GitHub.class);
        return connection != null ? connection.getApi() : null;
    }

    @Bean
    public ConnectController connectController(
            ConnectionFactoryLocator factoryLocator,
            ConnectionRepository repository) {

        ConnectController controller = new ConnectController(
            factoryLocator, repository);
        controller.setApplicationUrl("http://localhost:8080");
        return controller;
    }

    @Override
    protected ConnectionFactory<?> createConnectionFactory() {
        return new GitHubConnectionFactory(properties.getAppId(),
                properties.getAppSecret());
    }
}

1.2.4. controller层

代码语言:javascript
复制
@Controller
public class RepositoriesController {

    @Autowired
    private GitHub github;

    @Autowired
    private ConnectionRepository connectionRepository;

    @GetMapping
    public String repositories(Model model) {
        if (connectionRepository.findPrimaryConnection(GitHub.class) == null) {
            return "redirect:/connect/github";
        }

        String name = github.userOperations().getUserProfile().getUsername();
        String username = github.userOperations().getUserProfile()
                .getUsername();
        model.addAttribute("name", name);

        String uri = "https://api.github.com/users/{user}/repos";
        GitHubRepo[] repos = github.restOperations().getForObject(uri,
                GitHubRepo[].class, username);
        model.addAttribute("repositories", Arrays.asList(repos));

        return "repositories";
    }

}
  1. 当我们请求localhost:8080 会重定向到localhost:8080/connect/github ,这又是写在哪呢?查看源代码,会发现在social-web包的ConnectController类中有
代码语言:javascript
复制
@Controller
@RequestMapping({"/connect"})
public class ConnectController implements InitializingBean {
代码语言:javascript
复制
    @RequestMapping(
        value = {"/{providerId}"},
        method = {RequestMethod.GET}
    )
    public String connectionStatus(@PathVariable String providerId, NativeWebRequest request, Model model) {
        this.setNoCache(request);
        this.processFlash(request, model);
        List<Connection<?>> connections = this.connectionRepository.findConnections(providerId);
        this.setNoCache(request);
        if (connections.isEmpty()) {
            return this.connectView(providerId);
        } else {
            model.addAttribute("connections", connections);
            return this.connectedView(providerId);
        }
    }
  1. 进入connectView方法
代码语言:javascript
复制
    protected String connectView(String providerId) {
        return this.getViewPath() + providerId + "Connect";
    }
  1. 可以看到,在这里它固定拼接了参数Connect,所以,在自己的跳转页面中需要有特定的命名规范,这里一定就是githubConnect.html了
代码语言:javascript
复制
<html>
<head>
    <title>Social Authcode</title>
</head>
<body>
    <h2>Connect to GitHub to see your repositories</h2>

    <form action="/connect/github" method="POST">
        <input type="hidden" name="scope" value="public_repo user" />

        <div class="formInfo">
            Click the button to share your repositories with <b>social-github</b>
        </div>
        <p><button type="submit">Connect to GitHub</button></p>
    </form>

</body>
</html>
  1. 显示页面如下
  1. 点击按钮进行post请求,进入源码如下
代码语言:javascript
复制
    @RequestMapping(
        value = {"/{providerId}"},
        method = {RequestMethod.POST}
    )
    public RedirectView connect(@PathVariable String providerId, NativeWebRequest request) {
        ConnectionFactory<?> connectionFactory = this.connectionFactoryLocator.getConnectionFactory(providerId);
        MultiValueMap<String, String> parameters = new LinkedMultiValueMap();
        this.preConnect(connectionFactory, parameters, request);

        try {
            return new RedirectView(this.connectSupport.buildOAuthUrl(connectionFactory, request, parameters));
        } catch (Exception var6) {
            this.sessionStrategy.setAttribute(request, "social_provider_error", var6);
            return this.connectionStatusRedirect(providerId, request);
        }
    }
  1. 层层深入后,会发现它本质还是在组装授权参数,使用的是OAuth2的授权码模式,最后组装的http请求为如下,很明显为了去获得授权码
代码语言:javascript
复制
https://github.com/login/oauth/authorize?client_id=9fc0081c3dd4f8b11f86&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fconnect%2Fgithub&scope=public_repo+user&state=e37f1891-cd45-47b4-adb4-5c541f777e60&state=48742b99-c04e-4dfd-af0a-f19b0193f1bb&state=c2737022-3cc7-4b80-92ce-fcba2ca9beb4
  1. 这最后跳转这层的代码如下,封装成buildOAuthUrl方法进行了组装
代码语言:javascript
复制
    public RedirectView connect(@PathVariable String providerId, NativeWebRequest request) {
        ConnectionFactory<?> connectionFactory = this.connectionFactoryLocator.getConnectionFactory(providerId);
        MultiValueMap<String, String> parameters = new LinkedMultiValueMap();
        this.preConnect(connectionFactory, parameters, request);

        try {
            return new RedirectView(this.connectSupport.buildOAuthUrl(connectionFactory, request, parameters));
        } catch (Exception var6) {
            this.sessionStrategy.setAttribute(request, "social_provider_error", var6);
            return this.connectionStatusRedirect(providerId, request);
        }
    }
  1. 获取授权码后,跳转github登录页面
  1. 输入用户名密码正确后立即回调到方法
代码语言:javascript
复制
    @RequestMapping(
        value = {"/{providerId}"},
        method = {RequestMethod.GET},
        params = {"code"}
    )
    public RedirectView oauth2Callback(@PathVariable String providerId, NativeWebRequest request) {
        try {
            OAuth2ConnectionFactory<?> connectionFactory = (OAuth2ConnectionFactory)this.connectionFactoryLocator.getConnectionFactory(providerId);
            Connection<?> connection = this.connectSupport.completeConnection(connectionFactory, request);
            this.addConnection(connection, connectionFactory, request);
        } catch (Exception var5) {
            this.sessionStrategy.setAttribute(request, "social_provider_error", var5);
            logger.warn("Exception while handling OAuth2 callback (" + var5.getMessage() + "). Redirecting to " + providerId + " connection status page.");
        }

        return this.connectionStatusRedirect(providerId, request);
    }
  1. 通过授权码再去取得token
  1. 再继续跳转/connect/github
代码语言:javascript
复制
 @RequestMapping(
        value = {"/{providerId}"},
        method = {RequestMethod.GET}
    )
    public String connectionStatus(@PathVariable String providerId, NativeWebRequest request, Model model) {
        this.setNoCache(request);
        this.processFlash(request, model);
        List<Connection<?>> connections = this.connectionRepository.findConnections(providerId);
        this.setNoCache(request);
        if (connections.isEmpty()) {
            return this.connectView(providerId);
        } else {
            model.addAttribute("connections", connections);
            return this.connectedView(providerId);
        }
    }
  1. 此时connections有值,进入connectedView方法
代码语言:javascript
复制
    protected String connectedView(String providerId) {
        return this.getViewPath() + providerId + "Connected";
    }
  1. 由此可以知道,下个页面我们命名也定下来了,githubConnected.html,这里简单一个点击连接,跳转到主页
代码语言:javascript
复制
<html>
    <head>
        <title>Social Authcode</title>
    </head>
    <body>
        <h2>Connected to GitHub</h2>

        <p>
            Click <a href="/">here</a> to see your repositories.
        </p>
    </body>
</html>
  1. 到此其实授权操作都已经完成了,接下来就是正式调用github需要权限的接口了,点击here

代码学习地址 https://github.com/spring2go/oauth2lab

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-03-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. OAuth2简易实战(四)-Github社交联合登录
    • 1.1. 用到的第三方插件
      • 1.2. 测试步骤
        • 1.2.1. 先在github上注册一个OAuth Apps
        • 1.2.2. 属性配置
        • 1.2.3. social核心配置
        • 1.2.4. controller层
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档