Spring 集成Shiro(XML方式)--官网

Spring 集成Shiro(XML方式)

Shiro中的组件都是JavaBean形式实现的,因此可以通过Spring的XML或其他Spring的配置机制来设置这些组件。首先,Shiro应用程序需要一个单例的SecurityManager实例,但这并不表示它是静态的,只需要确保它在应用程序中是唯一的即可

非Web应用

下面展示了非Web应用如何配置一个单例的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"/>

<!-- 最简单的让SecurityUtils.*方法起作用的方式,就是将SecurityManager的bean设置为静态单例 -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
    <property name="arguments" ref="securityManager" />
</bean>

Web 应用

在Web应用中,所有的Web请求都要经过Shiro的过滤器

Shiro1.0之前,需要采用一种混合的方式来配置Spring Web项目,即在web.xml中定义和配置Shiro的过滤器,而在Spring的XML中定义SecurityManager。这样就不能将自己的配置合并到一个地方,也不能利用更高级的Spring特性(如PropertyPlaceholderConfigurer或抽象bean)的配置能力来合并公共配置

现在在Shiro 1.0和更高版本中,所有Shiro配置都是在Spring XML中完成的,提供了对更健壮的Spring配置机制的访问

web.xml

除了之前Web章节的web.xml元素之外,Spring Web项目还需要定义以下过滤器

<!-- 这个shiroFilter和applicationContext.xml中定义的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>
...
<!-- 确保所有的请求都能经过过滤,通常这个filter-mapping定义在最前面,确保请求在到达其他过滤器之前先经过shiroFilter -->
<filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

完整示例参见github

applicationContext.xml

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean" >
    <property name="securityManager" ref="securityManager"></property>
    <!-- 根据需要配置如下属性
    <property name="loginUrl" value="/login.jsp" />
    <property name="successUrl" value="/home.jsp" />
    <property name="unauthorizedUrl" value="/unauthorized.jsp"/> -->
    
    <!-- 
        还记得前面的通过ini将Shiro配置到web项目中时,我们通过过滤器的名字来定义一条过滤器链
        shiroFilter会自动获取所有声明的javax.servlet.Filter的bean定义,将这些bean设置到filters属性中,其默认的key就是它的beanName,这样就可以通过这个key来定义过滤器链了
        当然也可以覆盖整个默认选项
    <property name="filters">
        <util:map>
            <entry key="anAlias" value-ref="someFilter" />
        </util:map>
    </property>
    -->
    
    <property name="filterChainDefinitions">
        <value>
            # 这里是一些过滤器链的例子
            /admin/** = authc, roles[admin]
            /docs/** = authc, perms[document:read]
            /** = authc
            # 其他例子
        </value>
        <!-- 可以看到,这里的value其实就是ini配置中的url内容 -->
    </property>
</bean>

<!-- 可以在应用程序上下文的任意一个位置定义javax.servlet.Filter的bean,它们会自动的设置到shiroFilter的filters属性中 -->
<bean id="someFilter" class="..." />
<bean id="anotherFilter" class="...">...</bean>
...

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <!-- 这里示例是一个单realm的应用,也可以通过realms属性值设置多realm应用 -->
    <property name="realm" ref="myRealm"/>
    
    <!-- sessionMode属性值决定了是否启用Shiro的原生会话管理,默认采用的是Servlet容器的会话管理 -->
    <property name="sessionMode" value="native"/>
</bean>

<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

<!-- 定义一个realm -->
<bean id="myRealm" class="...">
    ...
</bean>

开启Shiro的注解支持

无论是Web应用还是非Web应用,想要开启Shiro的注解支持,都需要Spring的AOP集成来支持

<!-- 为Spring配置的bean启动开启Shiro注解支持 -->
<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远程支持分为两部分:进行远程调用的客户端配置和接收处理远程调用的服务器配置

服务端配置

当一个远程调用到达服务端时,服务端必须绑定相关联的Subject到执行线程中,这个是由Shiro的SecureRemoteInvocationExecutor实例完成的

<!-- 确保任何Spring的远程调用都能绑定一个用于安全检查的Subject -->
<bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
    <property name="securityManager" ref="seucrityManager" />
</bean>

一旦定义了此bean,必须将其设置到用于导出/公开服务的ExporterExporter的实现是根据使用中的远程处理机制/协议定义的。下面展示了一个基于HTTP的远程服务的例子

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

客户端配置

当一个远程调用发生时,客户端必须将Subject的标识信息附加到远程调用中,让服务器知道谁在调用。如果这个客户端是一个基于Spring的客户端,则可以通过Shiro的SecureRemoteInvocationFactory来完成这些操作

<bean id="secureRemoteInvocationFactory" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationFactory" />

最后,将这个bean设置到协议相关的Spring远程ProxyFactoryBean就可以了。下面的配置展示了一个基于HTTP的远程调用

<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>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容