Web应用中权限管理是个很重要的部分。本文结合SpringBoot security和Jwt(Json Web Token)实现基于RBAC(Role-Based Access Control)的权限管理。ps:千万不要被这些新名词吓到。
目录
SpringBoot security
SpringBoot security是SpringBoot提供的访问控制框架。参考链接
Jwt(Json Web Token)
- Jwt是基于token的认证方式,不同于session的用户认证,token认证方式不需要在服务器内存中存储session信息,在服务扩展的时候无需考虑session共享的问题,因此使用Jwt可以很方便对服务进行扩展。
- Jwt本质是使用非对称加密得到的一串字符串,因此Jwt是跨语言的,Jwt字符串在不同语言之间传输时,如果拥有密钥就可以对Jwt进行解密。参考链接
RBAC
Role-Based Access Control 中文译义:基于角色的权限控制。可以简单理解为不同用户属于不同角色,而角色绑定了具体的权限或资源。给用户分配角色,则是对用户进行权限控制,在验证用户的时候,只需要获取用户角色,就能够了解到用户有没有相应权限。RBAC的使用简化了用户和权限的关系,方便扩展、易于维护。参考链接
新建SpringBoot security项目
导入maven管理的jar
项目依赖SpringBoot2.0.3、MySQL5.7、mybatis、jjwt0.9.0、tk.mybatis.mapper工具类、lombok工具类等。我这里引入工具类是为了方便开发。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>