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
      • 常规使用
        • 添加依赖
        • 实现 JapUserService 接口
        • 实现 controller
        • 测试登录
      • 更多使用方法
        • 自定义 fieldName
      • SimpleConfig 配置项
      • 官方推荐
    • 使用jap-social
    • 使用jap-oauth2
    • 使用jap-oidc
    • 使用jap-sso
    • 使用jap-mfa
    • 使用jap-http-api
    • 使用jap-ldap
    • JAP 错误代码
  • IDS

  • starter

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

使用jap-simple

提示

jap-simple 是为了方便快速的集成本地账号密码登录而添加的增强包

# 常规使用

# 添加依赖

<dependency>
    <groupId>com.fujieid</groupId>
    <artifactId>jap-simple</artifactId>
    <version>{latest-version}</version>
</dependency>
1
2
3
4
5

# 实现 JapUserService 接口

JapUserService 是 JAP 调用(操作)开发者业务系统中用户的接口, 实现 getByName 和 validPassword 方法

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

public class JapSimpleUserServiceImpl implements JapUserService {

    @Override
    public JapUser getByName(String username) {
        return new JapUser()
    }

    @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

# 实现 controller

import cn.hutool.core.util.URLUtil;
import com.fujieid.jap.core.JapUserService;
import com.fujieid.jap.core.config.JapConfig;
import com.fujieid.jap.core.context.JapAuthentication;
import com.fujieid.jap.core.result.JapResponse;
import com.fujieid.jap.http.jakarta.JakartaRequestAdapter;
import com.fujieid.jap.http.jakarta.JakartaResponseAdapter;
import com.fujieid.jap.simple.SimpleConfig;
import com.fujieid.jap.simple.SimpleStrategy;
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 org.springframework.web.servlet.view.RedirectView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

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

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

    @PostMapping("/login")
    public ModelAndView renderAuth(HttpServletRequest request, HttpServletResponse response) {
        SimpleStrategy simpleStrategy = new SimpleStrategy(japUserService, new JapConfig());
        JapResponse japResponse = simpleStrategy.authenticate(new SimpleConfig(), new JakartaRequestAdapter(request), new JakartaResponseAdapter(response));
        if (!japResponse.isSuccess()) {
            return new ModelAndView(new RedirectView("/?error=" + URLUtil.encode(japResponse.getMessage())));
        }
        if (japResponse.isRedirectUrl()) {
            return new ModelAndView(new RedirectView((String) japResponse.getData()));
        } else {
            // 登录成功,需要对用户数据进行处理
            // ...
            System.out.println(japResponse.getData());
            return new ModelAndView(new RedirectView("/"));
        }
    }
}
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

# 测试登录

启动测试项目后访问 http://127.0.0.1:8443/simple/login

进入登录页面后,使用账号密码登录:jap/jap

登录完成后返回首页

# 更多使用方法

# 自定义 fieldName

jap-simple 从 request 中获取 username、password 和 rememberMe 时,name 值默认为 username、password、rememberMe

有些时候,开发者可能需要单独定义这三个属性的 name 值,这个时候,只需要如下操作即可:

JapResponse japResponse = simpleStrategy.authenticate(new SimpleConfig()
                // 重新定义用户名的 name
                .setUsernameField("name")
                // 重新定义密码的 name
                .setPasswordField("pass")
                // 重新定义“记住我”的 name
                .setRememberMeField("remember"),
                request, response);
1
2
3
4
5
6
7
8

# SimpleConfig 配置项

参数名 参数类型 必填 解释
usernameField String × 通过 request.getParameter(usernameField) 从 request 中获取用户名,默认为“username”
passwordField String × 通过 request.getParameter(passwordField) 从 request 中获取密码,默认为"password"
rememberMeField String × 通过 request.getParameter(rememberMeField) 从 request 中获取 remember-me,默认为“rememberMe”
rememberMeCookieKey String × “记住我” 的默认 cookie 键名,默认为“_jap_remember_me“
rememberMeCookieExpire String × “记住我” cookie 的过期时间,单位:秒,默认60602430秒[一个月]
rememberMeCookieDomain String × “记住我” 适用的 cookie 域,顶级域请填写 “xx.com”,不要填写 “.xx.com”
credentialEncryptSalt String × “记住我” 功能中加密用户信息时的盐值,默认为“_jap:rememberMe“

# 官方推荐

  • 普通示例项目:jap-demo (opens new window)
  • 前后端分离项目示例:jap-demo-vue (opens new window)
编辑 (opens new window)
#jap-simple
Last Updated: 2021/10/09, 22:47:39
名词解释
使用jap-social

← 名词解释 使用jap-social→

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