使用jap-simple
提示
jap-simple 是为了方便快速的集成本地账号密码登录而添加的增强包
# 常规使用
# 添加依赖
<dependency>
<groupId>com.fujieid</groupId>
<artifactId>jap-simple</artifactId>
<version>{latest-version}</version>
</dependency>
1
2
3
4
5
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
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
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
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)
Last Updated: 2021/10/09, 22:47:39