saas-export项目-apache shiro框架**

Shiro简介

  • (1)Shiro是什么?
    Apache Shiro是一个强大且易用的Java安全框架/权限框架
    本质:预先定义好的权限代码(过滤器,RBAC模型设计,JSP标签等)
  • (2)Shiro有什么用?
    执行身份验证、授权(查询有什么权限)、密码学(md5,sha1)和会话管理
  • (3)Shiro有什么特点?
    常见的权限框架有apache Shiro ,与spring Security等
    在这里插入图片描述

Shiro的功能组成

  • (1)有四大功能
    》认证,授权,加密,会话管理
  • (2)认证:判断账号密码
    Authentication:身份认证/登录。常见的认证方式: 登陆认证
    认证: 用户访问系统,系统校验用户身份是否合法的过程就是认证
  • (3)授权:查询权限
    Authorization 分配权限,查询权限
    授权: 权限校验。校验用户是否有权限访问的过程就是授权
    如何实现授权?
    1.获取登陆用户的权限;
    2.自己指定访问资源需要的权限;
    3.判断,拿着访问资源需要的权限去用户权限列表查找如果存在,就授权访问通过。否则,拒绝访问。
  • (4)加密:将密码加密保存到数据库
    Cryptography,加密,保护数据的安全性,如密码加密存储到数据库,而不是明文存储
  • (5)会话管理:退出功能
    Session Manager,会话管理,即用户登录后就是一次会话,在没有退出之前,它的所有信息都在会话中;会话可以是普通JavaSE环境的,也可以是如Web环境的
    在这里插入图片描述

Shiro架构

  • (1)Shiro架构图
    Subject:主体,代表了当前“用户”,类似jdbc的连接
    SecurityManager:安全管理器,核心,所有与安全有关的操作都会与SecurityManager交互
    Realm:域,Shiro从Realm获取安全数据(如用户、角色、权限)
  • (2)工作原理
    1、应用代码通过Subject来进行认证和授权,而Subject又委托给SecurityManager;
    2、我们需要给Shiro的SecurityManager注入Realm,从而让SecurityManager能得到合法的用户及其权限进行判断。
    在这里插入图片描述
  • Subject调SecurityManger 安全管理器
  • 安全管理器调realm
  • realm访问数据库的表数据(类似5表)

Shiro环境搭建

Shiro搭建步骤介绍

搭建shiro环境并实现认证,步骤如下:

  • (1)项目添加shiro依赖(在parent工程已经有了

shiro依赖

       <!--shiro-->
        <!--shiro和spring整合-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--shiro核心包-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.3.2</version>
        </dependency>
  • (2)配置web.xml
  • (3)配置applicationContext-shiro.xml, Spring整合shiro
  • (4)创建自定义realm类:AuthRealm,继承AuthorizingRealm
    AuthRealm extends AuthorizingRealm

web.xml中配置的过滤器

 <!-- shiro 搭建1 :配置过滤器 DelegatingFilterProxy
      配置Shiro的过滤器:拦截需要认证或授权的请求 -->
  <filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

当前过滤器会使用一个叫做过滤器工厂的对象创建多个用途过滤器
如何找到spring管理的过滤器工厂,根据filter-name的值与bean的id的值 一致

applicationContext-shiro.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--shiro搭建2  2.3 配置shiroFilter 认证或授权逻辑处理对象-->
    <!--注入:这里的shiroFilter必须和web.xml的filter-name保持一致-->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!--注入SecurityManager-->
        <property name="securityManager" ref="securityManager"/>
    </bean>

    <!--2.1.创建 SecurityManager-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!--注入Realm-->
        <property name="realm" ref="authRealm"/>
    </bean>

    <!--2.2.创建Realm-->
    <bean id="authRealm" class="com.wzx.web.shiro.AuthRealm"/>

</beans>

AuthRealm

//不需要再添加@Component 因为在xml中使用bean标签配置了
//AuthorizingRealm 认证(登录账号密码)和 授权(查有什么权限)
public class AuthRealm extends AuthorizingRealm {
    
    
    private Logger l = LoggerFactory.getLogger(this.getClass());
    //spring是通过反射调用无参构造函数
    public AuthRealm(){
    
    
        l.info("AuthRealm 无参构造函数执行了");
    }
    //授权(查有什么权限)
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    
    
        l.info("AuthRealm doGetAuthorizationInfo 函数执行了");
        return null;
    }
    //认证(登录账号密码)
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
    
    
        l.info("AuthRealm doGetAuthenticationInfo 函数执行了");
        return null;
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_40711092/article/details/109544293
今日推荐