Shiro的JavaBean兼容性使得它非常适合通过SpringXML或其他基于Spring的配置机制。Shiro应用程序需要一个具有单例SecurityManager实例的应用程序。请注意,这不会是一个静态的单例,但应该只有一个应用程序能够使用的实例,无论它是否是静态单例的。
Standalone应用程序
这里是在Spring应用程序中启用应用程序单例SecurityManager的最简单的方法:
<beanid=“myRealm"class=”…">
…
<beanid="securityManager"class=“org.apache.shiro.mgt.DefaultSecurityManager”>
<propertyname="realm"ref=“myRealm”/>
<beanid="lifecycleBeanPostProcessor"class=“org.apache.shiro.spring.LifecycleBeanPostProcessor”/>
<beanclass=“org.springframework.beans.factory.config.MethodInvokingFactoryBean”>
<propertyname="staticMethod"value=“org.apache.shiro.SecurityUtils.setSecurityManager”/>
<propertyname="arguments"ref=“securityManager”/>
Web应用程序
Shiro拥有对SpringWeb应用程序的一流支持。在Web应用程序中,所有Shiro可访问的请求必须通过一个主要的Shiro过滤器。该过滤器本身是极为强大的,允许临时的自定义过滤器链基于任何URL路径表达式执行。
以下是如何在基于Springweb应用程序中配置Shiro。除了其他Springweb.xml中的元素(ContextLoaderListener,Log4jConfigListener等等),定义下面的过滤器及过滤器映射:
web.xml
shiroFilter
org.springframework.web.filter.DelegatingFilterProxy
targetFilterLifecycle
true
…
shiroFilter
/*
在你的applicationContext.xml文件中,定义web支持的SecurityManager和’shiroFilter’bean将会被web.xml引用。
applicationContext.xml
<beanid="shiroFilter"class=“org.apache.shiro.spring.web.ShiroFilterFactoryBean”>
<propertyname="securityManager"ref=“securityManager”/>
<propertyname=“filterChainDefinitions”>
#someexamplechaindefinitions:
/admin/**=authc,roles[admin]
/docs/**=authc,perms[document:read]
/**=authc
#moreURL-to-FilterChaindefinitionshere
<beanid=“someFilter"class=”…"/>
<beanid=“anotherFilter"class=”…">…
…
<beanid="securityManager"class=“org.apache.shiro.web.mgt.DefaultWebSecurityManager”>
<propertyname="realm"ref=“myRealm”/>
<beanid="lifecycleBeanPostProcessor"class=“org.apache.shiro.spring.LifecycleBeanPostProcessor”/>
<beanid=“myRealm"class=”…">
…
EnablingShiroAnnotations
在Standalone应用程序和Web应用程序中,你可能想为安全检查使用Shiro的注释(例如,@RequiresRoles,@RequiresPermissions等等)。这需要Shiro的SpringAOP集成来扫描合适的注解类以及执行必要的安全逻辑。
以下是如何使用这些注解的。只需添加这两个bean定义到applicationContext.xml中:
<beanclass="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"depends-on=“lifecycleBeanPostProcessor”/>
<beanclass=“org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor”>
<propertyname="securityManager"ref=“securityManager”/>
SecureSpringRemoting
Shiro的Spring远程支持有两部分:配置客户端远程调用和配置服务器接收及处理远程调用。
Server-sideConfiguration
当一个远程调用方法到达启用Shiro的服务器时,与该RPC调用关联的Subject在线程执行时必须绑定到访问的接收线程。这是通过在applicationContext.xml中定义SecureRemotInvocationExecutorbean来完成的:
<beanid="secureRemoteInvocationExecutor"class=“org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor”>
<propertyname="securityManager"ref=“securityManager”/>
当你定义这个bean之后,你必须将其插入到任何你正在用来export/expose你服务的远程Exporter。Exporter实现是根据使用的远程处理机制/协议来定义的。请参阅Sping的Remoting章节关于定义Exporterbean的内容。
例如,如果使用基于HTTP的远程调用(注意secureRemoteInvocationExecutorbean的相关属性):
<beanname="/someService"class=“org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter”>
<propertyname="service"ref=“someService”/>
<propertyname="serviceInterface"value=“com.pkg.service.SomeService”/>
<propertyname="remoteInvocationExecutor"ref=“secureRemoteInvocationExecutor”/>
Client-sideConfiguration
当远程调用被执行后,Subject的识别信息必须附加到远程调用的负载上使服务器知道是谁作出了该调用。若客户端是一个基于Spring的客户端,该关联是通过Shiro的SecureRemoteInvocationFactory来完成的:
<beanid="secureRemoteInvocationFactory"class=“org.apache.shiro.spring.remoting.SecureRemoteInvocationFactory”>
在你定义好这个bean后,你需要将它插入到你正在使用的基于特定协议的SpringremotingProxyFactoryBean中。
例如,如果你正在使用基于HTTP的远程调用(注意上面定义的secureRemoteInvocationFactorybean的相关属性):
<beanid="someService"class=“org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean”>
<propertyname="serviceUrl"value=“http://host:port/remoting/someService”/>
<propertyname="serviceInterface"value=“com.pkg.service.SomeService”/>
<propertyname="remoteInvocationFactory"ref=“secureRemoteInvocationFactory”/>