1、Shiro模块组成:
(1)Application Code:用户代码,代表一个Shiro的启动入口,用Shiro的API来启动,可以理解成把用户的数据用Shiro的API传递给Shiro,用Shiro来处理用户数据。
(2)Subject:原义是主题,也可以理解为主体。每一个subject代表一个用户,抽象的用户,就是用Shiro对用户的数据进行封装,把数据封装给token令牌,最终可以狭义理解为是数据的封装。
(3)SecurityManager:安全管理中心是Shiro的核心,所有的数据都要经过Shiro的安全管理中心来管理。
(4)Realm:英文原义是域或范围,可以理解成原始数据的源头,源头域,可以狭义的理解为数据库或文件。
2、Shiro的原理:
(1)applicationcode由客户端用户输入用户的数据。
(2)subject代表一个Shiro用户,由Shiro用户封装给令牌token,由Shiro的用户,把封装完的数据(token)传递给安全管理中心。
(3)安全管理中心拿到subject的数据,同时还要从realm源头域中获取数据库中的原始数据,把原始数据返回给安全管理中心,最后由安全管理中心把两个数据做对比,如果匹配上,Shiro就放行,如果没有匹配,就由Shiro控制跳转到指定的页面(比如login.html)。
(4)回调方法,在方法中调用查询数据的方法,查询数据,并把查询完的数据返回给安全管理中心。
3、Shiro项目的搭建步骤:
(1)创建项目
(2)导入jar包
手动导入或者maven导入
(3)把Shiro的对象交给spring容器来管理
a. web.xml:添加一个Shiro的过滤器,要用这个过滤器过滤所有的url。
注意: Shiro的过滤器的filter-name跟spring配置文件中的bean id名称一致 。
<filter>
<filter-name>ShiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<!-- 将Shiro的声明周期交给web容器管理 -->
<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>
b. spring_Shiro.xml:配置Shiro的安全管理中心的对象,还有Shiro的过滤器对象,还有Shiro的登录和权限认证的接口的实现对象。
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--将Shiro的组件以bean的形式交给Spring管理 -->
<bean id="lifeCycleBeanProcessor"
class="org.apache.Shiro.spring.LifecycleBeanPostProcessor"></bean>
<!--Spring为Shiro的bean创建代理对象
代理的方式:
1.jdk
2.cglib
-->
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifeCycleBeanProcessor">
<!--表示强制使用cglib为其创建代理对象 -->
<property name="proxyTargetClass" value="true"></property>
</bean>
<!--切面中需要的对象,也使用cglib来创建代理对象 -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<!--Shiro的安全中心 其中需要提供真实的用户信息. 需要加载realm -->
<bean id="securityManager" class="org.apache.Shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="AuthRealm"></property>
</bean>
<!--自定义Realm 为安全中心提供信息 -->
<bean id="AuthRealm" class="com.tarena.Shiro.AuthRealm">
<property name="credentialsMatcher" ref="authCredential"></property>
</bean>
<!--自定义加密算法 -->
<bean id="authCredential" class="com.tarena.Shiro.AuthCredential"/>
<!--权限认证的适配器 -->
<bean class="org.apache.Shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"></property>
</bean>
<!-- Shiro的过滤器 -->
<bean id="ShiroFilter" class="org.apache.Shiro.spring.web.ShiroFilterFactoryBean">
<!--配置安全中心 -->
<property name="securityManager" ref="securityManager"></property>
<!--指定登录的地址 当用户没有登陆时.默认跳转该页面-->
<property name="loginUrl" value="/login.html"></property>
<!--过滤器链 -->
<property name="filterChainDefinitions">
<value>
<!--添加过滤信息
1.anon 表示放行
2.authc 表示拦截-->
/user/login = anon
/css/** = anon
/font-awesome/** = anon
/fonts/** = anon
/head/** = anon
/images/** = anon
/js/** = anon
/page/** = anon
<!--/** 拦截所有的请求和静态资源文件 -->
/index.html* = authc
/index.jsp* = authc
/** = authc
</value>
</property>
</bean>
</beans>
(4) 创建java类,给相应的接口实现类。
创建java类AuthRealm extends AuthorizingRealm
AuthorizingRealm实现自:
org.apache.shiro.authz.Authorizer,org.apache.shiro.util.Initializable,org.apache.shiro.authz.permission.PermissionResolverAware,org.apache.shiro.authz.permission.RolePermissionResolverAware
5、运行项目