 jap-social-spring-boot-starter 模块使用帮助
jap-social-spring-boot-starter 模块使用帮助
 # jap-social-spring-boot-starter
为JustAuth Plus (opens new window) 的 social 授权策略开发的Spring Boot Starter依赖。
可访问本starter的demo (opens new window) ,包含较为详尽的调用流程和相关配置说明。
# 开源仓库地址:
# 快速开始
# 引入依赖
在你项目的 pom.xml 文件中添加jap-social的starter的maven依赖:
<dependency>
    <groupId>xyz.dong6662.jap.spring.boot</groupId>
    <artifactId>jap-social-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>
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
2
3
4
5
6
7
8
9
然后,为social策略添加它的配置信息,下面是gitee平台和阿里云RAM (opens new window)的配置信息:
# social策略
# social 缓存
jap.social.cache.type=default
# gitee平台
jap.social.gitee.platform=gitee
jap.social.gitee.state=fdefc
jap.social.gitee.just-auth-config.client-id=228d103043840b9706f04ad165726a0079c7e0263bf7c11f1205b4054ff094a9
jap.social.gitee.just-auth-config.client-secret=a06ccbdef86d193f25dc240d3e0a9038801ff3cf4c40937f2b58904c8f32a298
jap.social.gitee.just-auth-config.redirect-uri=http://localhost:8080/social/gitee
jap.social.gitee.just-auth-config.ignore-check-state=true
# 阿里云平台
# 可参考 https://help.aliyun.com/document_detail/93696.html
jap.social.aliyun.platform=aliyun
jap.social.aliyun.state=1f334ee76ba98fc5e8d62854cecf5d25P7ZKCJ5F2Dc_idp
jap.social.aliyun.just-auth-config.client-id=4330480533918027749
jap.social.aliyun.just-auth-config.client-secret=qsw5CWGXHAb4kGW9loPWWTO7i3xgzDALcCpVxDCCVCVXSHNN8BbG7NFji7O4J6XO
jap.social.aliyun.just-auth-config.redirect-uri=http://localhost:8080/social/aliyun
jap.social.aliyun.just-auth-config.ignore-check-state=true
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
social策略会缓存已经登录的AuthUser实例,JustAuth Plus框架提供了三种缓存类型,稍后会提到如何更改。
# 实现JapUserService接口
与jap-social实现JapUserService接口 (opens new window)不同的是,🎈你不仅需要为该实现类添加@Service注解,还需要对该注解添加参数JapUserServiceType.SOCIAL,表明这是social策略的实现类:
@Service(JapUserServiceType.SOCIAL)
public class SocialUserServiceImpl implements JapUserService {
    /**
     * 模拟 DB 操作
     */
    private static List<JapUser> userDatas = Lists.newArrayList();
    /**
     * 根据第三方平台标识(platform)和第三方平台的用户 uid 查询数据库
     *
     * @param platform 第三方平台标识
     * @param uid      第三方平台的用户 uid
     * @return JapUser
     */
    @Override
    public JapUser getByPlatformAndUid(String platform, String uid) {
        // FIXME 注意:此处仅作演示用,并没有判断 platform,实际业务系统中需要根据 platform 和 uid 进行获取唯一用户
        return userDatas.stream().filter(user -> user.getUserId().equals(uid)).findFirst().orElse(null);
    }
    /**
     * 创建并获取第三方用户,相当于第三方登录成功后,将授权关系保存到数据库(开发者业务系统中 social user -> sys user 的绑定关系)
     *
     * @param userInfo JustAuth 中的 AuthUser
     * @return JapUser
     */
    @Override
    public JapUser createAndGetSocialUser(Object userInfo) {
        AuthUser authUser = (AuthUser) userInfo;
        // 查询绑定关系,确定当前用户是否已经登录过业务系统
        JapUser japUser = this.getByPlatformAndUid(authUser.getSource(), authUser.getUuid());
        if (null == japUser) {
            japUser = createJapUser();
            japUser.setAdditional(authUser);
            userDatas.add(japUser);
        }
        return japUser;
    }
    /**
     * 模拟创建用户
     *
     * @return JapUser
     */
    private JapUser createJapUser() {
        JapUser user = new JapUser();
        user.setUserId("1");
        user.setUsername("jap");
        user.setPassword("jappassword");
        return user;
    }
}
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
当然,也可以在 application.properties 中指定social策略的JapUserService实现类,即指定该实现类的包全名(binary name):
jap.social-user-service=xyz.dong6662.japspringbootstarterdemo.service.SocialUserServiceImpl
# 实现Controller
@RestController
@RequestMapping("/social")
public class SocialController {
    @Autowired
    SocialStrategy socialStrategy;
    @Autowired
    SocialProperties socialProperties;
    @RequestMapping("/aliyun")
    public JapResponse authenticate(HttpServletRequest request, HttpServletResponse response){
        return socialStrategy.authenticate(socialProperties.getSocial().get("aliyun"),request,response);
    }
}
2
3
4
5
6
7
8
9
10
11
12
13
# 使用JapTemplate
这是为了简化四种授权策略的调用而开发的starter依赖。只需在引入了jap-social的maven依赖的基础上,引入 JapTemplate的依赖便可使用:
<dependency>
    <groupId>xyz.dong6662.jap.spring.boot</groupId>
    <artifactId>jap-spring-boot-starter-template</artifactId>
    <version>1.0.0</version>
</dependency>
2
3
4
5
这样,在Controller中的授权代码只需一行便可完成授权:
@RestController
@RequestMapping("/social")
public class SocialController {
    @Autowired
    JapTemplate japTemplate;
    @Autowired
    JapUserStore japUserStore;
    @RequestMapping(method = RequestMethod.GET, path = "/gitee")
    public JapResponse giteeCallback() {
        return japTemplate.opsForSocial().authenticate("gitee");//进行授权
    }
    @RequestMapping("/gitee/user-info")
    public JapResponse userInfo(HttpServletRequest request, HttpServletResponse response){
        // japUserStore实现类为SessionJapUserStore时,获取的是当前会话保存的AuthUser
        AuthUser authUser = (AuthUser) japUserStore.get(request, response).getAdditional();
        AuthToken token = authUser.getToken();
        return japTemplate.opsForSocial().getUserInfo("gitee", token);
    }
    @RequestMapping("/gitee/refresh-token")
    public JapResponse refreshToken(HttpServletRequest request, HttpServletResponse response){
        // 当前会话保存的AuthUser
        AuthUser authUser = (AuthUser) japUserStore.get(request, response).getAdditional();
        AuthToken token = authUser.getToken();
        // FIXME: 2021/10/10 AuthGiteeRequest中没有实现refresh方法,此处暂无法测试
        return japTemplate.opsForSocial().refreshToken("gitee",token);
    }
    // FIXME: 2021/10/10 同refreshToken, AuthGiteeRequest(me.zhyd.oauth.request包下)中也没有实现revoke,故暂不测试revokeToken
}
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
# 引入Redis缓存
social授权策略会缓存state,对state的描述可参考jap-oauth2:oauthconfig-配置项 (opens new window):
客户端用于维护请求和回调之间的状态的不透明值。授权服务器在将用户代理重定向回客户端时包含此值
默认采用JustAuth提供的缓存,本starter支持Redis作为缓存。若采用Redis,则需要引入Redis的Spring Boot Starter:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2
3
4
并在application.properties中完成redis的一些基本配置:
# redis基础配置
spring.redis.port=6379
spring.redis.host=127.0.0.1
spring.redis.timeout=3m
2
3
4
然后指定Redis作为state的缓存即可:
# social 缓存类型
jap.social.cache.type=redis
2
