OAuth2.0 Password接入说明

1.OAuth服务端需要引入pom

<dependency>
    <groupId>com.whty</groupId>
    <artifactId>whty-framework-oauth</artifactId>
</dependency>

2.项目必须配置redis,配置文件为cache.cfg
在需要进行OAuth权限校验的方法上加入注解

@RequiresOAuth

3.在客户端使用用户名和密码登陆时,使用${ucs_url}/rest/v1/oauth/password进行登录并获取token

请求参数包括:
- client_id:系统编码domainId
- client_secret:通信密钥
- username:用户名
- password:MD5密码
ucs会生成accessToken和refreshToken返回给客户端
返回参数包括:状态200-299成功,其他失败
- access_token
- refresh_token
- expires_in

JAVA使用下面的代码来发送请求

    @Test
    public void testCreateToken() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("client_id", "0b7a6f616f61417d817c9b12d2325ce0");
        params.put("client_secret", "5e063398b9da491b");
        params.put("grant_type", "password");
        params.put("response_type", "token");

        params.put("username", "admin");
        params.put("password", DigestsUtil.md5Hex("123456"));
        String result = HttpUtil.doPost(accessTokenUrl, params);
        System.out.println(result);
    }

注:access_token过期时间1小时,refresh_token过期时间24小时,expires_in返回的为access_token过期时间,单位为秒。

4.发送其他请求时
服务端:服务端接口controller方法上加入@RequiresOAuth注解
客户端:

请求参数:
- access_token
- 其他正常调用接口所需参数

JAVA使用下面的代码来发送请求

    @Test
    public void testcheckUserByUsername(){
        String uri = member_uri + "05";
        Map<String, String> params = new HashMap<String, String>();
        params.put("access_token", "171a78d53104fca629366c42c3e9806e");

        params.put("domainId", "0b7a6f616f61417d817c9b12d2325ce0");
        params.put("username", "admin");
      String result = HttpUtil.doPost(uri, params);
        System.out.println(result);
    }

5、更新token
URL:${ucs_url}/rest/v1/oauth/password

请求参数包括:
- client_id:系统编码domainId
- client_secret:通信密钥
- refresh_token:用户名
- password:MD5密码

客户端需在请求资源时需要根据expiresIn判断accessToken是否过期,如果过期需要调用更新token请求,如果refreshToken也过期,则需要重新发送token申请

代码如下:

@Test
    public void testRefreshToken() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("client_id", "0b7a6f616f61417d817c9b12d2325ce0");
        params.put("client_secret", "5e063398b9da491b");
        params.put("grant_type", "refresh_token");
        params.put("response_type", "token");
        params.put("refresh_token", "35012cc5787862d9f92f61550a097b50");
        String result = HttpUtil.doPost(accessTokenUrl, params);
        System.out.println(result);
    }

6、错误处理

返回错误信息参数
- success
- msg
例子:
{“success”:false,”msg”:”access token 无效”}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值