读书人

怎么将Apache Shiro集成到基于Spring的

发布时间: 2012-09-16 17:33:16 作者: rapoo

如何将Apache Shiro集成到基于Spring的应用
本文主要介绍如何将Apache Shiro集成到基于Spring的应用。

Shiro兼容javabean使得它能很好的与Spring XML或其他基于Spring的配置方式集成。在基于Shiro的应用程序中的SecurityManager是单例的。不过,SecurityManager不一定是静态单例,但是不论是否是静态单例,必须保证一个应用程序中只有一个SecurityManager的实例。

独立的应用程序

下面是在Spring的应用程序中以最简单的方式配置单例SecurityManager:

<!-- 定义连接后台安全数据源的realm -->
<bean id="myRealm" ref="myRealm"/>
</bean>
<bean id="lifecycleBeanPostProcessor" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
<property name="arguments" ref="securityManager"/>
</bean>
Web应用程序

Shiro对基于Spring的Web应用提供了完美的支持,web应用中,Shiro可控制的web请求必须经过Shiro主过滤器的拦截。Shiro主过滤器本身功能十分强大,其强大之处就在于它支持任何基于URL路径表达式的、自定义的过滤器的执行。

Shiro1.0版本前,Spring web应用使用混合方式进行配置,在web.xml中定义Shiro的过滤器和它所有的配置,而在Spring XML中定义SecurityManager。这样有些别扭,因为第一你没办法将所有的配置放到一个位置;第二没法最大发挥Spring提供配置上的优点,比如PropertyPlaceholderConfigurer或者通过抽象beans来合并配置。

在Shiro1.0或更高版本中,所有的Shiro配置都放到Spring XML中,这样做进一步发挥了Spring配置机制的优势。

下面是如何在基于Spring的web应用中配置Shiro:

web.xml
除了定义ContextLoaderListener, Log4jConfigListener等Spring元素外,只要在web.xml中增加如下的filter和filter-mapping:

<!-- filter-name对应applicationContext.xml中定义的名字为“shiroFilter”的bean -->
<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>
...
<!-- 使用“/*”匹配所有请求,保证所有的可控请求都经过Shiro的过滤。通常这个filter-mapping
放置到最前面(其他filter-mapping前面),保证它是过滤器链中第一个起作用的 -->
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
applicationContext.xml
在applicationContext.xml中定义SecurityManager和web.xml中使用的名字叫shiroFilter的bean

<bean id="shiroFilter" ref="securityManager"/>
<!-- 可根据项目的URL进行替换 -->
<property name="loginUrl" value="/login.jsp"/>
<property name="successUrl" value="/home.jsp"/>
<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
<!-- 因为每个已经定义的javax.servlet.Filter类型的bean都可以在链的定义中通过bean名
称获取,所以filters属性不是必须出现的。但是可以根据需要通过filters属性替换filter
实例或者为filter起别名 -->
<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>
<!-- 定义应用上下文的 javax.servlet.Filter beans。这些beans 会被上面定义的shiroFilter自
动感知,并提供给“filterChainDefinitions”属性使用。或者也可根据需要手动的将他们添加在
shiroFilter bean的“filters”属性下的Map标签中。 -->
<bean id="someFilter" ref="myRealm"/>
<!-- 默认使用servlet容器session。下面是使用shiro 原生session的例子(细节请参考帮助文档)-->
<!-- <property name="sessionMode" value="native"/> -->
</bean>
<bean id="lifecycleBeanPostProcessor" depends-on="lifecycleBeanPostProcessor"/>
<bean ref="securityManager"/>
</bean>
Spring远程安全
Shiro的Spring远程支持有两部分组成:远程调用的客户端配置和接收、处理远程调用的服务器端配置。

Server端配置

当Shiro的Server端接收到一个远程方法调用时,与远程调用相关的Subject必须在接收线程执行时绑定到接收线程上,这项工作通过在applicationContext.xml中定义SecureRemoteInvocationExecutor bean完成。

<!-- Spring远程安全确保每个远程方法调用都与一个负责安全验证的Subject绑定 -->
<bean id="secureRemoteInvocationExecutor" ref="securityManager"/>
</bean>
SecureRemoteInvocationExecutor定义完成后,需要将它加入到Exporter中,这个Exporter用于暴露向外提供的服务,而且Exporter的实现类由具体使用的远程处理机制和协议决定。定义Exporter beans请参照Spring的Remoting章节。

以基于HTTP的远程SecureRemoteInvocationExecutor为例。(remoteInvocationExecutor属性引用自secureRemoteInvocationExecutor)

<bean name="/someService" ref="someService"/>
<property name="serviceInterface" value="com.pkg.service.SomeService"/>
<property name="remoteInvocationExecutor" ref="secureRemoteInvocationExecutor"/>
</bean>
Client端配置

当远程调用发生时,负责鉴别信息的Subject需要告知server远程方法是谁发起的 。如果客户端是基于Spring的,那么这种关联可以通过Shiro的SecureRemoteInvocationFactory 完成。

<bean id="secureRemoteInvocationFactory" value="http://host:port/remoting/someService"/>
<property name="serviceInterface" value="com.pkg.service.SomeService"/>
<property name="remoteInvocationFactory" ref="secureRemoteInvocationFactory"/>
</bean>

读书人网 >Apache

热点推荐