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 模块使用帮助
      • 开源仓库地址:
      • 快速开始
        • 引入依赖
        • application.properties中配置
        • 实现JapUserService接口
        • 实现Controller
      • 使用JapTemplate
    • jap-social-spring-boot-starter 模块使用帮助
    • jap-oauth2-spring-boot-starter 模块使用帮助
    • jap-oidc-spring-boot-starter 模块使用帮助
  • 指南
  • starter
FuJie Team
2021-10-12

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

# jap-simple-spring-boot-starter

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

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

# 开源仓库地址:

GitHub (opens new window)

Gitee (opens new window)

# 快速开始

# 引入依赖

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

<dependency>
    <groupId>xyz.dong6662.jap.spring.boot</groupId>
    <artifactId>jap-simple-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

然后,为simple策略添加它的配置信息,但大多数情况下采用默认即可:

# simple策略
# 通常情况采用默认配置
jap.simple.username-field=username
jap.simple.password-field=password
jap.simple.remember-me-field=rememberMe
jap.simple.remember-me-cookie-key=rememberMeCookieKey
# ...
1
2
3
4
5
6
7

# 实现JapUserService接口

与jap-simple实现JapUserService接口 (opens new window)不同的是,🎈你不仅需要为该实现类添加@Service注解,还需要对该注解添加参数JapUserServiceType.SIMPLE,表明这是simple策略的实现类:

@Service(JapUserServiceType.SIMPLE)//Service注解和必要的参数
public class SimpleUserServiceImpl implements JapUserService {
    /**
     * 模拟 DB 操作
     */
    private static final List<JapUser> userDatas = new ArrayList<>();

    static {
        // 模拟数据库中的数据
        userDatas.add(new JapUser().setUsername("jap").setPassword("jap").setUserId("jap"));
        for (int i = 0; i < 10; i++) {
            userDatas.add(new JapUser().setUsername("jap" + i).setPassword("jap" + i).setUserId(UUID.fastUUID().toString()));
        }
    }

    @Override
    public JapUser getByName(String username) {
        return userDatas.stream()
                .filter((user) -> user.getUsername().equals(username))
                .findFirst()
                .orElse(null);
    }

    @Override
    public boolean validPassword(String password, JapUser user) {
        return user.getPassword().equals(password);
    }
}
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

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

jap.simple-user-service=xyz.dong6662.japspringbootstarterdemo.service.SimpleUserServiceImpl
1

# 实现Controller

@RestController
@RequestMapping("/simple")
public class SimpleController {
    @Autowired
    SimpleStrategy simpleStrategy;
    @Autowired
    SimpleProperties simpleProperties;

    @RequestMapping(method = RequestMethod.GET, path = "/1")
    public JapResponse simple2(HttpServletRequest request, HttpServletResponse response){
        return simpleStrategy.authenticate(simpleProperties.getSimple(),request,response);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 使用JapTemplate

这是为了简化四种授权策略的调用而开发的starter依赖。只需在引入了jap-simple的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("/simple")
public class SimpleController {
    @Autowired
    JapTemplate japTemplate;

    @RequestMapping(method = RequestMethod.GET, path = "/2")
    public JapResponse simple1() {
        return japTemplate.opsForSimple().authenticate();// 进行授权
    }
}
1
2
3
4
5
6
7
8
9
10
11
编辑 (opens new window)
#jap-simple#springboot#starter
Last Updated: 2021/10/21, 17:48:43
jap-spring-boot-starter 使用帮助
jap-social-spring-boot-starter 模块使用帮助

← jap-spring-boot-starter 使用帮助 jap-social-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
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式