Apache Shiro In Easy Steps With Spring Boot(一)

简介: Apache Shiro In Easy Steps With Spring Boot(一)

Chapter 01 什么是权限控制

Section 01 - 权限控制的概念

  权限控制就是对用户访问系统的控制,按照用户的角色等控制用户可以访问的资源或者可以执行的操作,因此权限控制分为多种如功能权限控制,数据权限控制及管理权限控制

Servlet实现权限控制

  Servlet中通过实现自定义的Filter来实现权限控制,设定好哪些URL地址是需要授权才可以访问的,针对这些URL地址的访问进行拦截,校验用户信息是否符合访问该URL的资格,从而实现权限的控制,Spring Security的用户认证也是实现了Servlet中的Filter来实现的。

JavaWeb实现自定义的权限控制,这种实现方式比较简单,但是不规范,不易管理

@PostMapping("/category/add")
public ApiRestResponse addCategory(HttpSession session, @RequestBody AddCategoryReq addCategoryReq){
    if (addCategoryReq.getName() == null || addCategoryReq.getType() == null || addCategoryReq.getOrderNum() == null){
        return ApiRestResponse.error(MallExceptionEnum.NAME_NOT_NULL);
    }
    // 校验身份
    User currentUser = (User) session.getAttribute(Constant.MALL_CURRENT_USER);
    if (userService.checkAdminRole(currentUser)){
        categoryService.add(addCategoryReq);
        return ApiRestResponse.success();
    } else {
        return ApiRestResponse.error(MallExceptionEnum.NEED_ADMIN);
    }
}
复制代码

Section 02 - RBAC 和 ACL 的概念

在另一篇Spring Security 中有关于RBAC的介绍,并且通过自定义user表,role表,user和role中间表实现了RBAC权限模型,Apache Shiro框架也是基于RBAC模型来实现认证与授权的。另一种权限控制模型是ACL即访问控制列表Access Control List,ACL权限模型核心是用户与权限直接挂钩,这样就造成了权限的复杂性分散性,不宜管理。

Section 03 - Apache Shiro VS Spring Security

Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications.

Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements

Spring Security是基于Spring Framework 核心的一个 可以提供声明式的安全访问控制解决方案的框架

Apache Shiro™ is a powerful and easy-to-use Java security framework that performs authentication, authorization, cryptography, and session management. With Shiro’s easy-to-understand API, you can quickly and easily secure any application – from the smallest mobile applications to the largest web and enterprise applications.

Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码和会话管理。使用Shiro的易于理解的API,可以快速、轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序。

Spring Security基于Spring Framework可以很好的与Spring应用程序整合,且支持OAuth Apache Shiro是独立的且拥有非常简洁的API,使用更简单,但是不支持OAuth,需要单独实现

Section 04 - Apache Shiro Overview

Shiro provides the application security API to perform the following aspects (I like to call these the 4 cornerstones of application security):

  • Authentication - proving user identity, often called user ‘login’.
  • Authorization - access control
  • Cryptography - protecting or hiding data from prying eyes
  • Session Management - per-user time-sensitive state Shiro提供四大核心模块,身份认证,授权,加密,会话管理

b3ebc62435ef43f3aaffdd3a479f8a70_tplv-k3u1fbpfcp-zoom-in-crop-mark_4536_0_0_0.png

Section 05 - Core Concepts

  • Subject:用户或者程序称为主体(如用户,第三方服务,cron作业),主体去访问系统或者资源
  • SecurityManager:安全管理器,Subject的认证和授权都要在安全管理器下进行
  • Authenticator:认证器,主要负责Subject的认证
  • Realm:域,Shiro和安全数据的连接器,好比jdbc连接数据库; 通过realm获取认证授权相关信息
  • Authorizer:授权器,主要负责Subject的授权, 控制subject拥有的角色或者权限
  • Cryptography:加解密,Shiro的包含易于使用和理解的数据加解密方法,简化了很多复杂的api
  • Cache Manager:缓存管理器,比如认证或授权信息,通过缓存进行管理,提高性能

image.png


相关文章
|
2月前
|
Java 程序员 API
Springboot-swagger配置(idea社区版2023.1.4+apache-maven-3.9.3-bin)
Springboot-swagger配置(idea社区版2023.1.4+apache-maven-3.9.3-bin)
59 1
|
2月前
|
缓存 安全 Java
Shiro框架以及Spring Boot整合Shiro
Shiro框架以及Spring Boot整合Shiro
Shiro框架以及Spring Boot整合Shiro
|
2月前
|
前端开发 Java 数据库连接
Springboot-MyBatis配置-配置端口号与服务路径(idea社区版2023.1.4+apache-maven-3.9.3-bin)
Springboot-MyBatis配置-配置端口号与服务路径(idea社区版2023.1.4+apache-maven-3.9.3-bin)
33 0
|
2月前
|
安全 Java 数据库
【SpringSecurity】Spring Security 和Shiro对比
【SpringSecurity】Spring Security 和Shiro对比
95 0
|
2月前
|
开发框架 安全 Java
【Java专题_01】springboot+Shiro+Jwt整合方案
【Java专题_01】springboot+Shiro+Jwt整合方案
|
2月前
|
Java 关系型数据库 MySQL
Shiro实战+springboot集成
Shiro实战+springboot集成
17 0
|
2月前
|
安全 前端开发 程序员
Springboot-EolinkApikit一键生成注释与一键上传API接口(idea社区版2023.1.4+apache-maven-3.9.3-bin)
Springboot-EolinkApikit一键生成注释与一键上传API接口(idea社区版2023.1.4+apache-maven-3.9.3-bin)
19 0
|
2月前
|
Java API Maven
Springboot快速搭建跨域API接口(idea社区版2023.1.4+apache-maven-3.9.3-bin)
Springboot快速搭建跨域API接口(idea社区版2023.1.4+apache-maven-3.9.3-bin)
38 0
|
2月前
|
前端开发 Java 关系型数据库
【Shiro】Springboot集成Shiro
【Shiro】Springboot集成Shiro
57 1
|
3月前
|
Java
springboot如何配置使用shiro
【1月更文挑战第11天】springboot如何配置使用shiro
19 2

推荐镜像

更多