Spring集成Shiro

集成Spring

  • 加入 Spring 和 Shiro 的 jar 包
  • 配置 Spring 及 SpringMVC
  • 参照:1.3.2\shiro-root-1.3.2-sourcerelease\shiro-root-1.3.2\samples\spring 配置 web.xml 文件和 Spring 的配置文件

  1. 配置Spring与SpringMVC

    web.xml

<!-- spring配置-->
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- SpringMVC 配置-->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

spring-servlet.xml

      <context:component-scan base-package="com.atguigu.shiro"></context:component-scan>

  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/"></property>
      <property name="suffix" value=".jsp"></property>
  </bean>


  <mvc:annotation-driven></mvc:annotation-driven>
  <!-- 配置默认的servlet -->
  <mvc:default-servlet-handler/> 

2.web.xml中加入filter

<!-- 
       1.配置Shiro的shiroFilter
       2.DelegatingFilterProxy 实际上是FilterShiroFilter 的一个代理对象,默认情况下,Spring会到IOC容器中查找和filter-name 对应的filter bean ,也可以通过targeBeanname 的初始化参数来配置filter bean 的id


-->
  <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> 
</web-app>
  1. 在spring的配置文件中加入如下代码
    1. 配置SecurityManager
    2. 配置缓存管理器,一般使用专门的缓存工具,需要加入ehcache的jar 和配置文件(hibernate中有)
    3. 配置Realm,直接配置实现了org.apache.shiro.realm.Realm 接口的bean
    4. 配置生命周期的bean的post管理器((lifecycleBeanPostProcessor))可以自动的来调用配置在Spring IOC 容器中Shrio bean 的生命中期方法
    5. 启用 IOC 容器中使用Shiro的注解 ,但必须在配置了lifecycleBeanPostProcessor 之后才可以使用
    6. 配置ShiroFilter,id必须和web.xml 文件中配置的DelegatingFilterProxy 的 一致

      <!--1.配置SecurityManager   -->   
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="cacheManager" ref="cacheManager"/>
       <!--Session的管理方式  -->
        <property name="sessionMode" value="native"/>
        <property name="realm" ref="jdbcRealm"/>
    </bean>

   <!-- 2.配置缓存管理 -->
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <!-- Set a net.sf.ehcache.CacheManager instance here if you already have one.  If not, a new one
             will be creaed with a default config:
             <property name="cacheManager" ref="ehCacheManager"/> -->
        <!-- If you don't have a pre-built net.sf.ehcache.CacheManager instance to inject, but you want
             a specific Ehcache configuration to be used, specify that here.  If you don't, a default
             will be used.:-->
        <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/> 
    </bean>
   <!-- 3. 配置Realm,直接配置实现了org.apache.shiro.realm.Realm 接口的bean -->
    <bean id="jdbcRealm" class="com.atguigu.shiro.realm.ShrioRealm"> </bean>

     <!-- 4.配置生命周期bean的post管理器(lifecycleBeanPostProcessor)可以自动的来调用配置在Spring AOC 容器中Shrio bean 的生命中期方法
        -->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

    <!-- 5 .启用  IOC 容器中使用Shiro的注解 ,但必须在配置了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>
    <!--6. 配置ShiroFilter,id必须和web.xml 文件中配置的DelegatingFilterProxy 的<filter-name> 一致,不一致将抛出NoSuchBeanFefinitionException

 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/login.jsp"/>
        <property name="successUrl" value="/list.jsp"/>
        <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
         <!--
           配置哪些页面需要保护,以及访问这些页面需要的权限
           1.anon 可以被匿名访问
           2.authc 必须认证(登陆)后才可以访问,否则自动重定向到登陆界面
          -->
        <property name="filterChainDefinitions">
            <value>
                /login.jsp = anon    
                # everything else requires authentication:
                /** = authc
            </value>
        </property>
    </bean> 








其中页面都为简单JSP页面,仅作测试用例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值