您的位置:首页 > 运维架构 > Apache

[翻译-Shiro]-集成Apache Shiro到基于Spring的应用

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

独立的应用程序

下面是在Spring的应用程序中以最简单的方式配置单例SecurityManager:
<!-- 定义连接后台安全数据源的realm -->
<bean id="myRealm" class="...">
...
</bean>
<bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager">
<!-- 单realm应用。如果有多个realm,使用‘realms’属性代替 -->
<property name="realm" ref="myRealm"/>
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- 最简单的集成,是将securityManager bean配置成一个静态单例,也就是让            SecurityUtils.*
下的所有方法在任何情况下都起作用。在web应用中不要将securityManager bean配置为静态单例,
具体方式请参阅下文中的‘Web Application’部分 -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" 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" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" 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" class="..."/>
<bean id="anotherFilter" class="..."> ... </bean>
...
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 单realm应用。如果需要配置多个realm,使用“realms”属性 -->
<property name="realm" ref="myRealm"/>
<!-- 默认使用servlet容器session。下面是使用shiro 原生session的例子(细节请参考帮助文档)-->
<!-- <property name="sessionMode" value="native"/> -->
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- 定义连接后台安全数据源的realm -->
<bean id="myRealm" class="...">
...
</bean>


开启Shiro的注解

不论独立的应用程序还是web应用程序,都可以使用Shiro提供的注解进行安全检查。比如@RequiresRoles, @RequiresPermissions等。这些注解需要借助Spring的AOP扫描使用Shiro注解的类,并在必要时进行安全逻辑验证。

下面让我们看下如何开启注解,其实很简单,只要在applicationContext.xml中定义两个bean即可。
<!-- 开启Shiro注解的Spring配置方式的beans。在lifecycleBeanPostProcessor之后运行 -->
<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>


Spring远程安全

Shiro的Spring远程支持有两部分组成:远程调用的客户端配置和接收、处理远程调用的服务器端配置。

Server端配置

当Shiro的Server端接收到一个远程方法调用时,与远程调用相关的Subject必须在接收线程执行时绑定到接收线程上,这项工作通过在applicationContext.xml中定义SecureRemoteInvocationExecutor bean完成。
<!-- Spring远程安全确保每个远程方法调用都与一个负责安全验证的Subject绑定 -->
<bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
<property name="securityManager" ref="securityManager"/>
</bean>


SecureRemoteInvocationExecutor定义完成后,需要将它加入到Exporter中,这个Exporter用于暴露向外提供的服务,而且Exporter的实现类由具体使用的远程处理机制和协议决定。定义Exporter beans请参照Spring的Remoting章节

以基于HTTP的远程SecureRemoteInvocationExecutor为例。(remoteInvocationExecutor属性引用自secureRemoteInvocationExecutor)
<bean name="/someService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" 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" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationFactory"/>


然后将SecureRemoteInvocationFactory 添加到与协议相关的Spring远程ProxyFactoryBean 中。

以基于HTTP协议的远程ProxyFactoryBean为例。(remoteInvocationExecutor属性引用自secureRemoteInvocationExecutor)
<bean id="someService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="http://host:port/remoting/someService"/>
<property name="serviceInterface" value="com.pkg.service.SomeService"/>
<property name="remoteInvocationFactory" ref="secureRemoteInvocationFactory"/>
</bean>


原文地址:http://shiro.apache.org/spring.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: