码迷,mamicode.com
首页 > 编程语言 > 详细

shiro整合spring配置

时间:2017-07-23 16:48:30      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:artifact   依赖   应用   val   encoding   rman   app   factory   tco   

    shiro应用到项目中,一般都是通过spring来管理。下面就如何把shiro整理到spring中进行了讲解,及给出了配置的步骤:

一、pom.xml文件配置

      本例子主要是介绍maven管理的web项目进行配置介绍,因此,首先需建立好一个maven管理的web项目(可参考本博客创建maven管理的web项目)。

       pom.xml文件配置,主要是添加相关依赖的jar支持。因为整合到spring中需添加spring支持。具体配置参考配置代码:

       

技术分享
  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3   <modelVersion>4.0.0</modelVersion>
  4   <groupId>com.hik.shiro</groupId>
  5   <artifactId>shiroSpringWeb</artifactId>
  6   <packaging>war</packaging>
  7   <version>0.0.1-SNAPSHOT</version>
  8   <name>shiroSpringWeb Maven Webapp</name>
  9   <url>http://maven.apache.org</url>
 10   <dependencies>
 11     <dependency>
 12       <groupId>junit</groupId>
 13       <artifactId>junit</artifactId>
 14       <version>4.8.1</version>
 15       <scope>test</scope>
 16     </dependency>
 17     
 18     <!--添加servlet支持  -->
 19     <dependency>
 20         <groupId>javax.servlet</groupId>
 21         <artifactId>javax.servlet-api</artifactId>
 22         <version>3.1.0</version>
 23         <scope>provided</scope>
 24     </dependency>
 25     <dependency>
 26         <groupId>javax.servlet.jsp</groupId>
 27         <artifactId>javax.servlet.jsp-api</artifactId>
 28         <version>2.3.1</version>
 29         <scope>provided</scope>
 30     </dependency>
 31     
 32     <!--添加jstl支持  -->
 33     <dependency>
 34         <groupId>javax.servlet</groupId>
 35         <artifactId>jstl</artifactId>
 36         <version>1.2</version>
 37     </dependency>
 38     
 39     <!-- 添加Spring支持 -->
 40     <dependency>
 41           <groupId>org.springframework</groupId>
 42           <artifactId>spring-core</artifactId>
 43           <version>4.1.7.RELEASE</version>
 44       </dependency>
 45       <dependency>
 46           <groupId>org.springframework</groupId>
 47           <artifactId>spring-beans</artifactId>
 48           <version>4.1.7.RELEASE</version>
 49       </dependency>
 50       <dependency>
 51          <groupId>org.springframework</groupId>
 52          <artifactId>spring-tx</artifactId>
 53          <version>4.1.7.RELEASE</version>
 54         </dependency>
 55       <dependency>
 56           <groupId>org.springframework</groupId>
 57           <artifactId>spring-context</artifactId>
 58           <version>4.1.7.RELEASE</version>
 59       </dependency>
 60       <dependency>
 61           <groupId>org.springframework</groupId>
 62           <artifactId>spring-context-support</artifactId>
 63           <version>4.1.7.RELEASE</version>
 64       </dependency>
 65       
 66       <dependency>
 67         <groupId>org.springframework</groupId>
 68         <artifactId>spring-web</artifactId>
 69         <version>4.1.7.RELEASE</version>
 70     </dependency>
 71     
 72     <dependency>
 73         <groupId>org.springframework</groupId>
 74         <artifactId>spring-webmvc</artifactId>
 75         <version>4.1.7.RELEASE</version>
 76     </dependency>
 77     
 78     <dependency>
 79         <groupId>org.springframework</groupId>
 80         <artifactId>spring-aop</artifactId>
 81         <version>4.1.7.RELEASE</version>
 82     </dependency>
 83     
 84     <dependency>
 85         <groupId>org.springframework</groupId>
 86         <artifactId>spring-aspects</artifactId>
 87         <version>4.1.7.RELEASE</version>
 88     </dependency>
 89     
 90     <dependency>
 91         <groupId>org.springframework</groupId>
 92         <artifactId>spring-jdbc</artifactId>
 93         <version>4.1.7.RELEASE</version>
 94     </dependency>
 95   
 96       <dependency>
 97         <groupId>org.mybatis</groupId>
 98         <artifactId>mybatis-spring</artifactId>
 99         <version>1.2.3</version>
100     </dependency>
101     
102     <!-- 添加mybatis支持 -->
103      <dependency>
104         <groupId>org.mybatis</groupId>
105         <artifactId>mybatis</artifactId>
106         <version>3.3.0</version>
107     </dependency>
108     
109     <!-- 添加mysql的jdbc驱动包支持 -->
110     <dependency>
111         <groupId>mysql</groupId>
112         <artifactId>mysql-connector-java</artifactId>
113         <version>5.1.38</version>
114     </dependency>
115     
116     <!-- 添加shiro支持 -->
117     <dependency>
118         <groupId>org.apache.shiro</groupId>
119         <artifactId>shiro-core</artifactId>
120         <version>1.2.4</version>
121     </dependency>
122     
123     <dependency>
124         <groupId>org.slf4j</groupId>
125         <artifactId>slf4j-log4j12</artifactId>
126         <version>1.7.12</version>
127     </dependency>
128     
129     <!-- 添加shrio-web支持 -->
130     <dependency>
131         <groupId>org.apache.shiro</groupId>
132         <artifactId>shiro-web</artifactId>
133         <version>1.2.4</version>
134     </dependency>
135     
136     <!-- 添加shrio-spring支持 -->
137     <dependency>
138         <groupId>org.apache.shiro</groupId>
139         <artifactId>shiro-spring</artifactId>
140         <version>1.2.4</version>
141     </dependency>
142     
143     <!-- 添加日志支持 -->
144       <dependency>
145         <groupId>log4j</groupId>
146         <artifactId>log4j</artifactId>
147         <version>1.2.17</version>
148     </dependency>
149     <dependency>
150         <groupId>commons-logging</groupId>
151         <artifactId>commons-logging</artifactId>
152         <version>1.2</version>
153     </dependency>
154     
155   </dependencies>
156   <build>
157     <finalName>shiroSpringWeb</finalName>
158   </build>
159 </project>
View Code

 

二、web.xml文件配置

       添加shiro过滤器定义,具体配置参考配置代码:

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>ShiroSpringWeb</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.jsp</welcome-file>
 6   </welcome-file-list>
 7   
 8     <!-- shiro过滤器定义 -->
 9     <filter>  
10         <filter-name>shiroFilter</filter-name>  
11         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
12         <init-param>  
13         <!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理 -->  
14         <param-name>targetFilterLifecycle</param-name>  
15         <param-value>true</param-value>  
16         </init-param>  
17     </filter>  
18     <filter-mapping>  
19             <filter-name>shiroFilter</filter-name>  
20             <url-pattern>/*</url-pattern>  
21     </filter-mapping>
22     
23     
24     <!-- Spring配置文件 -->
25     <context-param>
26         <param-name>contextConfigLocation</param-name>
27         <param-value>classpath:applicationContext.xml</param-value>
28     </context-param>
29     <!-- 编码过滤器 -->
30     <filter>
31         <filter-name>encodingFilter</filter-name>
32         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
33         <async-supported>true</async-supported>
34         <init-param>
35             <param-name>encoding</param-name>
36             <param-value>UTF-8</param-value>
37         </init-param>
38     </filter>
39     <filter-mapping>
40         <filter-name>encodingFilter</filter-name>
41         <url-pattern>/*</url-pattern>
42     </filter-mapping>
43     <!-- Spring监听器 -->
44     <listener>
45         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
46     </listener>
47     
48     <!-- 添加对springmvc的支持 -->
49     <servlet>
50         <servlet-name>springMVC</servlet-name>
51         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
52         <init-param>
53             <param-name>contextConfigLocation</param-name>
54             <param-value>classpath:spring-mvc.xml</param-value>
55         </init-param>
56         <load-on-startup>1</load-on-startup>
57         <async-supported>true</async-supported>
58     </servlet>
59     <servlet-mapping>
60         <servlet-name>springMVC</servlet-name>
61         <url-pattern>*.do</url-pattern>
62     </servlet-mapping>
63     
64     
65 </web-app>
View Code

 

三、spring中applicationContext.xml配置

       reaml配置及过滤配置

       

技术分享
  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     xmlns:p="http://www.springframework.org/schema/p"  
  5     xmlns:aop="http://www.springframework.org/schema/aop"   
  6     xmlns:context="http://www.springframework.org/schema/context"  
  7     xmlns:jee="http://www.springframework.org/schema/jee"  
  8     xmlns:tx="http://www.springframework.org/schema/tx"  
  9     xsi:schemaLocation="    
 10         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
 11         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
 12         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
 13         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
 14         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    
 15         
 16     <!-- 自动扫描 -->
 17     <context:component-scan base-package="com.hik.service" />
 18     
 19     <!-- 配置数据源 -->
 20     <bean id="dataSource"
 21         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 22         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
 23         <property name="url" value="jdbc:mysql://localhost:3306/db_shiro"/>
 24         <property name="username" value="root"/>
 25         <property name="password" value="passwd"/>
 26     </bean>
 27 
 28     <!-- 配置mybatis的sqlSessionFactory -->
 29     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 30         <property name="dataSource" ref="dataSource" />
 31         <!-- 自动扫描mappers.xml文件 -->
 32         <property name="mapperLocations" value="classpath:com/hik/mappers/*.xml"></property>
 33         <!-- mybatis配置文件 -->
 34         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
 35     </bean>
 36 
 37     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
 38     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 39         <property name="basePackage" value="com.hik.dao" />
 40         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
 41     </bean>
 42 
 43     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
 44     <bean id="transactionManager"
 45         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 46         <property name="dataSource" ref="dataSource" />
 47     </bean>
 48     
 49     <!-- 自定义Realm -->
 50     <bean id="myRealm" class="com.hik.realm.MyRealm"/>  
 51     
 52     <!-- 安全管理器 -->
 53     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
 54         <property name="realm" ref="myRealm"/>  
 55     </bean>  
 56     
 57     <!-- Shiro过滤器 -->
 58     <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
 59         <!-- Shiro的核心安全接口,这个属性是必须的 -->  
 60         <property name="securityManager" ref="securityManager"/>
 61         <!-- 身份认证失败,则跳转到登录页面的配置 -->  
 62         <property name="loginUrl" value="/index.jsp"/>
 63         <!-- 权限认证失败,则跳转到指定页面 -->  
 64         <property name="unauthorizedUrl" value="/unauthor.jsp"/>  
 65         <!-- Shiro连接约束配置,即过滤链的定义 -->  
 66         <property name="filterChainDefinitions">  
 67             <value>  
 68                  /login=anon
 69                 /admin*=authc
 70                 /student=roles[teacher]
 71                 /teacher=perms["user:create"]
 72             </value>  
 73         </property>
 74     </bean>  
 75     
 76     <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->  
 77     <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>  
 78     
 79     <!-- 开启Shiro注解 -->
 80     <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>  
 81           <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">  
 82         <property name="securityManager" ref="securityManager"/>  
 83     </bean>  
 84   
 85     <!-- 配置事务通知属性 -->  
 86     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
 87         <!-- 定义事务传播属性 -->  
 88         <tx:attributes>  
 89             <tx:method name="insert*" propagation="REQUIRED" />  
 90             <tx:method name="update*" propagation="REQUIRED" />  
 91             <tx:method name="edit*" propagation="REQUIRED" />  
 92             <tx:method name="save*" propagation="REQUIRED" />  
 93             <tx:method name="add*" propagation="REQUIRED" />  
 94             <tx:method name="new*" propagation="REQUIRED" />  
 95             <tx:method name="set*" propagation="REQUIRED" />  
 96             <tx:method name="remove*" propagation="REQUIRED" />  
 97             <tx:method name="delete*" propagation="REQUIRED" />  
 98             <tx:method name="change*" propagation="REQUIRED" />  
 99             <tx:method name="check*" propagation="REQUIRED" />  
100             <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
101             <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
102             <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
103             <tx:method name="*" propagation="REQUIRED" read-only="true" />  
104         </tx:attributes>  
105     </tx:advice>  
106   
107     <!-- 配置事务切面 -->  
108     <aop:config>  
109         <aop:pointcut id="serviceOperation"  
110             expression="execution(* com.hik.service.*.*(..))" />  
111         <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
112     </aop:config>  
113     
114    
115 </beans>
View Code

 

 至此,shiro整合spring配置已完成

     

shiro整合spring配置

标签:artifact   依赖   应用   val   encoding   rman   app   factory   tco   

原文地址:http://www.cnblogs.com/jedjia/p/Shiro-spring.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!