SpringBoot uses Sa-Token to complete authority authentication

1. Design ideas

The so-called authority authentication, the core logic is to judge whether an account has the specified authority:

  • Yes, let you pass.
  • No? Then no access!

Going deep into the underlying data, each account will have a set of permission codes, and the framework will check whether the set contains the specified permission codes.

For example: the current account has a set of permission codes ["user-add", "user-delete", "user-get"]. At this time, I will verify the permissions "user-update", and the result will be: verification failed and access is prohibited .

Dynamic demo diagram:

Authentication

So now the core of the problem is:

  1. How to obtain the set of permission codes owned by an account?
  2. Which permission code needs to be verified for this operation?

Next, we will introduce how to use Sa-Token to complete the authority authentication operation in SpringBoot.

Sa-Token is a lightweight java authority authentication framework, which mainly solves a series of authority-related issues such as login authentication, authority authentication, single sign-on, OAuth2, and microservice gateway authentication. Gitee open source address: gitee.com/dromara/sa-…

First introduce the Sa-Token dependency in the project:

<!-- Sa-Token 权限认证 -->
<dependency>
    <groupId>cn.dev33</groupId>
    <artifactId>sa-token-spring-boot-starter</artifactId>
    <version>1.34.0</version>
</dependency>
复制代码

Note: If you are using SpringBoot 3.x, just sa-token-spring-boot-starterchange to sa-token-spring-boot3-starter.

2. Obtain the current account permission code set

Because the requirements of each project are different, its permission design is also ever-changing, so the operation of [get the current account permission code set] cannot be built into the framework, so Sa-Token exposes this operation to you in the form of an interface for your convenience You rewrite according to your own business logic.

All you need to do is to create a new class and implement StpInterfacethe interface , such as the following code:

/**
 * 自定义权限验证接口扩展
 */
@Component	// 保证此类被SpringBoot扫描,完成Sa-Token的自定义权限验证扩展 
public class StpInterfaceImpl implements StpInterface {

	/**
	 * 返回一个账号所拥有的权限码集合 
	 */
	@Override
	public List<String> getPermissionList(Object loginId, String loginType) {
		// 本list仅做模拟,实际项目中要根据具体业务逻辑来查询权限
		List<String> list = new ArrayList<String>();	
		list.add("101");
		list.add("user.add");
		list.add("user.update");
		list.add("user.get");
		// list.add("user.delete");
		list.add("art.*");
		return list;
	}

	/**
	 * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
	 */
	@Override
	public List<String> getRoleList(Object loginId, String loginType) {
		// 本list仅做模拟,实际项目中要根据具体业务逻辑来查询角色
		List<String> list = new ArrayList<String>();	
		list.add("admin");
		list.add("super-admin");
		return list;
	}

}
复制代码

Parameter explanation:

  • loginId: account id, which is the identification value you write StpUtil.login(id)when .
  • loginType: ID of the account system, which can be temporarily ignored here, and this concept will be explained in detail under the chapter [Multiple Account Authentication].

注意点: 类上一定要加上 @Component 注解,保证组件被 Springboot 扫描到,成功注入到 Sa-Token 框架内。

三、权限校验

启动类:

@SpringBootApplication
public class SaTokenCaseApplication {
	public static void main(String[] args) {
		SpringApplication.run(SaTokenCaseApplication.class, args); 
		System.out.println("\n启动成功:Sa-Token配置如下:" + SaManager.getConfig());
	}	
}
复制代码

然后就可以用以下api来鉴权了

// 获取:当前账号所拥有的权限集合
StpUtil.getPermissionList();

// 判断:当前账号是否含有指定权限, 返回 true 或 false
StpUtil.hasPermission("user.add");		

// 校验:当前账号是否含有指定权限, 如果验证未通过,则抛出异常: NotPermissionException 
StpUtil.checkPermission("user.add");		

// 校验:当前账号是否含有指定权限 [指定多个,必须全部验证通过]
StpUtil.checkPermissionAnd("user.add", "user.delete", "user.get");		

// 校验:当前账号是否含有指定权限 [指定多个,只要其一验证通过即可]
StpUtil.checkPermissionOr("user.add", "user.delete", "user.get");	
复制代码

扩展:NotPermissionException 对象可通过 getLoginType() 方法获取具体是哪个 StpLogic 抛出的异常

四、角色校验

在Sa-Token中,角色和权限可以独立验证

// 获取:当前账号所拥有的角色集合
StpUtil.getRoleList();

// 判断:当前账号是否拥有指定角色, 返回 true 或 false
StpUtil.hasRole("super-admin");		

// 校验:当前账号是否含有指定角色标识, 如果验证未通过,则抛出异常: NotRoleException
StpUtil.checkRole("super-admin");		

// 校验:当前账号是否含有指定角色标识 [指定多个,必须全部验证通过]
StpUtil.checkRoleAnd("super-admin", "shop-admin");		

// 校验:当前账号是否含有指定角色标识 [指定多个,只要其一验证通过即可] 
StpUtil.checkRoleOr("super-admin", "shop-admin");		
复制代码

扩展:NotRoleException 对象可通过 getLoginType() 方法获取具体是哪个 StpLogic 抛出的异常

五、拦截全局异常

有同学要问,鉴权失败,抛出异常,然后呢?要把异常显示给用户看吗?当然不可以!

你可以创建一个全局异常拦截器,统一返回给前端的格式,参考:

@RestControllerAdvice
public class GlobalExceptionHandler {
    // 全局异常拦截 
    @ExceptionHandler
    public SaResult handlerException(Exception e) {
        e.printStackTrace(); 
        return SaResult.error(e.getMessage());
    }
}
复制代码

六、权限通配符

Sa-Token允许你根据通配符指定泛权限,例如当一个账号拥有art.*的权限时,art.addart.deleteart.update都将匹配通过

// 当拥有 art.* 权限时
StpUtil.hasPermission("art.add");        // true
StpUtil.hasPermission("art.update");     // true
StpUtil.hasPermission("goods.add");      // false

// 当拥有 *.delete 权限时
StpUtil.hasPermission("art.delete");      // true
StpUtil.hasPermission("user.delete");     // true
StpUtil.hasPermission("user.update");     // false

// 当拥有 *.js 权限时
StpUtil.hasPermission("index.js");        // true
StpUtil.hasPermission("index.css");       // false
StpUtil.hasPermission("index.html");      // false
复制代码

上帝权限:当一个账号拥有 "*" 权限时,他可以验证通过任何权限码 (角色认证同理)

七、如何把权限精确到按钮级?

权限精确到按钮级的意思就是指:权限范围可以控制到页面上的每一个按钮是否显示

思路:如此精确的范围控制只依赖后端已经难以完成,此时需要前端进行一定的逻辑判断。

如果是前后端一体项目,可以参考:Thymeleaf 标签方言,如果是前后端分离项目,则:

  1. 在登录时,把当前账号拥有的所有权限码一次性返回给前端。
  2. 前端将权限码集合保存在localStorage或其它全局状态管理对象中。
  3. 在需要权限控制的按钮上,使用 js 进行逻辑判断,例如在Vue框架中我们可以使用如下写法:
<button v-if="arr.indexOf('user.delete') > -1">删除按钮</button>
复制代码

Among them: arris the permission code array owned by the current user, user.deleteis the permission code that the display button needs to have, 删除按钮and is the content that the user can only see with the permission code.

Note: The above writing method is only to provide a reference example. Different frameworks have different writing methods. You can flexibly package and call according to the project technology stack.

8. Does the front-end need authentication even if the back-end has authentication?

need!

Front-end authentication is just an auxiliary function, and these restrictions can be easily bypassed for professionals. To ensure server security, regardless of whether the front-end has performed permission verification, the back-end interface needs to perform permission verification on session requests again!

Nine, a small example to deepen the impression

New JurAuthController, copy the following code

package com.pj.cases.use;

import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;

/**
 * Sa-Token 权限认证示例 
 * 
 * @author kong
 * @since 2022-10-13
 */
@RestController
@RequestMapping("/jur/")
public class JurAuthController {

	/*
	 * 前提1:首先调用登录接口进行登录,代码在 com.pj.cases.use.LoginAuthController 中有详细解释,此处不再赘述 
	 * 		---- http://localhost:8081/acc/doLogin?name=zhang&pwd=123456
	 * 
	 * 前提2:项目实现 StpInterface 接口,代码在  com.pj.satoken.StpInterfaceImpl
	 * 		Sa-Token 将从此实现类获取 每个账号拥有哪些权限。
	 * 
	 * 然后我们就可以使用以下示例中的代码进行鉴权了 
	 */
	
	// 查询权限   ---- http://localhost:8081/jur/getPermission
	@RequestMapping("getPermission")
	public SaResult getPermission() {
		// 查询权限信息 ,如果当前会话未登录,会返回一个空集合 
		List<String> permissionList = StpUtil.getPermissionList();
		System.out.println("当前登录账号拥有的所有权限:" + permissionList);
		
		// 查询角色信息 ,如果当前会话未登录,会返回一个空集合 
		List<String> roleList = StpUtil.getRoleList();
		System.out.println("当前登录账号拥有的所有角色:" + roleList);
		
		// 返回给前端 
		return SaResult.ok()
				.set("roleList", roleList)
				.set("permissionList", permissionList);
	}
	
	// 权限校验  ---- http://localhost:8081/jur/checkPermission
	@RequestMapping("checkPermission")
	public SaResult checkPermission() {
		
		// 判断:当前账号是否拥有一个权限,返回 true 或 false
		// 		如果当前账号未登录,则永远返回 false 
		StpUtil.hasPermission("user.add");
		StpUtil.hasPermissionAnd("user.add", "user.delete", "user.get");  // 指定多个,必须全部拥有才会返回 true 
		StpUtil.hasPermissionOr("user.add", "user.delete", "user.get");	 // 指定多个,只要拥有一个就会返回 true 
		
		// 校验:当前账号是否拥有一个权限,校验不通过时会抛出 `NotPermissionException` 异常 
		// 		如果当前账号未登录,则永远校验失败 
		StpUtil.checkPermission("user.add");
		StpUtil.checkPermissionAnd("user.add", "user.delete", "user.get");  // 指定多个,必须全部拥有才会校验通过 
		StpUtil.checkPermissionOr("user.add", "user.delete", "user.get");  // 指定多个,只要拥有一个就会校验通过 
		
		return SaResult.ok();
	}

	// 角色校验  ---- http://localhost:8081/jur/checkRole
	@RequestMapping("checkRole")
	public SaResult checkRole() {
		
		// 判断:当前账号是否拥有一个角色,返回 true 或 false
		// 		如果当前账号未登录,则永远返回 false 
		StpUtil.hasRole("admin");
		StpUtil.hasRoleAnd("admin", "ceo", "cfo");  // 指定多个,必须全部拥有才会返回 true 
		StpUtil.hasRoleOr("admin", "ceo", "cfo");	  // 指定多个,只要拥有一个就会返回 true 
		
		// 校验:当前账号是否拥有一个角色,校验不通过时会抛出 `NotRoleException` 异常 
		// 		如果当前账号未登录,则永远校验失败 
		StpUtil.checkRole("admin");
		StpUtil.checkRoleAnd("admin", "ceo", "cfo");  // 指定多个,必须全部拥有才会校验通过 
		StpUtil.checkRoleOr("admin", "ceo", "cfo");  // 指定多个,只要拥有一个就会校验通过 
		
		return SaResult.ok();
	}

	// 权限通配符  ---- http://localhost:8081/jur/wildcardPermission
	@RequestMapping("wildcardPermission")
	public SaResult wildcardPermission() {
		
		// 前提条件:在 StpInterface 实现类中,为账号返回了 "art.*" 泛权限
		StpUtil.hasPermission("art.add");  // 返回 true 
		StpUtil.hasPermission("art.delete");  // 返回 true 
		StpUtil.hasPermission("goods.add");  // 返回 false,因为前缀不符合  
		
		// * 符合可以出现在任意位置,比如权限码的开头,当账号拥有 "*.delete" 时  
		StpUtil.hasPermission("goods.add");        // false
		StpUtil.hasPermission("goods.delete");     // true
		StpUtil.hasPermission("art.delete");      // true
		
		// 也可以出现在权限码的中间,比如当账号拥有 "shop.*.user" 时  
		StpUtil.hasPermission("shop.add.user");  // true
		StpUtil.hasPermission("shop.delete.user");  // true
		StpUtil.hasPermission("shop.delete.goods");  // false,因为后缀不符合 

		// 注意点:
		// 1、上帝权限:当一个账号拥有 "*" 权限时,他可以验证通过任何权限码
		// 2、角色校验也可以加 * ,指定泛角色,例如: "*.admin",暂不赘述 
		
		return SaResult.ok();
	}
}
复制代码

The code comments have been explained in detail for each step of operation, and you can perform step-by-step tests according to the access links in the comments.


References

Guess you like

Origin juejin.im/post/7225862344482865210