Shiro与基于spring项目的整合 (shiro 第六篇)

SHIRO

Shiro 的Javabeans兼容性使其通过spring xml或者基于spring的配置机制变得非常适合。Shiro应用需要一个应用单例SecurityManager实例。注意,SecurityManager不必是静态单例,但是应用中使用的必须是单 例,管它是不是静态单例。

独立的应用

下面是在spring应用中配置SecurityManager最简单的方式

<bean id="myRealm" class="...">

 ... </bean>

 <bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager"> 

<!-- Single realm app. If you have multiple realms, use the 'realms' property instead. -->

 <property name="realm" ref="myRealm"/> </bean>

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

 <!-- For simplest integration, so that all SecurityUtils.* methods work in all cases, --> 

<!-- make the securityManager bean a static singleton. DO NOT do this in web --> 

<!-- applications - see the 'Web Applications' section below instead. --> 

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">

 <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/> 

<property name="arguments" ref="securityManager"/> 

</bean>

 

Shiro对spring web应用的支持是一流的,在网络应用中,所有与shiro接触的请求都必须要通过shiro主过滤器。这个过滤器本身就很厉害,可以基于任何url路径表达式允许执行自定义的过滤器链。

shiro 1.0之前,你必须使用混合配置方式,在web.xml中配置shiro过滤器和和其他所有配置,securityManger是在spring.xml中配置。这是有点让人沮丧的 因为不能做两件事情 1)你的配置不能在一个地方配置 2)不能使用spring更先进的配置,像PropertyPlaceholderConfigurer或者其他抽象类来配置。

从现在起shiro1.0或者更高的版本中,shiro的配置在spring xml中完成并提供对更健壮的spring配置机制的访问。

下面是shiro整合基于spring的web项目

Web.xml

除了你的spring web.xml 元素(ContextLoaderListener,Log4jConfigListener),定义下面的过滤器和过滤器Mapping:

<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定义在其他filter-mapping之前,以保证子过滤器在过滤器链中正常工作-->

<filter-mapping>

    <filter-name>shiroFilter</filter-name>

    <url-pattern>/*</url-pattern>

</filter-mapping>

Application.xml

applicationContext.xml文件中定义SecurityManager并且‘shirofilter’可以从web.xml中引用

Session共享  思路

先禁用session 然后想办法每次都进行认证

猜你喜欢

转载自blog.csdn.net/weixin_38570967/article/details/80261684
今日推荐