重构 sa-token-jwt 插件

This commit is contained in:
click33 2022-04-30 08:21:04 +08:00
parent 7f93c2086d
commit d45d404fa0
9 changed files with 24 additions and 34 deletions

View File

@ -12,7 +12,7 @@ public class ApiDisabledException extends SaTokenException {
private static final long serialVersionUID = 6806129545290130133L;
/** 异常提示语 */
public static final String BE_MESSAGE = "This API is disabled";
public static final String BE_MESSAGE = "this api is disabled";
/**
* 一个异常代表 API 已被禁用

View File

@ -26,7 +26,7 @@ public class NotLoginException extends SaTokenException {
/** 表示未提供token */
public static final String NOT_TOKEN = "-1";
public static final String NOT_TOKEN_MESSAGE = "提供Token";
public static final String NOT_TOKEN_MESSAGE = "能读取到有效Token";
/** 表示token无效 */
public static final String INVALID_TOKEN = "-2";

View File

@ -14,9 +14,10 @@ public interface SaTokenListener {
* 每次登录时触发
* @param loginType 账号类别
* @param loginId 账号id
* @param tokenValue 本次登录产生的 token
* @param loginModel 登录参数
*/
public void doLogin(String loginType, Object loginId, SaLoginModel loginModel);
public void doLogin(String loginType, Object loginId, String tokenValue, SaLoginModel loginModel);
/**
* 每次注销时触发

View File

@ -17,7 +17,7 @@ public class SaTokenListenerDefaultImpl implements SaTokenListener {
* 每次登录时触发
*/
@Override
public void doLogin(String loginType, Object loginId, SaLoginModel loginModel) {
public void doLogin(String loginType, Object loginId, String tokenValue, SaLoginModel loginModel) {
println("账号[" + loginId + "]登录成功");
}

View File

@ -350,7 +350,7 @@ public class StpLogic {
setLastActivityToNow(tokenValue);
// $$ 通知监听器账号xxx 登录成功
SaManager.getSaTokenListener().doLogin(loginType, id, loginModel);
SaManager.getSaTokenListener().doLogin(loginType, id, tokenValue, loginModel);
// 检查此账号会话数量是否超出最大值
if(config.getMaxLoginCount() != -1) {

View File

@ -10,7 +10,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<version>2.5.12</version>
<relativePath/>
</parent>
@ -22,13 +22,12 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Http请求工具 -->
<dependency>
<groupId>com.ejlchina</groupId>
<artifactId>okhttps</artifactId>
<version>3.1.1</version>
<!-- Http 请求工具 -->
<dependency>
<groupId>com.dtflys.forest</groupId>
<artifactId>forest-spring-boot-starter</artifactId>
<version>1.5.19</version>
</dependency>
</dependencies>

View File

@ -1,14 +1,12 @@
package com.pj.sso;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.Map;
import java.util.Random;
import com.ejlchina.okhttps.OkHttps;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.dtflys.forest.Forest;
import com.pj.sso.util.AjaxJson;
/**
@ -62,18 +60,9 @@ public class SsoRequestUtil {
* @param url 请求地址
* @return 返回的结果
*/
@SuppressWarnings("unchecked")
public static AjaxJson request(String url) {
String body = OkHttps.sync(url)
.post()
.getBody()
.toString();
try {
Map<String, Object> map = new ObjectMapper().readValue(body, Map.class);
return new AjaxJson(map);
} catch (IOException e) {
throw new RuntimeException(e);
}
Map<String, Object> map = Forest.post(url).executeAsMap();
return new AjaxJson(map);
}
/**

View File

@ -55,7 +55,7 @@ public class SaJwtUtil {
// 构建
String token = JWT.create()
.setPayload(LOGIN_ID, loginId)
// 混入随机字符
// 混入随机字符防止每次生成的 token 都是一样的
.setPayload("rn", SaFoxUtil.getRandomString(32))
.addPayloads(extraData)
.setKey(keyt.getBytes())

View File

@ -82,11 +82,13 @@ public class StpLogicJwtForStateless extends StpLogic {
// ------------------- 登录相关操作 -------------------
/**
* 会话登录并指定所有登录参数Model
* 创建指定账号id的登录会话
* @param id 登录id建议的类型long | int | String
* @param loginModel 此次登录的参数Model
* @return 返回会话令牌
*/
@Override
public void login(Object id, SaLoginModel loginModel) {
public String createLoginSession(Object id, SaLoginModel loginModel) {
SaTokenException.throwByNull(id, "账号id不能为空");
// ------ 1初始化 loginModel
@ -95,11 +97,10 @@ public class StpLogicJwtForStateless extends StpLogic {
// ------ 2生成一个token
String tokenValue = createTokenValue(id, loginModel.getDeviceOrDefault(), loginModel.getTimeout(), loginModel.getExtraData());
// 3在当前会话写入tokenValue
setTokenValue(tokenValue, loginModel.getCookieTimeout());
// $$ 通知监听器账号xxx 登录成功
SaManager.getSaTokenListener().doLogin(loginType, id, loginModel);
SaManager.getSaTokenListener().doLogin(loginType, id, tokenValue, loginModel);
return tokenValue;
}
/**