上篇说到基于sa-token的登录权限认证,本篇将介绍方法权限认证;
那么我先提供二个常见的场景:
当我们需要限制我们的增删改查方法只能在我们用户登录之后才能进行操作;
当我们需要为不同的用户添加不同的角色,而不同的角色又有不同的权限(李四有主管角色,能够对员工的考勤工作进行增删改查,而张三是员工角色,只能对考勤管理进行查看)
这个时候我们使用sa-token就可以轻轻松松的完成对不同角色施加不同的权限,而且sa-token支持注解式开发,是我们的代码简单而又易懂;
接下来通过一个·demo来加深我们对sa-token的理解;
首先我们创建一个项目并引入sa-token依赖,然后首先创建一个tb_user的表格用来储存我们的username 和password (账号跟密码)
再创建另一个表格tb_employee来储存我们的员工考勤
创建完表格之后我们建我们的实体类
user实体类这里使用了lombok的注解
package com.example.demo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("tb_user")
public class User {
@TableId(value ="id",type = IdType.ASSIGN_ID)
private int id;
@TableField(value ="user_Name")
private String userName;
@TableField(value = "user_key")
private String userKey;
}
employee的实体类如下:
package com.example.demo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;
@Data
@TableName("tb_employee")
public class Employee {
@TableId(value = "id",type = IdType.ASSIGN_ID)
private int id;
@TableField("employee_name")
private String employeeName;
@TableField("on_time")
private Date onTime;
@TableField("off_time")
private Date offTime;
@TableField("employee_days")
private int employeeDays;
}
然后我们创建我们的配置文件,主要是我们的sa-token和mysql数据库的问题的配置信息;
server:
# 端口
port: 8082
############## Sa-Token 配置 (文档: https://sa-token.cc) ##############
sa-token:
# token名称 (同时也是cookie名称)
token-name: satoken
# token有效期,单位s 默认30天, -1代表永不过期
timeout: 2592000
# token临时有效期 (指定时间内无操作就视为token过期) 单位: 秒
activity-timeout: -1
# 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
is-concurrent: true
# 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
is-share: true
# token风格
token-style: uuid
# 是否输出操作日志
is-log: false
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/hui_table?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
#url: jdbc:mysql://localhost:3306/ssp-server?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: 123456
写我们的接口类,跟实现方法:
package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.Employee;
public interface EmployeeMapper extends BaseMapper<Employee> {
}
package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
配置类:
package com.example.demo.config;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.List;
public class StpInterfaceImpl implements StpInterface {
@Override
public List<String> getPermissionList(Object loginId, String loginType) {
// 本list仅做模拟,实际项目中要根据具体业务逻辑来查询权限
List<String> list = new ArrayList<String>();
list.add("1001");
list.add("user.addEmployee");
list.add("user.updateEmployee");
list.add("user.query");
list.add("user.deletedEmployee");
List<String> arrayList = new ArrayList<String>();
list.add("1002");
list.add("user.query");
return list;
}
/**
* 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
*/
@Override
public List<String> getRoleList(Object loginId, String loginType) {
// 本list仅做模拟,实际项目中要根据具体业务逻辑来查询角色
List<String> list = new ArrayList<String>();
list.add("1001");
list.add("admin");
list.add("super-admin");
List<String> arrayList = new ArrayList<String>();
list.add("1002");
list.add("admin");
return list;
}
@RequestMapping("checkRole")
public String checkRole(){
return"当前帐号拥有的集合" +StpUtil.getRoleList();
}
}
controller实现类:
package com.example.demo.controller;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.example.demo.entity.Employee;
import com.example.demo.entity.User;
import com.example.demo.mapper.EmployeeMapper;
import com.example.demo.mapper.UserMapper;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@AllArgsConstructor
@RequestMapping("/user/")
public class EmployeeController {
private UserMapper userMapper;
private EmployeeMapper employeeMapper;
@RequestMapping("doLoginSuperAdmin")
public String doLogin(String username,String password){
if ("张三".equals(username)&&"123456".equals(password)){
StpUtil.login(1001);
return "当前是否登录"+ SaResult.ok();
}
return "当前是否登录"+SaResult.error();
}
@RequestMapping("doLoginAdmin")
public String doLoginAdmin(String username,String password){
List<User> userList=userMapper.selectList(null);
for (User u:userList) {
if(username.equals(u.getUserName())
) {if (password.equals(u.getUserKey())){
StpUtil.login(1002);
return "当前是否登录"+ SaResult.ok();
}
}
}
return "当前是否登录"+SaResult.error();
}
@RequestMapping("query")
public List<Employee> query(){
return employeeMapper.selectList(null);
}
@RequestMapping("addEmployee")
public int addEmployee(Employee employee){
return employeeMapper.insert(employee);
}
@RequestMapping("updateEmployee")
public int updateEmployee(Employee employee){
return employeeMapper.updateById(employee);
}
@RequestMapping("deletedEmployee")
public int deletedEmployee(Employee employee){
StpUtil.checkRole("super-admin");
return employeeMapper.deleteById(employee);
}
@RequestMapping("loginOut")
public String loginOut(){
StpUtil.logout(1001);
return "当前账号是否注销"+ SaResult.ok();
}
@RequestMapping("isLogin")
public String isLogin(){
return "当前是否登录"+StpUtil.isLogin();
}
}
运行之后测试我们的方法:
其中张三 123456 是超级管理员,可以修改添加我们的员工考勤:
其他的均为普通管理员,只有查看的权限: