Skip to content

Commit

Permalink
账号封禁功能
Browse files Browse the repository at this point in the history
  • Loading branch information
click33 committed Apr 4, 2021
1 parent 37f1642 commit 89b1a2a
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cn.dev33.satoken.exception;

/**
* 一个异常:代表账号已被封禁
*
* @author kong
*/
public class DisableLoginException extends SaTokenException {

/**
* 序列化版本号
*/
private static final long serialVersionUID = 6806129545290130143L;

/** 异常标记值 */
public static final String BE_VALUE = "disable";

/** 异常提示语 */
public static final String BE_MESSAGE = "此账号已被封禁";

/**
* LoginKey
*/
private String loginKey;

/**
* 被封禁的账号id
*/
private Object loginId;

/**
* 封禁剩余时间,单位:秒
*/
private long disableTime;

/**
* 获得LoginKey
*
* @return See above
*/
public String getLoginKey() {
return loginKey;
}

/**
* 获取: 被封禁的账号id
*
* @return See above
*/
public Object getLoginId() {
return loginId;
}

/**
* 获取: 封禁剩余时间,单位:秒
* @return See above
*/
public long getDisableTime() {
return disableTime;
}

/**
* 构造方法创建一个
*
* @param loginKey loginKey
* @param loginId 被封禁的账号id
* @param disableTime 封禁剩余时间,单位:秒
*/
public DisableLoginException(String loginKey, Object loginId, long disableTime) {
super(BE_MESSAGE);
this.loginId = loginId;
this.loginKey = loginKey;
this.disableTime = disableTime;
}



}
46 changes: 45 additions & 1 deletion sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.exception.DisableLoginException;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
Expand Down Expand Up @@ -181,6 +182,8 @@ public SaTokenInfo getTokenInfo() {

// =================== 登录相关操作 ===================

// 登录与注销

/**
* 在当前会话上登录id
* @param loginId 登录id,建议的类型:(long | int | String)
Expand Down Expand Up @@ -214,6 +217,11 @@ public void setLoginId(Object loginId, boolean isLastingCookie) {
*/
public void setLoginId(Object loginId, SaLoginModel loginModel) {

// ------ 0、检查此账号是否已被封禁
if(isDisable(loginId)) {
throw new DisableLoginException(loginKey, loginId, getDisableTime(loginId));
}

// ------ 1、获取相应对象
SaTokenConfig config = getConfig();
SaTokenDao dao = SaTokenManager.getSaTokenDao();
Expand Down Expand Up @@ -353,6 +361,34 @@ public void logoutByLoginId(Object loginId, String device) {
session.logoutByTokenSignCountToZero();
}

/**
* 封禁指定账号
* <p> 此方法不会直接将此账号id踢下线,而是在对方再次登录时抛出`DisableLoginException`异常
* @param loginId 指定账号id
* @param disableTime 封禁时间, 单位: 秒 (-1=永久封禁)
*/
public void disableLoginId(Object loginId, long disableTime) {
SaTokenManager.getSaTokenDao().set(splicingKeyDisable(loginId), DisableLoginException.BE_VALUE, disableTime);
}

/**
* 指定账号是否已被封禁 (true=已被封禁, false=未被封禁)
* @param loginId 账号id
* @return see note
*/
public boolean isDisable(Object loginId) {
return SaTokenManager.getSaTokenDao().get(splicingKeyDisable(loginId)) != null;
}

/**
* 获取指定账号剩余封禁时间,单位:秒(-1=永久封禁,-2=未被封禁)
* @param loginId 账号id
* @return see note
*/
public long getDisableTime(Object loginId) {
return SaTokenManager.getSaTokenDao().getTimeout(splicingKeyDisable(loginId));
}

// 查询相关

/**
Expand Down Expand Up @@ -1129,7 +1165,15 @@ public String splicingKeySwitch() {
public String splicingKeyJustCreatedSave() {
return SaTokenConsts.JUST_CREATED_SAVE_KEY + loginKey;
}


/**
* 拼接key: 账号封禁
* @param loginId 账号id
* @return key
*/
public String splicingKeyDisable(Object loginId) {
return getConfig().getTokenName() + ":" + loginKey + ":disable:" + loginId;
}

// =================== Bean对象代理 ===================

Expand Down
28 changes: 28 additions & 0 deletions sa-token-core/src/main/java/cn/dev33/satoken/stp/StpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,35 @@ public static void logoutByLoginId(Object loginId) {
public static void logoutByLoginId(Object loginId, String device) {
stpLogic.logoutByLoginId(loginId, device);
}

/**
* 封禁指定账号
* <p> 此方法不会直接将此账号id踢下线,而是在对方再次登录时抛出`DisableLoginException`异常
* @param loginId 指定账号id
* @param disableTime 封禁时间, 单位: 秒 (-1=永久封禁)
*/
public static void disableLoginId(Object loginId, long disableTime) {
stpLogic.disableLoginId(loginId, disableTime);
}

/**
* 指定账号是否已被封禁 (true=已被封禁, false=未被封禁)
* @param loginId 账号id
* @return see note
*/
public static boolean isDisable(Object loginId) {
return stpLogic.isDisable(loginId);
}

/**
* 获取指定账号剩余封禁时间,单位:秒(-1=永久封禁,-2=未被封禁)
* @param loginId 账号id
* @return see note
*/
public static long getDisableTime(Object loginId) {
return stpLogic.getDisableTime(loginId);
}


// 查询相关

Expand Down
8 changes: 4 additions & 4 deletions sa-token-demo-springboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@
</dependency> -->

<!-- sa-token整合redis (使用jackson序列化方式) -->
<!-- <dependency>
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-dao-redis-jackson</artifactId>
<version>${sa-token-version}</version>
</dependency> -->
</dependency>

<!-- 提供redis连接池 -->
<!-- <dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency> -->
</dependency>

<!-- sa-token整合SpringAOP实现注解鉴权 -->
<!-- <dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import com.pj.util.AjaxJson;

import cn.dev33.satoken.exception.DisableLoginException;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
Expand Down Expand Up @@ -50,6 +51,9 @@ public AjaxJson handlerException(Exception e, HttpServletRequest request, HttpSe
} else if(e instanceof NotPermissionException) { // 如果是权限异常
NotPermissionException ee = (NotPermissionException) e;
aj = AjaxJson.getNotJur("无此权限:" + ee.getCode());
} else if(e instanceof DisableLoginException) { // 如果是被封禁异常
DisableLoginException ee = (DisableLoginException) e;
aj = AjaxJson.getNotJur("账号被封禁:" + ee.getDisableTime() + "秒后解封");
} else { // 普通异常, 输出:500 + 异常信息
aj = AjaxJson.getError(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,6 @@ public AjaxJson loginByDevice() {
@RequestMapping("test")
public AjaxJson test() {
System.out.println("进来了");
// StpUtil.setLoginId(10001, new SaLoginModel()
// .setDevice("PC") // 此次登录的客户端设备标识, 用于[同端互斥登录]时指定此次登录的设备名称
// .setIsLastingCookie(true) // 是否为持久Cookie(临时Cookie在浏览器关闭时会自动删除,持久Cookie在重新打开后依然存在)
// .setTimeout(60 * 60 * 24 * 7) // 指定此次登录token的有效期, 单位:秒 (如未指定,自动取全局配置的timeout值)
// );
return AjaxJson.getSuccess("访问成功");
}

Expand Down
2 changes: 1 addition & 1 deletion sa-token-doc/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ body{font-size: 16px; color: #34495E; font-family: "Source Sans Pro","Helvetica
.nav-right a:hover{color: #42B983;}

/* -------- 海报部分 --------- */
.main-box{width: 100%; /* height: 80vh; */ text-align: center; background-image: url(http://yun94.cn/static/images/comment_bg.jpg2);}
.main-box{width: 100%; /* height: 80vh; */ text-align: center;}
.main-box{}
.fenge{min-height: 90px;}
.content-box{color: #000;}
Expand Down
6 changes: 6 additions & 0 deletions sa-token-doc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ <h2 class="s-title">正在使用 Sa-Token 的公司 / 机构</h2>
<a href="https://shop.jfh.com/6089/" target="_blank">
<img src="https://oss.dev33.cn/sa-token/com/banxiawangluo.png">
</a>
<a href="https://ms.airsr.com/" target="_blank">
<img src="https://oss.dev33.cn/sa-token/com/tianquhangkong.png">
</a>
</div>
<div style="height: 10px; clear: both;"></div>
<p style="color: #666;">
Expand Down Expand Up @@ -213,6 +216,9 @@ <h2 class="s-title">友情链接</h2>
<a href="https://cubic.jiagoujishu.com/" target="_blank">
<img src="https://oss.dev33.cn/sa-token/link/cubic.png">
</a>
<a href="http://www.pearadmin.com/" target="_blank">
<img src="https://oss.dev33.cn/sa-token/link/pear-admin.png">
</a>
</div>
<div style="height: 10px; clear: both;"></div>
</div>
Expand Down

0 comments on commit 89b1a2a

Please sign in to comment.