mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-04-05 17:37:53 +08:00
重构 sa-token-jwt 插件
This commit is contained in:
parent
7f93c2086d
commit
d45d404fa0
@ -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 已被禁用
|
||||
|
@ -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";
|
||||
|
@ -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);
|
||||
|
||||
/**
|
||||
* 每次注销时触发
|
||||
|
@ -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 + "]登录成功");
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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>
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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())
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user