Skip to content

Commit

Permalink
⚡ v1.5版本 升级
Browse files Browse the repository at this point in the history
  • Loading branch information
click33 committed Dec 15, 2020
1 parent 19e81b2 commit 6e10bce
Show file tree
Hide file tree
Showing 26 changed files with 286 additions and 416 deletions.
360 changes: 21 additions & 339 deletions LICENSE

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<p align="center">
<img alt="logo" src="http://sa-token.dev33.cn/doc/logo.png" width="150" height="150" style="margin-bottom: 10px;">
<img alt="logo" src="https://gitee.com/sz6/sa-token/raw/master/sa-token-doc/doc/logo.png" width="150" height="150" style="margin-bottom: 10px;">
</p>
<h1 align="center" style="margin: 30px 0 30px; font-weight: bold;">sa-token v1.4.0</h1>
<h1 align="center" style="margin: 30px 0 30px; font-weight: bold;">sa-token v1.5.1</h1>
<h4 align="center">一个JavaWeb轻量级权限认证框架,功能全面,上手简单</h4>
<h4 align="center">
<a href="https://gitee.com/sz6/sa-token/stargazers"><img src="https://gitee.com/sz6/sa-token/badge/star.svg"></a>
<a href="https://github.com/click33/sa-token"><img src="https://img.shields.io/badge/sa--token-v1.4.0-2B9939"></a>
<a href="https://github.com/click33/sa-token"><img src="https://img.shields.io/badge/sa--token-v1.5.1-2B9939"></a>
<a href="https://github.com/click33/sa-token/stargazers"><img src="https://img.shields.io/github/stars/click33/sa-token"></a>
<a href="https://github.com/click33/sa-token/watchers"><img src="https://img.shields.io/github/watchers/click33/sa-token"></a>
<a href="https://github.com/click33/sa-token/network/members"><img src="https://img.shields.io/github/forks/click33/sa-token"></a>
Expand Down Expand Up @@ -48,7 +48,7 @@ StpUtil.checkLogin();


## 💦️️ 涵盖功能
-**登录验证** —— 轻松登录鉴权,提供个性化登录提示
-**登录验证** —— 轻松登录鉴权,并提供五种细分场景值
-**权限验证** —— 拦截违规调用,不同角色不同授权
-**自定义session会话** —— 专业的数据缓存中心
-**踢人下线** —— 将违规用户立刻清退下线
Expand Down
12 changes: 6 additions & 6 deletions sa-token-demo-springboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
</dependency>

<!-- 开发测试 -->
<dependency>
<!-- <dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-dev</artifactId>
<version>1.4.0</version>
</dependency>
<version>1.5.1</version>
</dependency> -->

<!-- sa-token 权限认证, 在线文档:http://sa-token.dev33.cn/ -->
<!-- <dependency>
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token</artifactId>
<version>1.4.0</version>
</dependency> -->
<version>1.5.1</version>
</dependency>

<!-- SpringBoot整合redis -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.session.SaSession;

/**
* sa-token持久层的实现类 , 基于redis
*/
@Component // 打开此注解,保证此类被springboot扫描,即可完成sa-token与redis的集成
//@Component // 打开此注解,保证此类被springboot扫描,即可完成sa-token与redis的集成
public class SaTokenDaoRedis implements SaTokenDao {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public void get(HttpServletRequest request) throws IOException {

}




// 全局异常拦截(拦截项目中的所有异常)
@ExceptionHandler
public AjaxJson handlerException(Exception e, HttpServletRequest request, HttpServletResponse response)
Expand All @@ -35,21 +38,8 @@ public AjaxJson handlerException(Exception e, HttpServletRequest request, HttpSe
// 不同异常返回不同状态码
AjaxJson aj = null;
if (e instanceof NotLoginException) { // 如果是未登录异常
aj = AjaxJson.getNotLogin();
// 判断具体是什么类型
NotLoginException ee = (NotLoginException) e;
if(ee.getType() == NotLoginException.NOT_TOKEN) {
aj.setMsg("未提供token");
}
if(ee.getType() == NotLoginException.INVALID_TOKEN) {
aj.setMsg("token无效");
}
if(ee.getType() == NotLoginException.BE_REPLACED) {
aj.setMsg("token已被顶下线");
}
if(ee.getType() == NotLoginException.TOKEN_TIMEOUT) {
aj.setMsg("token已过期");
}
aj = AjaxJson.getNotLogin().setMsg(ee.getMessage());
} else if(e instanceof NotPermissionException) { // 如果是权限异常
NotPermissionException ee = (NotPermissionException) e;
aj = AjaxJson.getNotJur("无此权限:" + ee.getCode());
Expand All @@ -64,5 +54,41 @@ public AjaxJson handlerException(Exception e, HttpServletRequest request, HttpSe
// response.setContentType("application/json; charset=utf-8"); // http说明,我要返回JSON对象
// response.getWriter().print(new ObjectMapper().writeValueAsString(aj));
}



// 全局异常拦截(拦截项目中的NotLoginException异常)
// @ExceptionHandler(NotLoginException.class)
// public AjaxJson handlerNotLoginException(NotLoginException nle, HttpServletRequest request, HttpServletResponse response)
// throws Exception {
//
// // 打印堆栈,以供调试
// nle.printStackTrace();
//
// // 判断场景值,定制化异常信息
// String message = "";
// if(nle.getType().equals(NotLoginException.NOT_TOKEN)) {
// message = "未提供token";
// }
// else if(nle.getType().equals(NotLoginException.INVALID_TOKEN)) {
// message = "token无效";
// }
// else if(nle.getType().equals(NotLoginException.TOKEN_TIMEOUT)) {
// message = "token已过期";
// }
// else if(nle.getType().equals(NotLoginException.BE_REPLACED)) {
// message = "token已被顶下线";
// }
// else if(nle.getType().equals(NotLoginException.KICK_OUT)) {
// message = "token已被踢下线";
// }
// else {
// message = "当前会话未登录";
// }
//
// // 返回给前端
// return AjaxJson.getError(message);
// }


}
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,34 @@ public AjaxJson getInfo() {




// 测试踢人下线 浏览器访问: http://localhost:8081/test/kickOut
@RequestMapping("kickOut")
public AjaxJson kickOut() {
// 先登录上
StpUtil.setLoginId(10001);
// 清退下线
// StpUtil.logoutByLoginId(10001);
// 踢下线
StpUtil.kickoutByLoginId(10001);
// 再尝试获取
StpUtil.getLoginId();
// 返回
return AjaxJson.getSuccess();
}


// 测试 浏览器访问: http://localhost:8081/test/test
@RequestMapping("test")
public AjaxJson test() {
StpUtil.setLoginId(10001);
// StpUtil.getSession();
StpUtil.logout();

// System.out.println(StpUtil.getSession().getId());
// System.out.println(StpUserUtil.getSession().getId());
StpUtil.getSessionByLoginId(10001).setAttribute("name", "123");
System.out.println(StpUtil.getSessionByLoginId(10001).getAttribute("name"));
// StpUtil.getSessionByLoginId(10001).setAttribute("name", "123");
// System.out.println(StpUtil.getSessionByLoginId(10001).getAttribute("name"));

return AjaxJson.getSuccess();
}
Expand Down
2 changes: 1 addition & 1 deletion sa-token-dev/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>cn.dev33</groupId>
<artifactId>sa-token-dev</artifactId>
<packaging>jar</packaging>
<version>1.4.0</version>
<version>1.5.1</version>

<!-- SpringBoot -->
<parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public interface SaTokenAction {

/**
* 获取当前会话的 response
* @return
* @return 当前请求的response
*/
public HttpServletResponse getResponse();

/**
* 生成一个token
* @param loginId 账号id
* @param loginKey 登录标识key
* @return
* @return 一个token
*/
public String createToken(Object loginId, String loginKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,32 @@ public class NotLoginException extends RuntimeException {

/** 表示未提供token */
public static final String NOT_TOKEN = "-1";
public static final String NOT_TOKEN_MESSAGE = "未提供token";

/** 表示token无效 */
public static final String INVALID_TOKEN = "-2";
public static final String INVALID_TOKEN_MESSAGE = "token无效";

/** 表示token已过期 */
public static final String TOKEN_TIMEOUT = "-3";
public static final String TOKEN_TIMEOUT_MESSAGE = "token已过期";

/** 表示token已被顶下线 */
public static final String BE_REPLACED = "-3";
public static final String BE_REPLACED = "-4";
public static final String BE_REPLACED_MESSAGE = "token已被顶下线";

/** 表示token已被踢下线 */
public static final String KICK_OUT = "-5";
public static final String KICK_OUT_MESSAGE = "token已被踢下线";

/** 默认的提示语 */
public static final String DEFAULT_MESSAGE = "当前会话未登录";

/** 表示token已过期 */
public static final String TOKEN_TIMEOUT = "-4";

/**
* 代表异常token的标志集合
*/
public static final List<String> ABNORMAL_LIST = Arrays.asList(NOT_TOKEN, INVALID_TOKEN, BE_REPLACED, TOKEN_TIMEOUT);
public static final List<String> ABNORMAL_LIST = Arrays.asList(NOT_TOKEN, INVALID_TOKEN, TOKEN_TIMEOUT, BE_REPLACED, KICK_OUT);


/**
Expand Down Expand Up @@ -76,15 +88,47 @@ public String getLoginKey() {
// this(StpUtil.stpLogic.loginKey);
// }


/**
* 创建一个
* @param loginKey login_key
* 创建一个
* @param message 异常消息
* @param loginKey loginKey
* @param type 类型
*/
public NotLoginException(String loginKey, String type) {
public NotLoginException(String message, String loginKey, String type) {
// 这里到底要不要拼接上login_key呢?纠结
super("当前会话未登录");
super(message);
this.loginKey = loginKey;
this.type = type;
}

/**
* 静态方法构建一个NotLoginException
* @param loginKey loginKey
* @param type 场景类型
* @return 构建完毕的异常对象
*/
public static NotLoginException newInstance(String loginKey, String type) {
String message = null;
if(type.equals(NOT_TOKEN)) {
message = NOT_TOKEN_MESSAGE;
}
else if(type.equals(INVALID_TOKEN)) {
message = INVALID_TOKEN_MESSAGE;
}
else if(type.equals(TOKEN_TIMEOUT)) {
message = TOKEN_TIMEOUT_MESSAGE;
}
else if(type.equals(BE_REPLACED)) {
message = BE_REPLACED_MESSAGE;
}
else if(type.equals(KICK_OUT)) {
message = KICK_OUT_MESSAGE;
}
else {
message = DEFAULT_MESSAGE;
}
return new NotLoginException(message, loginKey, type);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class SaSession implements Serializable {

/**
* 构建一个 session对象
* @param id
* @param id sessionId
*/
public SaSession(String id) {
this.id = id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public boolean isExists(String sessionId) {
* 获取指定key的session
* @param sessionId key
* @param isCreate 如果没有,是否新建并返回
* @return
* @return SaSession
*/
public static SaSession getSessionById(String sessionId, boolean isCreate) {
SaSession session = SaTokenManager.getDao().getSaSession(getSessionKey(sessionId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class SpringSaToken {

/**
* 获取配置Bean
* @return
* @return .
*/
@Bean
@ConfigurationProperties(prefix="spring.sa-token")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface StpInterface {
* 返回指定login_id所拥有的权限码集合
* @param loginId 账号id
* @param loginKey 具体的stp标识
* @return
* @return .
*/
public List<Object> getPermissionCodeList(Object loginId, String loginKey);

Expand Down
Loading

0 comments on commit 6e10bce

Please sign in to comment.