Sa-Token学习笔记-07
Sa-Token学习笔记-07
账号封禁
// 先踢下线
StpUtil.kickout(10001);
// 再封禁账号
StpUtil.disable(10001, 86400);
此模块的所有内容
// 封禁指定账号
StpUtil.disable(10001, 86400);
// 获取指定账号是否已被封禁 (true=已被封禁, false=未被封禁)
StpUtil.isDisable(10001);
// 校验指定账号是否已被封禁,如果被封禁则抛出异常 `DisableServiceException`
StpUtil.checkDisable(10001);
// 获取指定账号剩余封禁时间,单位:秒,如果该账号未被封禁,则返回-2
StpUtil.getDisableTime(10001);
// 解除封禁
StpUtil.untieDisable(10001);
测试代码
package com.hayaizo.satoken.controller;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author moxiao
*/
@RestController
@RequestMapping("/disable/")
public class DisableController {
/*
* 测试步骤:
1、访问登录接口,可以正常登录 ---- http://localhost:8081/disable/login?userId=10001
2、注销登录 ---- http://localhost:8081/disable/logout
3、禁用账号 ---- http://localhost:8081/disable/disable?userId=10001
4、再次访问登录接口,登录失败 ---- http://localhost:8081/disable/login?userId=10001
5、解封账号 ---- http://localhost:8081/disable/untieDisable?userId=10001
6、再次访问登录接口,登录成功 ---- http://localhost:8081/disable/login?userId=10001
*/
// 会话登录接口 ---- http://localhost:8081/disable/login?userId=10001
@RequestMapping("login")
public SaResult login(long userId){
// 1.先检查是否封禁
StpUtil.checkDisable(userId);
// 2.检查通过后,再进行登陆
StpUtil.login(userId);
return SaResult.ok("账号登陆成功!");
}
// 会话注销接口 ---- http://localhost:8081/disable/logout
@RequestMapping("logout")
public SaResult logout(){
StpUtil.logout();
return SaResult.ok("账号退出成功");
}
// 封禁指定账号 ---- http://localhost:8081/disable/disable?userId=10001
@RequestMapping("disable")
public SaResult disable(long userId){
// 先踢下线再进行封号处理
StpUtil.kickout(userId);
StpUtil.disable(userId,180);
return SaResult.ok("账号 "+ userId + " 封禁成功");
}
@RequestMapping("untieDisable")
public SaResult untieDisable(long userId){
StpUtil.untieDisable(userId);
return SaResult.ok("账号 " + userId + " 解封成功");
}
@RequestMapping("info")
public SaResult info(){
return SaResult.ok("hello world");
}
}
拦截器
package com.hayaizo.satoken.config;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author moxiao
*/
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new SaInterceptor(handler -> {
// 跳过静态资源路径
SaRouter.match("/**")
.notMatch("/acc/doLogin")
.notMatch("/*.html")
.notMatch("/*.js")
.notMatch("/*.css")
.notMatch("/*.jpeg")
.notMatch("/*.png")
.notMatch("/*.jpg")
.check(r -> StpUtil.checkDisable(StpUtil.getLoginId()))
.check(r -> StpUtil.checkLogin());
// 用户模块鉴权
SaRouter.match("/user/**", r -> StpUtil.checkPermissionOr("user", "admin"));
// 管理员模块鉴权
SaRouter.match("/admin/**", r -> StpUtil.checkPermission("admin"));
}).isAnnotation(false)).addPathPatterns("/**");
}
}
版权申明
本文系作者 @hayaizo 原创发布在Hello World站点。未经许可,禁止转载。
暂无评论数据