1 saas系统(基于SSM框架)中的问题
- 需要对用户是否登录做判断
- 访问权限的控制
- 页面元素的权限处理
2 Shiro安全框架
2.2 shiro中的概念
认证(Authentication) : 身份认证(登录)
授权(Authorization): 授权(权限验证)
加密(Cryptography): 加密算法,密码比较
会话管理 : shiro内部模拟的session
2.3 shiro的内部结构
2.4 shiro中的过滤器
shiro中内置了10个过滤器
常用过滤器
- anon:匿名过滤器,每个人都可以访问
- authc:认证过滤器,只有登录之后才能访问
- perms:授权过滤器,具有某种权限才能访问
- 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