shiro安全框架搭建模板 (基于ssm框架)

博客围绕基于SSM框架的SaaS系统展开,指出系统存在用户登录判断、访问权限控制和页面元素权限处理等问题。介绍了Shiro安全框架的概念、内部结构和过滤器,还详细说明了搭建Shiro运行环境的步骤,包括配置过滤器、整合Shiro与Spring,以及创建自定义Realm域类和密码比较类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 saas系统(基于SSM框架)中的问题

  • 需要对用户是否登录做判断
  • 访问权限的控制
  • 页面元素的权限处理

2 Shiro安全框架

2.2 shiro中的概念

认证(Authentication) : 身份认证(登录)

授权(Authorization): 授权(权限验证)

加密(Cryptography): 加密算法,密码比较

会话管理 : shiro内部模拟的session

2.3 shiro的内部结构
在这里插入图片描述
2.4 shiro中的过滤器

shiro中内置了10个过滤器

常用过滤器

  1. anon:匿名过滤器,每个人都可以访问
  2. authc:认证过滤器,只有登录之后才能访问
  3. perms:授权过滤器,具有某种权限才能访问
  4. roles: 角色过滤器,具有某种角色才能访问

3 搭建Shiro的运行环境

3.1 配置web.xml中的过滤器
配置代理过滤器 : 以一当十
在export_web_manager工程的web.xml中添加shiro的代理过滤器
在这里插入图片描述
相关代码如下:

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

3.2 配置Shiro和spring的整合
将资料中的application-shiro.xml赋值到export_web_manager工程的resource/spring目录下
在这里插入图片描述

<?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">

    <description>Shiro与Spring整合</description>

    <!--安全管理器-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!-- 引用自定义的realm -->
        <property name="realm" ref="authRealm"/>
    </bean>


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

    <!-- 自定义的密码比较器 -->
    <bean id="customerCredentialsMatcher" class="cn.itcast.web.shiro.CustomCredentialsMatcher"></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
                /login.jsp* = anon
                /login* = anon
                /logout* = anon
                /css/** = anon
                /img/** = anon
                /plugins/** = anon
                /make/** = anon
                /** = authc,user
                /*.* = 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>

    <aop:aspectj-autoproxy proxy-target-class="true"/>

</beans>

3.3 创建自定义的Realm域类和自定义的密码比较类
3.3.1 自定义realm域
在export_web_manager工程的cn.itcast.web.shiro包下创建一个AuthRealm
自定义realm域需要继承AuthorizingRealm抽象父类,并实现其中的两个方法
在这里插入图片描述
在这里插入图片描述
3.3.2 自定义密码比较器
在export_web_manager工程的cn.itcast.web.shiro包下创建一个CustomCredentialsMatcher

自定义密码比较器需要继承SimpleCredentialsMatcher

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值