Apache Shiro框架

Shiro简介

(1)Shiro 是什么?

Apache Shiro是一个强大且易用的Java安全框架/权限框架
本质:预先定义好的权限代码(过滤器,RBAC模型设计,JSP标签等)

(2)Shiro有什么用?

  1. 身份认证/登录
  2. 授权
  3. 加密
  4. 会话管理

(3)Shiro有什么特点?

常见的权限框架有apache Shiro ,与spring Security等

Apache Shiro:

好处:设计简单,配置简单
弊端:和Spring结合,需要整合;功能没有spring Security强大

Spring Security

好处:不需要与Spring整合,功能强大
弊端:配置麻烦

Shiro可以非常容易的开发出足够好的应用,其不仅可以用在JavaSE环境,也可以用在JavaEE环境。Shiro可以帮助我们完成:认证、授权、加密、会话管理、与Web集成、缓存等。而且Shiro的API也是非常简单;其基本功能点如下图所示
在这里插入图片描述

Shiro的功能组成

四大功能

认证,授权,加密,会话管理

(1)认证:判断账号密码

Authentication:身份认证/登录。常见的认证方式: 登陆认证
认证: 用户访问系统,系统校验用户身份是否合法的过程就是认证

(2)授权:查询权限

Authorization 分配权限,查询权限
授权: 权限校验。校验用户是否有权限访问的过程就是授权
如何实现授权?

  1. 获取登陆用户的权限;
  2. 自己指定访问资源需要的权限;
  3. 判断,拿着访问资源需要的权限去用户权限列表查找如果存在,就授权访问通过。否则,拒绝访问。
(3) 加密:将密码加密保存到数据库

Cryptography,加密,保护数据的安全性,如密码加密存储到数据库,而不是明文存储

(4) 会话管理:退出功能

Session Manager,会话管理,即用户登录后就是一次会话,在没有退出之前,它的所有信息都在会话中;会话可以是普通JavaSE环境的,也可以是如Web环境的

主要架构

在这里插入图片描述
Subject:主体,可以代表用户,类似jdbc的连接
SecurityManager: 安全管理,核心,所有与安全有关的操作都会与SecurityManager交互
Realm:域,Shiro从Realm获取安全数据(如用户、角色、权限)

Shiro的工作流程

在这里插入图片描述
1、应用代码通过Subject来进行认证和授权,而Subject又委托给SecurityManager;
2、我们需要给Shiro的SecurityManager注入Realm,从而让SecurityManager能得到合法的用户及其权限进行判断。

认证流程图细化和扩充
在这里插入图片描述
Subject调SecurityManger 安全管理器
安全管理器调realm
realm访问数据库的表数据(类似5表)

Shiro环境搭建

Shiro搭建步骤介绍

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

(1)项目添加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中配置的过滤器

  <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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值