Shiro 集成Web时 web.xml详解

Shiro集成Spring时web.xml中配置详解

1、web.xml 中的 shiroFilter

  • Shiro提供了与Web 集成的支持,其通过一个ShiroFilter入口来拦截需要安全控制的URL,然后进行相应的控制

  • ShiroFilter类似于如Strut2/SpringMVC这种web 框架的前端控制器,是安全控制的入口点,其负责读取配置(如ini配置文件),然后判断URL 是否需要登录/权限等工作。

	<!-- filter-name匹配Spring 的配置文件: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>

    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
	

DelegatingFilterProxy 作用是自动到 Spring 容器查找名字为 shiroFilter(filter-name中的名字)的bean
并把所有Filter的操作委托给它。然后将ShiroFilter 配置到spring容器即可:

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
	<property name="securityManager" ref="securityManager"/>
	<!—忽略其他,详见与Spring 集成部分-->
</bean>

shiroFilter配置注意:

  • DelegatingFilterProxy 实际上是 Filter 的一个代理对象. 默认情况下, Spring 会到 IOC 容器中查找和
    <filter-name> 对应的 filter bean。也可以通过 ·targetBeanName· 的初始化参数来配置 filter bean 的 id。
  • Sping.xml 配置文件中id 必须和 web.xml 文件中配置的 DelegatingFilterProxy 的<filter-name> 一致.
    若不一致, 则会抛出: NoSuchBeanDefinitionException。 因为 Shiro 会来 IOC 容器中查找和 <filter-name>名字对应的 filter bean.

Shiro集成 Webweb.xml 中配置详解

1、web.xml 中的 shiroFilter

  • Shiro 1.2及以后版本的配置方式

从 Shiro 1.2 开始引入了Environment/WebEnvironment的概念,即由它们的实现提供相应的SecurityManager及其相应的依赖。ShiroFilter会自动找到Environment然后获取相应的依赖。

	 <!--
    	从Shiro 1.2开始引入了Environment/WebEnvironment的概念,即由它们的实现提供相应的SecurityManager及其相应的依赖。
    	ShiroFilter会自动找到Environment然后获取相应的依赖。
    	底层:返回反射创建shiroEnvironmentClass对象,调用其init方法.
    		shiroEnvironmentClass中的init方法创建SecurityManager实例并绑定到当前运行环境
     -->
    <listener>
        <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
    </listener>


    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <!-- 拦截所有的请求 -->
        <url-pattern>/*</url-pattern>
    </filter-mapping>

通 过EnvironmentLoaderListener 来创建相应的WebEnvironment , 并自动绑定到
ServletContext,默认使用IniWebEnvironment实现。

可以通过如下配置修改默认实现及其加载的配置文件位置:

<!--初始化securityManager对象所需要的环境配置-->
    <context-param>
        <param-name>shiroEnvironmentClass</param-name>
        <param-value>org.apache.shiro.web.env.IniWebEnvironment</param-value>
    </context-param>
    <context-param>
        <param-name>shiroConfigLocations</param-name>
        <param-value>classpath:shiro.ini</param-value>
    </context-param>

shiroConfigLocations 默认是“/WEB-INF/shiro.ini”,IniWebEnvironment 默认是先从
/WEB-INF/shiro.ini加载,如果没有就默认加载classpath:shiro.ini。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值