JA Plus 开发者文档 JA Plus 开发者文档
首页
📖 白皮书 (opens new window)
  • 名词解释
  • 快速开始

    • 使用jap-simple
    • 使用jap-social
    • 使用jap-oauth2
    • 使用jap-oidc
    • 使用jap-sso
    • 使用jap-mfa
    • 使用jap-http-api
    • 使用jap-ldap
    • 错误代码
  • IDS

    • 简介
    • 快速开始
    • 自定义scope
    • 自定义登录页面
    • 自定义确认授权页面
    • 自定义缓存
    • 自定义Token加密密钥
    • 使用PKCE模式
    • 自动授权
    • 错误代码
  • starter

    • jap-spring-boot-starter
    • jap-simple-spring-boot-starter
    • jap-social-spring-boot-starter
    • jap-oauth2-spring-boot-starter
    • jap-oidc-spring-boot-starter
  • jap-ids的demo
  • 前后端分离架构中使用JAP
  • SpringBoot中使用JAP
  • 问题反馈
  • 项目问题
  • 异常问题
  • 功能问题
  • 数据看板🔥
  • 贡献指南
  • 行为准则
  • 用户权益
  • 贡献者们
  • 社区配套 (opens new window)
  • 教程
  • 投稿
  • 资讯
  • 关于
  • 友情链接
  • 捐赠列表
  • 其他开源
  • 更新记录
收藏
GitHub (opens new window)

FuJie Team

以开源的形式赋能开发者. Just auth into any app.
首页
📖 白皮书 (opens new window)
  • 名词解释
  • 快速开始

    • 使用jap-simple
    • 使用jap-social
    • 使用jap-oauth2
    • 使用jap-oidc
    • 使用jap-sso
    • 使用jap-mfa
    • 使用jap-http-api
    • 使用jap-ldap
    • 错误代码
  • IDS

    • 简介
    • 快速开始
    • 自定义scope
    • 自定义登录页面
    • 自定义确认授权页面
    • 自定义缓存
    • 自定义Token加密密钥
    • 使用PKCE模式
    • 自动授权
    • 错误代码
  • starter

    • jap-spring-boot-starter
    • jap-simple-spring-boot-starter
    • jap-social-spring-boot-starter
    • jap-oauth2-spring-boot-starter
    • jap-oidc-spring-boot-starter
  • jap-ids的demo
  • 前后端分离架构中使用JAP
  • SpringBoot中使用JAP
  • 问题反馈
  • 项目问题
  • 异常问题
  • 功能问题
  • 数据看板🔥
  • 贡献指南
  • 行为准则
  • 用户权益
  • 贡献者们
  • 社区配套 (opens new window)
  • 教程
  • 投稿
  • 资讯
  • 关于
  • 友情链接
  • 捐赠列表
  • 其他开源
  • 更新记录
收藏
GitHub (opens new window)
  • 使用指南
  • 名词解释
  • 快速开始

  • IDS

  • starter

    • jap-spring-boot-starter 使用帮助
    • jap-simple-spring-boot-starter 模块使用帮助
    • jap-social-spring-boot-starter 模块使用帮助
    • jap-oauth2-spring-boot-starter 模块使用帮助
    • jap-oidc-spring-boot-starter 模块使用帮助
      • 开源仓库地址:
      • 快速开始
        • 引入依赖
        • application.properties中配置
        • 实现JapUserService接口
        • 实现Controller
      • 使用JapTemplate
      • 引入Redis缓存
      • 扩展阅读
  • 指南
  • starter
FuJie Team
2021-10-12

jap-oidc-spring-boot-starter 模块使用帮助

# jap-oidc-spring-boot-starter

为JustAuth Plus (opens new window) 的 oidc 授权策略开发的Spring Boot Starter依赖。

可访问本starter的demo (opens new window) ,包含较为详尽的调用流程和相关配置说明。

# 开源仓库地址:

GitHub (opens new window)

Gitee (opens new window)

# 快速开始

# 引入依赖

在你项目的 pom.xml 文件中添加jap-oidc的starter的maven依赖:

<dependency>
    <groupId>xyz.dong6662.jap.spring.boot</groupId>
    <artifactId>jap-oidc-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>
1
2
3
4
5

# application.properties中配置

首先在 application.properties 文件中完成一些基本配置,这些配置信息所有授权策略均会使用到:

# 基础配置
# 如果启启用了sso,则需要对sso进行一些配置
jap.basic.sso=false
jap.basic.cache-expire-time=12
jap.basic.token-expire-time=12
# sso
jap.sso.cookie-domain=xxx
jap.sso.cookie-max-age=xxx
jap.sso.cookie-name=xxx
1
2
3
4
5
6
7
8
9

然后,为oidc策略添加它的配置信息,这里以阿里云RAM (opens new window)为例:

# oidc策略

# 阿里云
jap.oidc.aliyun.platform=aliyun
jap.oidc.aliyun.client-id=4408723633922655083
jap.oidc.aliyun.client-secret=3u8PnaEQZjFHVzXdLcCoTAywpQEQ5anhZeW3NSO06lFYmiRuHNBzuz3FlO5u3ihP
jap.oidc.aliyun.issuer=https://oauth.aliyun.com
jap.oidc.aliyun.authorization-url=https://signin.aliyun.com/oauth2/v1/auth
jap.oidc.aliyun.callback-url=http://localhost:8080/oidc/aliyun
jap.oidc.aliyun.token-url=https://oauth.aliyun.com/v1/token
jap.oidc.aliyun.verify-state=false
jap.oidc.aliyun.grant-type=authorization_code
jap.oidc.aliyun.response-type=code
jap.oidc.aliyun.refresh-token-url=https://oauth.aliyun.com/v1/token
jap.oidc.aliyun.revoke-token-url=https://oauth.aliyun.com/v1/revoke
#jap.oidc.aliyun.scopes=aliuid,openid,profiles
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 实现JapUserService接口

与jap-oidc实现JapUserService接口不同的是,🎈你不仅需要为该实现类添加@Service注解,还需要对该注解添加参数JapUserServiceType.OIDC,表明这是oauth2策略的实现类:

@Service(JapUserServiceType.OIDC)
public class OidcUserServiceImpl implements JapUserService {

    /**
     * 模拟 DB 操作
     */
    private static final List<JapUser> userDatas = Lists.newArrayList();

    /**
     * 根据第三方平台标识(platform)和第三方平台的用户 uid 查询数据库
     *
     * @param platform 第三方平台标识
     * @param uid      第三方平台的用户 uid
     * @return JapUser
     */
    @Override
    public JapUser getByPlatformAndUid(String platform, String uid) {
        return null;
    }

    /**
     * 创建并获取第三方用户,相当于第三方登录成功后,将授权关系保存到数据库
     * (开发者业务系统中 oauth2 user -> sys user 的绑定关系)
     *
     * @param platform 第三方平台标识
     * @param userInfo 第三方返回的用户信息
     * @param tokenInfo token 信息,可以强制转换为 com.fujieid.jap.oauth2.token.AccessToken
     * @return JapUser
     */
    @Override
    public JapUser createAndGetOauth2User(String platform, Map<String, Object> userInfo, Object tokenInfo) {
        // FIXME 业务端可以对 tokenInfo 进行保存或其他操作
        AccessToken accessToken = (AccessToken) tokenInfo;
        System.out.println(JsonUtil.toJsonString(accessToken));
        // FIXME 注意:此处仅作演示用,不同的 oauth 平台用户id都不一样,
        // 此处需要开发者自己分析第三方平台的用户信息,提取出用户的唯一ID
        String uid = (String) userInfo.get("userId");
        // 查询绑定关系,确定当前用户是否已经登录过业务系统
        JapUser japUser = this.getByPlatformAndUid(platform, uid);
        if (null == japUser) {
            // 保存用户
            japUser = createJapUser();
            japUser.setAdditional(userInfo);
            userDatas.add(japUser);
        }
        return japUser;
    }

    private JapUser createJapUser() {
        JapUser user = new JapUser();
        user.setUserId("1");
        user.setUsername("justauth");
        user.setPassword("justauthpassword");
        return user;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

当然,也可以在 application.properties 中指定oidc策略的JapUserService实现类,即指定该实现类的包全名(binary name):

jap.oidc-user-service=xyz.dong6662.japspringbootstarterdemo.service.OidcUserServiceImpl
1

# 实现Controller

@RestController
@RequestMapping("/oidc")
public class OidcController {
    @Autowired
    OidcStrategy oidcStrategy;
    @Autowired
    OidcProperties oidcProperties;

    @RequestMapping("/aliyun")
    public JapResponse aliyunCallback(HttpServletRequest request, HttpServletResponse response){
        return oidcStrategy.authenticate(oidcProperties.getOidc().get("aliyun"),request,response);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 使用JapTemplate

这是为了简化四种授权策略的调用而开发的starter依赖。只需在引入了jap-oidc的maven依赖的基础上,引入 JapTemplate的依赖便可使用:

<dependency>
    <groupId>xyz.dong6662.jap.spring.boot</groupId>
    <artifactId>jap-spring-boot-starter-template</artifactId>
    <version>1.0.0</version>
</dependency>
1
2
3
4
5

这样,在Controller中的授权代码只需一行便可完成授权:

@RestController
@RequestMapping("/oidc")
public class OidcController {
    @Autowired
    JapTemplate japTemplate;

    @RequestMapping("/aliyun")
    public JapResponse aliyunCallback(){
        return japTemplate.opsForOidc().authenticate("aliyun");
    }
}
1
2
3
4
5
6
7
8
9
10
11

# 引入Redis缓存

oauth2和oidc授权策略都会缓存token,除了默认的缓存类型(default)外,本starter还支持Redis作为缓存。若采用Redis方式,需要引入Redis的Spring Boot Starter:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
1
2
3
4

并在application.properties中完成redis的一些基本配置:

# redis基础配置
spring.redis.port=6379
spring.redis.host=127.0.0.1
spring.redis.timeout=3m
1
2
3
4

然后指定Redis作为token的缓存即可:

# token缓存
jap.cache.token.type=redis
jap.cache.token.expire-time=3m
1
2
3

# 扩展阅读

阿里云文档:通过OIDC获取用户信息 (opens new window)

编辑 (opens new window)
#jap-oidc#springboot#starter
Last Updated: 2021/10/21, 17:48:43
jap-oauth2-spring-boot-starter 模块使用帮助

← jap-oauth2-spring-boot-starter 模块使用帮助

最近更新
01
经验总结:关于为 JAP 开发不同语言的 Demo 的总结
11-02
02
jap-spring-boot-starter 使用帮助
10-28
03
使用jap-ldap
10-25
更多文章>
Theme by Vdoing | Copyright © 2021-2022

友情链接:UniAdmin | 江如意的博客

Copyright © 2021-2040 FUJIE. All rights reserved. 北京符节科技有限公司版权所有 | 京ICP备2020044519号-4
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式