Apache Shiro 集成-spring

本文详细介绍了如何在Spring环境中配置Shiro SecurityManager,包括Standalone应用和Web应用的设置,以及如何启用Shiro注解进行安全检查。还涉及了SecureSpringRemoting的客户端和服务器配置,确保远程调用的安全性。

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值