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)
  • 使用指南
  • 名词解释
  • 快速开始

    • 使用jap-simple
    • 使用jap-social
    • 使用jap-oauth2
    • 使用jap-oidc
    • 使用jap-sso
      • 准备工作
      • 修改 service 实现类
      • 启用 SSO
      • 测试 SSO 效果
        • 重启项目,分别访问
        • 测试登录
        • 在 jap-social 中实现单点登录
      • JapSsoConfig 配置项
      • 使用 redis 托管 session
        • 添加相关依赖
        • 添加相关配置
      • 官方推荐
    • 使用jap-mfa
    • 使用jap-http-api
    • 使用jap-ldap
    • JAP 错误代码
  • IDS

  • starter

  • 指南
  • 快速开始
FuJie Team
2021-09-20

使用jap-sso

提示

jap-sso 是为了方便快速的实现单点登录(SSO)而添加的增强包,由 jap-core 默认集成。

# 准备工作

修改本地 hosts,加入以下配置

127.0.0.1 sso.jap.com
127.0.0.1 sso1.jap.com
127.0.0.1 sso2.jap.com
127.0.0.1 sso3.jap.com
1
2
3
4

# 修改 service 实现类

jap-simple、jap-social、jap-oauth2等模块实现 SSO 的方式类似,所以,这儿仅以 jap-simple 为例,其他模块参考该方法。

修改 JapSimpleUserServiceImpl 实现类,实现 getById(String) 方法。

import com.fujieid.jap.core.JapUser;
import com.fujieid.jap.core.JapUserService;

public class JapSimpleUserServiceImpl implements JapUserService {

    // ...

    /**
     * 当启用 sso 功能时,该方法必须实现
     *
     * @param userId 用户id
     * @return JapUser
     */
    @Override
    public JapUser getById(String userId) {
        return null;
    }

    // ...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

注意

当启用 sso 功能时,必须实现 getById 方法

# 启用 SSO

以 jap-simple 为例,在初始化 SimpleStrategy 时,默认方式为:

SimpleStrategy simpleStrategy = new SimpleStrategy(japUserService, new JapConfig());
1

通过修改 JapConfig 参数,开启 SSO:

SimpleStrategy simpleStrategy = new SimpleStrategy(japUserService, new JapConfig()
                .setSso(true)
                .setSsoConfig(new JapSsoConfig()
                        .setCookieDomain("jap.com")));
1
2
3
4

请注意

  • Strategy 初始化优先级要尽量高,以此项目jap-demo (opens new window)为例,推荐的创建 Strategy 的方式如下:
import com.fujieid.jap.core.JapUserService;
import com.fujieid.jap.core.context.JapAuthentication;
import com.fujieid.jap.core.result.JapResponse;
import com.fujieid.jap.demo.config.JapConfigContext;
import com.fujieid.jap.demo.util.ViewUtil;
import com.fujieid.jap.http.adapter.jakarta.JakartaRequestAdapter;
import com.fujieid.jap.http.adapter.jakarta.JakartaResponseAdapter;
import com.fujieid.jap.simple.SimpleConfig;
import com.fujieid.jap.simple.SimpleStrategy;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/simple")
public class SimpleController implements InitializingBean {

    @Resource(name = "simple")
    private JapUserService japUserService;
    private SimpleStrategy simpleStrategy;

    @GetMapping("/login")
    public String toLogin(HttpServletRequest request, HttpServletResponse response) {
        JapConfigContext.strategy = "simple";
        if (JapAuthentication.checkUser(new JakartaRequestAdapter(request), new JakartaResponseAdapter(response)).isSuccess()) {
            return "redirect:/";
        }
        return "login";
    }

    @PostMapping("/login")
    public ModelAndView renderAuth(HttpServletRequest request, HttpServletResponse response) {
        JapResponse japResponse = simpleStrategy.authenticate(new SimpleConfig()
                .setRememberMeCookieDomain("jap.com"), new JakartaRequestAdapter(request), new JakartaResponseAdapter(response));
        return ViewUtil.getView(japResponse);
    }

    /**
     * 初始化 bean 时对 SimpleStrategy 进行初始化,适用于启用了 SSO 的情况,如果没有启用 SSO,则非强制使用该方式初始化
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        simpleStrategy = new SimpleStrategy(japUserService, JapConfigContext.getConfig());
    }
}
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
  • 请不要将 domain 设置为 .jap.com,将 domain 设置为 `.jap.com` 后会报错: `java.lang.IllegalArgumentException: An invalid domain [.jap.com] was specified for this cookie`
  • 原因如下:高版本(8.5版本 +)的 tomcat 对 cookie 处理机制发生变更,原来设置 .x.com 应该修改为 x.com
  • 参考解决方案:An invalid domain [.xxx] was specified for this cookie (opens new window)

请自行修改 jap-social、jap-oauth2 、jap-oidc 模块的 controller 方法

# 测试 SSO 效果

# 重启项目,分别访问

  • http://sso.jap.com:8443/ (opens new window)
  • http://sso1.jap.com:8443/ (opens new window)
  • http://sso2.jap.com:8443/ (opens new window)
  • http://sso3.jap.com:8443/ (opens new window)

# 测试登录

任意一个窗口进行登录

登录完成后,刷新其他三个窗口

# 在 jap-social 中实现单点登录

这儿需要注意一点,本例测试域名为 sso.jap.com 等四个域名,如果需要social单点,需要修改应用回调地址为sso.jap.com域下,如:

如果单点登录失败,请检查配置的回调地址和前面 setCookieDomain 配置的 domain 是否同源

由于我们前面已经登录过,先退出后,重新选择 jap-social 方式登录

单个窗口登录成功,我们重新刷新其他三个窗口

OK, jap-social单点登录完成~~

jap-oauth2和jap-oidc和以上流程一致,本例不做演示。

# JapSsoConfig 配置项

关于 SSO 的配置项解释如下:

属性 参数类型 必填 含义 备注
cookieName String x cookie name, 默认为 _jap_sso_id
cookieDomain String x Cookie 作用于的域名, 默认为当前访问域名
cookieMaxAge int x Cookie 过期时间,默认为 Integer.MAX_VALUE
paramReturnUrl String x 登录成功后的回调url字段名 保留字段,jap 中暂时未用到
loginUrl String x 登录地址,默认为 /login 保留字段,jap 中暂时未用到
logoutUrl String x 登录地址,默认为 /logout 保留字段,jap 中暂时未用到

# 使用 redis 托管 session

针对 springboot 项目,可以使用以下方式管理 session,实现分布式 Session 管理

# 添加相关依赖

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-core</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.8.0</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 添加相关配置

# 以下为 SSO 增强配置
## 配置 sessionId 的 cookie domain
server.servlet.session.cookie.domain=jap.com
server.servlet.session.cookie.max-age=PT24H

## 基于 spring-session-data-redis 实现 session 共享
spring.session.store-type=redis
spring.session.timeout=PT24H
spring.session.redis.flush-mode=immediate

## 基于 spring-boot-starter-data-redis 配置 redis,实现 session 的分布式存储
spring.redis.database=1
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=123456ZHYD
spring.redis.lettuce.pool.min-idle=0
spring.redis.lettuce.pool.max-idle=8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 官方推荐

  • 普通示例项目:jap-demo (opens new window)
  • 前后端分离项目示例:jap-demo-vue (opens new window)
编辑 (opens new window)
#jap-sso#单点登录#sso
Last Updated: 2021/10/09, 22:47:39
使用jap-oidc
使用jap-mfa

← 使用jap-oidc 使用jap-mfa→

最近更新
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
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式