Apache Shrio安全框架实现原理及实例详解

 更新时间:2020年04月04日 14:26:15   作者:狼_少_年  
这篇文章主要介绍了Apache Shrio安全框架实现原理及实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、Shiro整体概述

1.简介

Apache Shiro是Java的一个安全框架,功能强大,使用简单,Shiro为开发人员提供了一个直观而全面的认证(登录),授权(判断是否含有权限),加密(密码加密)及会话管理(Shiro内置Session)的解决方案.

2.Shiro组件

3.Shiro架构

3.1 外部架构(以应用程序角度)

3.2 内部架构

4. Shiro的过滤器

过滤器简称

对应的java类

anon

org.apache.shiro.web.filter.authc.AnonymousFilter

authc

org.apache.shiro.web.filter.authc.FormAuthenticationFilter

authcBasic

org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter

perms

org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter

port

org.apache.shiro.web.filter.authz.PortFilter

rest

org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter

roles

org.apache.shiro.web.filter.authz.RolesAuthorizationFilter

ssl

org.apache.shiro.web.filter.authz.SslFilter

user

org.apache.shiro.web.filter.authc.UserFilter

logout

org.apache.shiro.web.filter.authc.LogoutFilter

挑几个重要的说明一下:

anon:匿名过滤器,不登录也可以访问的资源使用,比如首页,一些静态资源等

authc:认证过滤器,登录成功后才能访问的资源使用

perms:授权过滤器,必须具备某种权限才能访问

roles:角色过滤器,必须具备某种角色才能访问

注意:这么多过滤器,使用起来肯定不方便,Shiro框架也考虑到了这一点,所以有一个过滤器,一个顶十个,即DelegatingFilterProxy.

5. Shiro与Spring整合

5.1 pom.xml

<!--shiro和spring整合-->
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-spring</artifactId>
      <version>1.3.2</version>
    </dependency>

5.2 web.xml

<filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
      <param-name>targetFilterLifecycle</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

5.3applicationContext-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" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  <!--安全管理器,需要注入realm域,如果有缓存配置,还需要注入缓存管理器-->
  <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <!--引用自定义的realm -->
    <property name="realm" ref="authRealm"/>
    <!--引入缓存管理器-->
    <property name="cacheManager" ref="cacheManager"/>
  </bean>

  <!-- 自定义Realm域的编写 -->
  <bean id="authRealm" class="com.itheima.web.shiro.AuthRealm">
    <!-- 注入自定义的密码比较器 -->
    <property name="credentialsMatcher" ref="customerCredentialsMatcher"></property>
  </bean>

  <!-- 自定义的密码比较器 -->
  <bean id="customerCredentialsMatcher" class="com.itheima.web.shiro.CustomCredentialsMatcher"></bean>

  <!--缓存配置-->
  <!--内置(windows)缓存配置:MemoryConstrainedCacheManager-->
  <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean>

  <!-- filter-name这个名字的值来自于web.xml中filter的名字 -->
  <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager"/>
    <!--登录页面 -->
    <property name="loginUrl" value="/login.jsp"></property>
    <!-- 登录失败后 -->
    <property name="unauthorizedUrl" value="/unauthorized.jsp"></property>

    <property name="filterChainDefinitions">
      <!-- /**代表下面的多级目录也过滤 -->
      <value>
        /system/module/list.do = perms["模块管理"]<!--路径和模块名称一定要和数据库表中存储的数据一致-->
        /index.jsp* = anon<!--anon 不登录也可以访问的资源-->
        /login.jsp* = anon
        /login* = anon
        /logout* = anon
        /css/** = anon
        /img/** = anon
        /plugins/** = anon
        /make/** = anon
        /** = authc
      </value>
    </property>
  </bean>

  <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
  <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

  <!-- 生成代理,通过代理进行控制 -->
  <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
     depends-on="lifecycleBeanPostProcessor">
    <property name="proxyTargetClass" value="true"/>
  </bean>

  <!-- 安全管理器 -->
  <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    <property name="securityManager" ref="securityManager"/>
  </bean>

  <!--支持Shiro注解配置-->
  <aop:aspectj-autoproxy proxy-target-class="true"/>

</beans>

5.4 如果想看具体的实现代码(含sql脚本),可以点击页面右上角Fork me on github,到我的github仓库中拉取

仓库地址:  https://github.com/AdilCao/Shiro.git

代码部分只需要关注三个类:

1.LoginController(登录,在这里获取Subject主体,调用subject.login()方法后直接调用认证方法)

2.AuthRealm(认证和授权在这个类中定义,认证成功后调用密码比较器进行比较;授权即查找登录用户所具有的权限模块集合)

3.CustomCredentialsMatcher(密码比较器,将浏览器输入明文密码加密后,与数据库中的安全密码进行比较)

注意:整个过程中如果登录不成功,就会抛出异常

相关文章

  • 简单剖析Java中动态线程池的扩容以及缩容操作

    简单剖析Java中动态线程池的扩容以及缩容操作

    这篇文章主要为大家详细介绍了Java中动态线程池的扩容以及缩容操作的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-01-01
  • SpringBoot配置Redis实现保存获取和删除数据

    SpringBoot配置Redis实现保存获取和删除数据

    本文主要介绍了SpringBoot配置Redis实现保存获取和删除数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • SpringBoot事件发布和监听详解

    SpringBoot事件发布和监听详解

    今天去官网查看spring boot资料时,在特性中看见了系统的事件及监听章节,所以下面这篇文章主要给大家介绍了关于SpringBoot事件发布和监听的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2021-11-11
  • springboot2启动时执行,初始化(或定时任务)servletContext问题

    springboot2启动时执行,初始化(或定时任务)servletContext问题

    这篇文章主要介绍了springboot2启动时执行,初始化(或定时任务)servletContext问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • MyBatisPlus 封装分页方法示例

    MyBatisPlus 封装分页方法示例

    本文主要介绍了基于MybatisPlus的分页插件封装,包括分页结果对象、查询对象的封装,以及对象转换处理,具有一定的参考价值,感兴趣的可以了解一下
    2024-12-12
  • 基于Java解决华为机试之字符串加解密 

    基于Java解决华为机试之字符串加解密 

    这篇文章主要介绍了基于Java解决华为机试之字符串加解密,问题描述展开主题即详细代码的分享完成文章内容,具有一的的参考价值,需要的小伙伴可以参考一下。希望对你有所帮助
    2022-02-02
  • Springboot深入讲解nocos的整合与使用

    Springboot深入讲解nocos的整合与使用

    Nacos 是阿里巴巴推出来的一个新开源项目,这是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台,在项目开发过程中,我们经常使用nacos作为配置中心和注册中心。本文章我们就从代码层面研究下springboot是如何整合nacos使用的
    2022-07-07
  • JAVA使用SimpleDateFormat类表示时间代码实例

    JAVA使用SimpleDateFormat类表示时间代码实例

    这篇文章主要介绍了JAVA使用SimpleDateFormat类表示时间代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • Java设计模式之策略模式案例详解

    Java设计模式之策略模式案例详解

    策略模式(Strategy Pattern)定义了一组同类型的算法,在不同的类中封装起来,每种算法可以根据当前场景相互替换,从而使算法的变化独立于使用它们的客户端即算法的调用者
    2022-07-07
  • Java线程间协作wait、notify和notifyAll详解

    Java线程间协作wait、notify和notifyAll详解

    这篇文章主要介绍了Java线程间协作wait、notify和notifyAll详解,在 Java 中可以用 wait、notify 和 notifyAll 来实现线程间的通信,尽管关于wait和notify的概念很基础,它们也都是Object类的函数,但用它们来写代码却并不简单,,需要的朋友可以参考下
    2023-10-10

最新评论