Shiro官网提供了两种Integrations方式,一个是和Spring整合,一个是和Guice整合,这两个框架的特点都是有IOC,而且Guice本身就是一个轻量级的IOC框架。
我现在做的项目都使用Spring Boot,但Spring Boot的话并不推荐使用XML文件配置,所以需要根据官网提供的XML配置来转换成Java类配置,但由于都是基于Spring的框架,原理是一样的,所以只需要理解Shiro配置部分使用的类的作用就好。以下就是我对官网提供的XML配置方法的理解。
Spring启动Shiro的最简单的方法:
<!-- Define the realm you want to use to connect to your back-end security datasource: -->
<bean id="myRealm" class="...">
...
</bean>
<bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager">
<!-- Single realm app. If you have multiple realms, use the 'realms' property instead. -->
<property name="realm" ref="myRealm"/>
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- For simplest integration, so that all SecurityUtils.* methods work in all cases, -->
<!-- make the securityManager bean a static singleton. DO NOT do this in web -->
<!-- applications - see the 'Web Applications' section below instead. -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
<property name="arguments" ref="securityManager"/>
</bean>
从上面看,最快启动Shiro的方法,只需要自定义一个Realm,即,Shiro框架中与数据库的安全信息数据进行交互的模块。将自定义的Realm交由Spring管理,然后注入到默认安全管理器中,默认安全管理器也交由Spring管理。
org.apache.shiro.spring.LifecycleBeanPostProcessor这个类它自动调用Shiro对象上的init()和/或destroy()方法,实现org.apache.shiro.util.Initializable或org.apache.shiro.util.Destroyable接口。
为了实现最简单的集成,因此所有SecurityUtils.*方法在所有情况下都工作,因此需要将securityManager bean设置为静态单例。换句话说,org.springframework.beans.factory.config.MethodInvokingFactoryBean是一个Spring的方法调用的bean工厂,它的值是静态或实例方法调用的结果,而Shiro需要在SecurityUtils中调用setSecurityManager()将安全管理器设置进去,参数依赖于前面声明的默认安全管理器。
web.xml配置:
<!-- The filter-name matches name of a 'shiroFilter' bean inside applicationContext.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>
...
<!-- Make sure any request you want accessible to Shiro is filtered. /* catches all -->
<!-- requests. Usually this filter mapping is defined first (before all others) to -->
<!-- ensure that Shiro works in subsequent filters in the filter chain: -->
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这里就是普通的Java Web项目中的web.xml文件配置,Spring过滤配置依赖于bean名为shiroFilter的bean,过滤的url设置为全局,并且也依赖于shiroFilter。
applicationContext.xml配置
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!-- override these for application-specific URLs if you like:
<property name="loginUrl" value="/login.jsp"/>
<property name="successUrl" value="/home.jsp"/>
<property name="unauthorizedUrl" value="/unauthorized.jsp"/> -->
<!-- The 'filters' property is not necessary since any declared javax.servlet.Filter bean -->
<!-- defined will be automatically acquired and available via its beanName in chain -->
<!-- definitions, but you can perform instance overrides or name aliases here if you like: -->
<!-- <property name="filters">
<util:map>
<entry key="anAlias" value-ref="someFilter"/>
</util:map>
</property> -->
<property name="filterChainDefinitions">
<value>
# some example chain definitions:
/admin/** = authc, roles[admin]
/docs/** = authc, perms[document:read]
/** = authc
# more URL-to-FilterChain definitions here
</value>
</property>
</bean>
<!-- Define any javax.servlet.Filter beans you want anywhere in this application context. -->
<!-- They will automatically be acquired by the 'shiroFilter' bean above and made available -->
<!-- to the 'filterChainDefinitions' property. Or you can manually/explicitly add them -->
<!-- to the shiroFilter's 'filters' Map if desired. See its JavaDoc for more details. -->
<bean id="someFilter" class="..."/>
<bean id="anotherFilter" class="..."> ... </bean>
...
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- Single realm app. If you have multiple realms, use the 'realms' property instead. -->
<property name="realm" ref="myRealm"/>
<!-- By default the servlet container sessions will be used. Uncomment this line
to use shiro's native sessions (see the JavaDoc for more): -->
<!-- <property name="sessionMode" value="native"/> -->
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- Define the Shiro Realm implementation you want to use to connect to your back-end -->
<!-- security datasource: -->
<bean id="myRealm" class="...">
...
</bean>
这里声明的shiroFilter就是web.xml里依赖的那个,它是Shiro的一个过滤器类的bean工厂,用来配置各种url过滤及权限设置。类中定义有的变量是securityManager、filters、filterChainDefinitionMap、loginUrl、successUrl、unauthorizedUrl、instance,相对重要的变量就是securityManager与filterChainDefinitionMap了。securityManager的作用不用多说,来说说filterChainDefinitionMap和loginUrl这类特殊url的作用。
filterChainDefinitionMap的作用,就是用来存放各种url以及url对应的拦截方式,Map的key为要拦截的url,value为Shiro的拦截方式。而从bean工厂类ShiroFilterFactoryBean的源码可知,该类在实例化的时候就同时把filterChainDefinitionMap和filters也实例化了,filters使用的是HashMap(),而filterChainDefinitionMap使用的是LinkedHashMap()。
filterChainDefinitionMap使用LinkedHashMap()的原因,正如后面的那个注释所说,拦截url配置的顺序非常重要,正因如此选择了具有顺序且线程同步的LinkedHashMap()。对于特殊url其实Shiro已经提供了一些完整的过滤器配置,而我们不需要去自定义,提供对应的url,Shiro会自动。loginUrl:登录页面url,该值将通过AccessControlFilter.setLoginUrl(String)方法传递给每个Filter;successUrl:登录成功后跳转的url,该值将通过AuthenticationFilter.setSuccessUrl(String)方法传递给每个Filter。unauthorizedUrl:无权限跳转的url,该值将通过AuthorizationFilter.setUnauthorizedUrl(String)方法传递给每个Filter。
启用Shiro注解:
若要启用Shiro的注解进行安全检查的话,就需要使用Spring AOP来扫描带注解的类,并根据需要执行安全逻辑。而启用注解只需要在applicationContext.xml中添加下面的配置:
<!-- Enable Shiro Annotations for Spring-configured beans. Only run after -->
<!-- the lifecycleBeanProcessor has run: -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
这里创建的DefaultAdvisorAutoProxyCreator依赖于之前创建的lifecycleBeanPostProcessor这个bean,会在它创建完后才开始创建,是一个通用的创建AOP代理,它不包含用于处理任何特定方面(如池方面)的特殊代码。创建AuthorizationAttributeSourceAdvisor设置的安全管理器依旧ref之前创建的安全管理器。
至此,Shiro的配置基本已完成,可以总结一下需要用到的类。首先从最底层开始:
-
(自定义)Realm,相关包路径org.apache.shiro.realm
-
ShiroFilterFactoryBean,类路径org.apache.shiro.spring.web.ShiroFilterFactoryBean
-
Filters,相关包路径org.apache.shiro.web.filter
-
DefaultWebSecurityManager,类路径org.apache.shiro.web.mgt.DefaultWebSecurityManager
-
AuthorizationAttributeSourceAdvisor,类路径org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor
也就是说,不光是Spring,Spring Boot也可以针对将上面这些类交由Spring管理,并完善其中的配置,即可将Shiro整合并能正常使用。