springmvc集成基本的shiro(一)

web.xml文件的配置

1、我的spring-servlet.xml文件放在了默认路径,所以web.xml文件中就不需要配置路径。另外我在src根目录下新建了applicationContext.xml配置文件,专门用来配置shiro的:

 <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!--注意:这个listener必须和<context-param>一起配置才起作用-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

2、配置shiro

  <!-- 配置shiro -->
    <!-- Shiro Filter is defined in the spring application context: -->
    <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>

导入jar包

需要导入shiro-all-1.3.2.jar和ehcache-core-2.4.3.jar包自行下载。这里要注意下shiro-all-1.3.2.jar包,好像它会和shiro-core那几个jar包起冲突,放在一起会报错。所以就用哪个shiro-all-1.3.2.jar包就可以了。

配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

   
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="cacheManager" ref="cacheManager"/>
        
        <property name="sessionMode" value="native"/>
        <property name="realm" ref="jdbcRealm"/>
    </bean>

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


    <bean id="jdbcRealm" class="com.ranqing.shirohello.ShiroRealm">

    </bean>


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

    <!-- Enable Shiro Annotations for Spring-configured beans.  Only run after
         the lifecycleBeanProcessor has run: -->
         
         
    <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>

    <!-- Secure Spring remoting:  Ensure any Spring Remoting method invocations can be associated
         with a Subject for security checks. -->
    <bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
        <property name="securityManager" ref="securityManager"/>
    </bean>

    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/webpage/sys/login.jsp"/>
        <property name="successUrl" value="/webpage/sys/list.jsp"/>
        <property name="unauthorizedUrl" value="/webpage/sys/unauthorized.jsp"/>
        
        <property name="filterChainDefinitions">
            <value>
                /webpage/sys/login.jsp = anon
                /static/**=anon
                # everything else requires authentication:
                /** = authc
            </value>
        </property>
    </bean>
       
</beans>

filterChainDefinitions中配置能访问的jsp和静态文件。
有以下需要注意的地方:

  • shiroFilter必须和web.xml文件中的shiroFilter名称相同
  • jdbcRealm是指向realm类路径,此类实现了realm接口
  • 配置cacheManager的时候需要ehcache.xml配置文件及jar包,jar包已经导入了,现在附上ehcache.xml内容:
<ehcache>

   <!-- Sets the path to the directory where cache .data files are created.

        If the path is a Java System Property it is replaced by
        its value in the running VM.

        The following properties are translated:
        user.home - User's home directory
        user.dir - User's current working directory
        java.io.tmpdir - Default temp file path -->
   <diskStore path="java.io.tmpdir"/>

   <cache name="authorizationCache"
          eternal="false"
          timeToIdleSeconds="3600"
          timeToLiveSeconds="0"
          overflowToDisk="false"
          statistics="true">
   </cache>

   <cache name="authenticationCache"
          eternal="false"
          timeToIdleSeconds="3600"
          timeToLiveSeconds="0"
          overflowToDisk="false"
          statistics="true">
   </cache>

   <cache name="shiro-activeSessionCache"
          eternal="false"
          timeToIdleSeconds="3600"
          timeToLiveSeconds="0"
          overflowToDisk="false"
          statistics="true">
   </cache>

   <!--Default Cache configuration. These will applied to caches programmatically created through
       the CacheManager.

       The following attributes are required for defaultCache:

       maxInMemory       - Sets the maximum number of objects that will be created in memory
       eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                           is never expired.
       timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                           if the element is not eternal. Idle time is now - last accessed time
       timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                           if the element is not eternal. TTL is now - creation time
       overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                           has reached the maxInMemory limit.

       -->
   <defaultCache
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="120"
           timeToLiveSeconds="120"
           overflowToDisk="true"
   />

   <!--Predefined caches.  Add your cache configuration settings here.
       If you do not have a configuration for your cache a WARNING will be issued when the
       CacheManager starts

       The following attributes are required for defaultCache:

       name              - Sets the name of the cache. This is used to identify the cache. It must be unique.
       maxInMemory       - Sets the maximum number of objects that will be created in memory
       eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                           is never expired.
       timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                           if the element is not eternal. Idle time is now - last accessed time
       timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                           if the element is not eternal. TTL is now - creation time
       overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                           has reached the maxInMemory limit.

       -->

   <!-- Sample cache named sampleCache1
       This cache contains a maximum in memory of 10000 elements, and will expire
       an element if it is idle for more than 5 minutes and lives for more than
       10 minutes.

       If there are more than 10000 elements it will overflow to the
       disk cache, which in this configuration will go to wherever java.io.tmp is
       defined on your system. On a standard Linux system this will be /tmp"
       -->
   <cache name="sampleCache1"
          maxElementsInMemory="10000"
          eternal="false"
          timeToIdleSeconds="300"
          timeToLiveSeconds="600"
          overflowToDisk="true"
   />

   <!-- Sample cache named sampleCache2
       This cache contains 1000 elements. Elements will always be held in memory.
       They are not expired. -->
   <cache name="sampleCache2"
          maxElementsInMemory="1000"
          eternal="true"
          timeToIdleSeconds="0"
          timeToLiveSeconds="0"
          overflowToDisk="false"
   />
   -->

   <!-- Place configuration for your caches following -->

</ehcache>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值