Git Product home page Git Product logo

ymate-module-oauth-connector's Introduction

YMP-OAuth-Connector

基于YMP框架实现的第三方OAuth授权登录模块, 目前已实现:

授权名称 状态
github 通过
weibo 通过
baidu 通过
qq 通过
wechat 通过
gitee 通过
oschina 通过
google -
linkedin -
windowslive -

Maven包依赖

<dependency>
    <groupId>net.ymate.module</groupId>
    <artifactId>ymate-module-oauth-connector</artifactId>
    <version>1.0.1</version>
</dependency>

搭建模块工程

  • 首先,你需要创建一个基于YMPv2框架的JavaWeb工程项目;(如何快速搭建工程?

  • YMP框架扫描包路径要包含net.ymate.module.oauth.connector, 调整配置如下:

      # 框架自动扫描的包路径集合,多个包名之间用'|'分隔,默认已包含net.ymate.platform包,其子包也将被扫描
      ymp.autoscan_packages=net.ymate
    

模块配置参数说明

#-------------------------------------
# module.oauth.connector 模块初始化参数
#-------------------------------------

# 缓存名称前缀, 默认值: ""
ymp.configs.module.oauth.connector.cache_name_prefix=

# 连接授权回调处理器, 默认值为net.ymate.cms.oauth.connector.impl.DefaultConnectCallbackHandler
ymp.configs.module.oauth.connector.callback_handler_class=

# 密码类参数是否已加密, 默认值: false
ymp.configs.module.oauth.connector.password_encrypted=

# 密码处理器, 可选参数, 用于对已加密OAuth客户端密钥进行解密,默认值: 空
ymp.configs.module.oauth.connector.password_class=

#--------------------------
# 以下为各连接器的初始化配置, xxx表示连接器名称, 目前支持[github|wechat|qq|weibo|gitee|oschina|baidu]
#--------------------------

# OAuth客户端ID, 必选项
ymp.configs.module.oauth.connector.xxx.client_id=

# OAuth客户端密钥, 必选项
ymp.configs.module.oauth.connector.xxx.client_secret=

# 重定向URL地址, 可选参数, 默认值: 空
ymp.configs.module.oauth.connector.xxx.redirect_uri=

启动并测试

  • 申请并配置好OAuth客户端ID和密钥(访问https://github.com/settings/developers进行申请);

  • 在申请过程中需要填写redirect_uri地址, 请填写: http://<你的域名>/oauth2/connect/github/redirect;

  • 打包工程并部署到目标服务器;

  • 打开浏览器并输入网址, 如: http://<你的域名>/oauth2/connect/github 进行授权测试;

  • 如果OAuth请求授权成功,则默认callback_handler_class处理器将为你显示回应的JSON数据, 接下来请自行编写IOAuthConnectCallbackHandler接口实现来存储授权信息吧:)

One More Thing

YMP不仅提供便捷的Web及其它Java项目的快速开发体验,也将不断提供更多丰富的项目实践经验。

感兴趣的小伙伴儿们可以加入 官方QQ群480374360,一起交流学习,帮助YMP成长!

了解更多有关YMP框架的内容,请访问官网:http://www.ymate.net/

ymate-module-oauth-connector's People

Contributors

suninformation avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ymate-module-oauth-connector's Issues

QQ 登陆返回 openId或access_token 的获取解析应该存在问题。

QQ参考地址

获取access_token它返回回来的值 应该是 access_token=FE04************************CCE2&expires_in=7776000&refresh_token=88E4************************BE14

应该覆写 doParseConnectResponseBody

 @Override
    protected JSONObject doParseConnectResponseBody(Response response) throws Exception {
        if (response != null && response.isSuccessful()) {
            JSONObject result;
            String body = response.body().string();
            if (StringUtils.startsWith(body, "callback")) {
                result = JSON.parseObject(StringUtils.substringBetween(body, "callback(", ");"));
            } else {
                //access_token=DA63BF6EB946DFD8D428ECFF8CE424E6&expires_in=7776000&refresh_token=786729E3D4001CA99D4881DF94BFA9B1
                Map<String, String> queryPairs = splitQuery(body);
                result = new JSONObject();
                for (Map.Entry<String, String> entry : queryPairs.entrySet()) {
                    result.put(entry.getKey(), entry.getValue());
                }
            }
            if (result.containsKey("error")) {
                throw new RuntimeException(result.toJSONString());
            }
            return result;
        }
        return null;
    }

    public static Map<String, String> splitQuery(String query) throws UnsupportedEncodingException {
        System.out.println("body:" + query);
        Map<String, String> query_pairs = new LinkedHashMap<>();
        String[] pairs = query.split("&");
        for (String pair : pairs) {
            int idx = pair.indexOf("=");
            query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
        }
        return query_pairs;
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.