zoukankan      html  css  js  c++  java
  • shiro 集成spring 配置 学习记录(一)

    首先当然是项目中需要增加shiro的架包依赖:

     1 <!-- shiro -->
     2             <dependency>
     3                 <groupId>org.apache.shiro</groupId>
     4                 <artifactId>shiro-core</artifactId>
     5                 <version>${shiro.version}</version>
     6                 <exclusions>
     7                     <exclusion>
     8                         <artifactId>slf4j-api</artifactId>
     9                         <groupId>org.slf4j</groupId>
    10                     </exclusion>
    11                 </exclusions>
    12             </dependency>
    13             <dependency>
    14                 <groupId>org.apache.shiro</groupId>
    15                 <artifactId>shiro-spring</artifactId>
    16                 <version>${shiro.version}</version>
    17             </dependency>
    18             <dependency>
    19                 <groupId>org.apache.shiro</groupId>
    20                 <artifactId>shiro-ehcache</artifactId>
    21                 <version>${shiro.version}</version>
    22                 <exclusions>
    23                     <exclusion>
    24                         <artifactId>slf4j-api</artifactId>
    25                         <groupId>org.slf4j</groupId>
    26                     </exclusion>
    27                 </exclusions>
    28             </dependency>

    1、shiro 总体来说就是过滤器的集合,首先在项目的web.xml里增加shiro的filter配置:如下

     1 <!--Shiro过滤器-->
     2     <filter>
     3         <filter-name>shiroFilter</filter-name>
     4         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
     5         <init-param>
     6             <param-name>targetFilterLifecycle</param-name>
     7             <param-value>true</param-value>
     8         </init-param>
     9     </filter>
    10     <filter-mapping>
    11         <filter-name>shiroFilter</filter-name>
    12         <url-pattern>/*</url-pattern>
    13         <dispatcher>REQUEST</dispatcher>
    14         <dispatcher>FORWARD</dispatcher>
    15     </filter-mapping>

    2、在项目的resource目录下 新增shiro的配置xml文件如:applicationContext-shiro.xml:

    1)、增加 securityManager  的初始化;

    2)、增加  shiroFilter  的配置, 注意 此配置文件中的bean  过滤器的 id  必须是 web.xml中的 filter-name值

    3)、增加自定义的realm实现

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     5 
     6     <description>Shiro安全配置</description>
     7 
     8     <!--此Bean 只是增加登录时的验证码处理,不是shiro必须的配置-->
     9     <bean class="com.itzixi.web.utils.ItzixiCaptcha">
    10         <property name="cacheManager" ref="shiroEhcacheManager"/>
    11         <!-- 复用半小时缓存 -->
    12         <property name="cacheName" value="itzixiCaptcha"/>
    13     </bean>
    14 
    15     <!-- 安全管理器 -->
    16     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    17         <property name="realm" ref="shiroDbRealm"></property>
    18     </bean>
    19 
    20     <!-- 用户授权信息Cache, 采用EhCache -->
    21     <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
    22         <property name="cacheManagerConfigFile" value="classpath:shiro/ehcache-shiro.xml"/>
    23     </bean>
    24 
    25     <!-- 项目自定义的Realm -->
    26     <bean id="shiroDbRealm" class="com.itzixi.web.shiro.ShiroDBRealm">
    27     </bean>
    28 
    29     <!-- Shiro Filter -->
    30     <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    31         <!-- 安全管理器 -->
    32         <property name="securityManager" ref="securityManager"/>
    33         <!-- 默认的登陆访问url -->
    34         <property name="loginUrl" value="/login.action"/>
    35         <!-- 登陆成功后跳转的url -->
    36         <property name="successUrl" value="/index.action"/>
    37         <!-- 登录成功以后,没有权限访问的页面,会跳转到该url -->
    38         <property name="unauthorizedUrl" value="/unauth.action"/>
    39 
    40         <property name="filterChainDefinitions">
    41             <value>
    42                 <!-- 
    43                     anon  不需要认证
    44                     authc 需要认证
    45                     user  验证通过或RememberMe登录的都可以
    46                 -->
    47                 /** = authc
    48             </value>
    49         </property>
    50     </bean>
    51 </beans>

    经过如上的配置 基本完成了shiro初始化集成。

  • 相关阅读:
    【HDU 1007】Quoit Design
    【BZOJ 4516】【SDOI 2016】生成魔咒
    【SPOJ 1812】Longest Common Substring II
    NOI2014 全国互测Round2
    1231: [Usaco2008 Nov]mixup2 混乱的奶牛
    3529: [Sdoi2014]数表
    2693: jzptab
    2565: 最长双回文串
    1562: [NOI2009]变换序列
    1965: [Ahoi2005]SHUFFLE 洗牌
  • 原文地址:https://www.cnblogs.com/yinfengjiujian/p/9073596.html
Copyright © 2011-2022 走看看