当前位置: 首页 > 编程日记 > 正文

Spring+SpringMVC+shiro+mysql(一)

Spring+SpringMVC+shiro+mysql(一)

最近要做个后台管理系统,就会设计到权限的管理控制,于是就想到 shiro ,下面是自己对Spring+shiro的一点点理解,记录下来,一起多探讨:

项目结构

1. pom.xml 配置

1.1. 版本属性信息配置

 1 <properties>
 2         <!-- base setting -->
 3         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 4         <project.build.locales>zh_CN</project.build.locales>
 5         <project.build.jdk>1.8</project.build.jdk>
 6 
 7         <!-- plugin setting -->
 8         <mybatis.generator.generatorConfig.xml>${basedir}/src/test/java/generatorConfig.xml</mybatis.generator.generatorConfig.xml>
 9         <mybatis.generator.generatorConfig.properties>file:///${basedir}/src/test/java/generatorConfig.properties</mybatis.generator.generatorConfig.properties>
10 
11         <!-- plugin versions -->
12         <plugin.mybatis.generator>1.3.1</plugin.mybatis.generator>
13         <plugin.maven-compiler>3.1</plugin.maven-compiler>
14         <plugin.maven-surefire>2.18.1</plugin.maven-surefire>
15         <skipTests>true</skipTests>
16 
17         <!-- lib versions -->
18         <junit.version>4.11</junit.version>
19         <spring.version>4.0.2.RELEASE</spring.version>
20         <mybatis.version>3.2.2</mybatis.version>
21         <mybatis.spring.version>1.2.2</mybatis.spring.version>
22         <mysql.connector.version>5.1.6</mysql.connector.version>
23         <slf4j.version>1.6.6</slf4j.version>
24         <log4j.version>1.2.12</log4j.version>
25         <httpclient.version>4.1.2</httpclient.version>
26         <jackson.version>1.9.13</jackson.version>
27         <druid.version>1.0.5</druid.version>
28         <jstl.version>1.2</jstl.version>
29         <google.collections.version>1.0</google.collections.version>
30         <cglib.version>3.1</cglib.version>
31         <shiro.version>1.2.3</shiro.version>
32         <commons.fileupload.version>1.3.1</commons.fileupload.version>
33         <commons.codec.version>1.9</commons.codec.version>
34         <commons.net.version>3.3</commons.net.version>
35         <aspectj.version>1.6.12</aspectj.version>
36         <netty.version>4.0.18.Final</netty.version>
37         <hibernate.validator.version>5.1.1.Final</hibernate.validator.version>
38     </properties>
View Code

1.2. 依赖库信息配置

  1     <dependencies>
  2         <!-- springframe start -->
  3         <dependency>
  4             <groupId>org.springframework</groupId>
  5             <artifactId>spring-core</artifactId>
  6             <version>${spring.version}</version>
  7         </dependency>
  8 
  9         <dependency>
 10             <groupId>org.springframework</groupId>
 11             <artifactId>spring-web</artifactId>
 12             <version>${spring.version}</version>
 13         </dependency>
 14 
 15         <dependency>
 16             <groupId>org.springframework</groupId>
 17             <artifactId>spring-oxm</artifactId>
 18             <version>${spring.version}</version>
 19         </dependency>
 20 
 21         <dependency>
 22             <groupId>org.springframework</groupId>
 23             <artifactId>spring-tx</artifactId>
 24             <version>${spring.version}</version>
 25         </dependency>
 26 
 27         <dependency>
 28             <groupId>org.springframework</groupId>
 29             <artifactId>spring-jdbc</artifactId>
 30             <version>${spring.version}</version>
 31         </dependency>
 32 
 33         <dependency>
 34             <groupId>org.springframework</groupId>
 35             <artifactId>spring-webmvc</artifactId>
 36             <version>${spring.version}</version>
 37         </dependency>
 38 
 39         <dependency>
 40             <groupId>org.springframework</groupId>
 41             <artifactId>spring-aop</artifactId>
 42             <version>${spring.version}</version>
 43         </dependency>
 44 
 45         <dependency>
 46             <groupId>org.springframework</groupId>
 47             <artifactId>spring-context-support</artifactId>
 48             <version>${spring.version}</version>
 49         </dependency>
 50 
 51         <dependency>
 52             <groupId>org.springframework</groupId>
 53             <artifactId>spring-test</artifactId>
 54             <version>${spring.version}</version>
 55         </dependency>
 56         <!-- springframe end -->
 57 
 58         <!-- shiro -->
 59         <dependency>
 60             <groupId>org.apache.shiro</groupId>
 61             <artifactId>shiro-spring</artifactId>
 62             <version>${shiro.version}</version>
 63         </dependency>
 64         <dependency>
 65             <groupId>org.apache.shiro</groupId>
 66             <artifactId>shiro-ehcache</artifactId>
 67             <version>${shiro.version}</version>
 68         </dependency>
 69         <dependency>
 70             <groupId>org.apache.shiro</groupId>
 71             <artifactId>shiro-core</artifactId>
 72             <version>${shiro.version}</version>
 73         </dependency>
 74         <dependency>
 75             <groupId>org.apache.shiro</groupId>
 76             <artifactId>shiro-web</artifactId>
 77             <version>${shiro.version}</version>
 78         </dependency>
 79         <dependency>
 80             <groupId>org.apache.shiro</groupId>
 81             <artifactId>shiro-quartz</artifactId>
 82             <version>${shiro.version}</version>
 83         </dependency>
 84 
 85         <!-- mybatis start-->
 86         <dependency>
 87             <groupId>org.mybatis</groupId>
 88             <artifactId>mybatis</artifactId>
 89             <version>${mybatis.version}</version>
 90         </dependency>
 91 
 92         <dependency>
 93             <groupId>org.mybatis</groupId>
 94             <artifactId>mybatis-spring</artifactId>
 95             <version>${mybatis.spring.version}</version>
 96         </dependency>
 97         <!--mybatis end-->
 98 
 99         <!-- mysql-connector -->
100         <dependency>
101             <groupId>mysql</groupId>
102             <artifactId>mysql-connector-java</artifactId>
103             <version>${mysql.connector.version}</version>
104         </dependency>
105 
106         <!-- DruidDataSource -->
107         <dependency>
108             <groupId>com.alibaba</groupId>
109             <artifactId>druid</artifactId>
110             <version>${druid.version}</version>
111         </dependency>
112 
113         <!-- jackson -->
114         <dependency>
115             <groupId>org.codehaus.jackson</groupId>
116             <artifactId>jackson-mapper-asl</artifactId>
117             <version>${jackson.version}</version>
118         </dependency>
119 
120         <!-- log start -->
121         <dependency>
122             <groupId>log4j</groupId>
123             <artifactId>log4j</artifactId>
124             <version>${log4j.version}</version>
125         </dependency>
126         <dependency>
127             <groupId>org.slf4j</groupId>
128             <artifactId>slf4j-api</artifactId>
129             <version>${slf4j.version}</version>
130         </dependency>
131         <dependency>
132             <groupId>org.slf4j</groupId>
133             <artifactId>slf4j-log4j12</artifactId>
134             <version>${slf4j.version}</version>
135         </dependency>
136         <!-- log end -->
137 
138         <!-- start apache -->
139         <dependency>
140             <groupId>commons-fileupload</groupId>
141             <artifactId>commons-fileupload</artifactId>
142             <version>${commons.fileupload.version}</version>
143         </dependency>
144 
145         <dependency>
146             <groupId>org.apache.httpcomponents</groupId>
147             <artifactId>httpclient</artifactId>
148             <version>${httpclient.version}</version>
149         </dependency>
150 
151         <dependency>
152             <groupId>commons-codec</groupId>
153             <artifactId>commons-codec</artifactId>
154             <version>${commons.codec.version}</version>
155         </dependency>
156 
157         <dependency>
158             <groupId>commons-net</groupId>
159             <artifactId>commons-net</artifactId>
160             <version>${commons.net.version}</version>
161         </dependency>
162 
163         <dependency>
164             <groupId>commons-logging</groupId>
165             <artifactId>commons-logging</artifactId>
166             <version>1.1.3</version>
167         </dependency>
168         <dependency>
169             <groupId>commons-collections</groupId>
170             <artifactId>commons-collections</artifactId>
171             <version>3.2.1</version>
172         </dependency>
173         <!-- end apache -->
174  
175         <!-- google -->
176         <dependency>
177             <groupId>com.google.collections</groupId>
178             <artifactId>google-collections</artifactId>
179             <version>${google.collections.version}</version>
180         </dependency>
181 
182         <!-- cglib -->
183         <dependency>
184             <groupId>cglib</groupId>
185             <artifactId>cglib-nodep</artifactId>
186             <version>${cglib.version}</version>
187         </dependency>
188  
189         <!-- aspectjweaver -->
190         <dependency>
191             <groupId>org.aspectj</groupId>
192             <artifactId>aspectjweaver</artifactId>
193             <version>${aspectj.version}</version>
194         </dependency>
195         <dependency>
196             <groupId>org.aspectj</groupId>
197             <artifactId>aspectjrt</artifactId>
198             <version>${aspectj.version}</version>
199         </dependency>
200 
201         <!-- hibernate-validator -->
202         <dependency>
203             <groupId>org.hibernate</groupId>
204             <artifactId>hibernate-validator</artifactId>
205             <version>${hibernate.validator.version}</version>
206         </dependency>
207  
208         <!-- netty -->
209         <dependency>
210             <groupId>io.netty</groupId>
211             <artifactId>netty-all</artifactId>
212             <version>${netty.version}</version>
213         </dependency>
214  
215         <!-- junit -->
216         <dependency>
217             <groupId>junit</groupId>
218             <artifactId>junit</artifactId>
219             <version>${junit.version}</version>
220             <scope>test</scope>
221         </dependency>
222 
223         <!-- servlet api -->
224         <dependency>
225             <groupId>javax.servlet</groupId>
226             <artifactId>javax.servlet-api</artifactId>
227             <version>3.0.1</version>
228             <scope>provided</scope>
229         </dependency>
230 
231         <!-- jstl -->
232         <dependency>
233             <groupId>javax.servlet</groupId>
234             <artifactId>jstl</artifactId>
235             <version>${jstl.version}</version>
236         </dependency>
237     </dependencies>
View Code

  1.3. 编译及tomcat部署配置

 1     <build>
 2         <finalName>shiro01</finalName>
 3         <plugins>
 4             <!-- web.xml 配置 -->
 5             <plugin>
 6                 <groupId>org.apache.maven.plugins</groupId>
 7                 <artifactId>maven-war-plugin</artifactId>
 8                 <version>2.6</version>
 9                 <configuration>
10                     <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
11                 </configuration>
12             </plugin>
13             <!-- Tomcat 部署至本机 -->
14             <plugin>
15                 <groupId>org.apache.tomcat.maven</groupId>
16                 <artifactId>tomcat7-maven-plugin</artifactId>
17                 <version>2.2</version>
18                 <configuration>
19                   <url>http://localhost:8080/manager</url>
20                   <server>tomcat</server>
21                   <username>hunter</username> 
22                   <password>hunter</password>
23                   <path>/shiro01</path>
24                   <contextReloadable>true</contextReloadable>
25                 </configuration>
26               </plugin>
27               <!-- Mybatis generator代码生成插件 配置 -->
28               <plugin>
29                 <groupId>org.mybatis.generator</groupId>
30                 <artifactId>mybatis-generator-maven-plugin</artifactId>
31                 <version>${plugin.mybatis.generator}</version>
32                 <configuration>
33                     <configurationFile>${mybatis.generator.generatorConfig.xml}</configurationFile>
34                     <overwrite>true</overwrite>
35                     <verbose>true</verbose>
36                 </configuration>
37               </plugin>
38         </plugins>
39     </build>
View Code

  

2. spring-mvc.xml 核心配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:aop="http://www.springframework.org/schema/aop"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:mvc="http://www.springframework.org/schema/mvc"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 8        xmlns:p="http://www.springframework.org/schema/p"
 9        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
10         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
11         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
12         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
13         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
14 
15     <!-- 扫描controller(controller层注入) -->
16     <context:component-scan base-package="com.hunter.shiro.web.controller"/>
17 
18     <!-- 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的 -->
19     <!-- 指定自己定义的validator -->
20     <mvc:annotation-driven validator="validator"/>
21 
22     <!-- 以下 validator ConversionService 在使用 mvc:annotation-driven 会 自动注册 -->
23     <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
24         <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
25         <!-- 如果不加默认到 使用classpath下的 ValidationMessages.properties -->
26         <property name="validationMessageSource" ref="messageSource"/>
27     </bean>
28 
29     <!-- 国际化的消息资源文件(本系统中主要用于显示/错误消息定制) -->
30     <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
31         <property name="basenames">
32             <list>
33                 <!-- 在web环境中一定要定位到classpath 否则默认到当前web应用下找 -->
34                 <value>classpath:messages</value>
35                 <value>classpath:org/hibernate/validator/ValidationMessages</value>
36             </list>
37         </property>
38         <property name="useCodeAsDefaultMessage" value="false"/>
39         <property name="defaultEncoding" value="UTF-8"/>
40         <property name="cacheSeconds" value="60"/>
41     </bean>
42 
43     <mvc:interceptors>
44         <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
45     </mvc:interceptors>
46 
47     <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
48         <property name="defaultLocale" value="zh_CN"/>
49     </bean>
50 
51     <!-- 支持返回json(避免IE在ajax请求时,返回json出现下载 ) -->
52     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
53         <property name="messageConverters">
54             <list>
55                 <ref bean="mappingJacksonHttpMessageConverter"/>
56             </list>
57         </property>
58     </bean>
59     <bean id="mappingJacksonHttpMessageConverter"
60           class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
61         <property name="supportedMediaTypes">
62             <list>
63                 <value>text/plain;charset=UTF-8</value>
64                 <value>application/json;charset=UTF-8</value>
65             </list>
66         </property>
67     </bean>
68     <!-- 支持返回json -->
69 
70     <!-- 对模型视图添加前后缀 -->
71     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
72           p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>
73 
74     <!-- 启用shrio授权注解拦截方式 -->
75     <aop:config proxy-target-class="true"></aop:config>
76     <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
77         <property name="securityManager" ref="securityManager"/>
78     </bean>
79 </beans>
View Code

3. server.properties 配置

##JDBC Global Setting
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/hunter_shiro?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=hunter##DataSource Global Setting#配置初始化大小、最小、最大
ds.initialSize=1
ds.minIdle=1
ds.maxActive=20#配置获取连接等待超时的时间 
ds.maxWait=60000#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
ds.timeBetweenEvictionRunsMillis=60000#配置一个连接在池中最小生存的时间,单位是毫秒
ds.minEvictableIdleTimeMillis=300000
View Code

4. spring-mybatis.xml 配置

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
  3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
  5        xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  6        xmlns:cache="http://www.springframework.org/schema/cache"
  7        xsi:schemaLocation="
  8     http://www.springframework.org/schema/context
  9     http://www.springframework.org/schema/context/spring-context.xsd
 10     http://www.springframework.org/schema/beans
 11     http://www.springframework.org/schema/beans/spring-beans.xsd
 12     http://www.springframework.org/schema/tx
 13     http://www.springframework.org/schema/tx/spring-tx.xsd
 14     http://www.springframework.org/schema/jdbc
 15     http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
 16     http://www.springframework.org/schema/cache
 17     http://www.springframework.org/schema/cache/spring-cache.xsd
 18     http://www.springframework.org/schema/aop
 19     http://www.springframework.org/schema/aop/spring-aop.xsd
 20     http://www.springframework.org/schema/util
 21     http://www.springframework.org/schema/util/spring-util.xsd">
 22 
 23     <!-- 自动扫描shiro.web包 ,将带有注解的类 纳入spring容器管理 -->
 24     <context:component-scan base-package="com.hunter.shiro.web"></context:component-scan>
 25 
 26     <!-- 引入配置文件 -->
 27     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 28         <property name="locations">
 29             <list>
 30                 <value>classpath*:server.properties</value>
 31             </list>
 32         </property>
 33     </bean>
 34 
 35     <!-- dataSource 配置 -->
 36     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
 37         <!-- 基本属性 url、user、password -->
 38         <property name="url" value="${jdbc.url}"/>
 39         <property name="username" value="${jdbc.username}"/>
 40         <property name="password" value="${jdbc.password}"/>
 41 
 42         <!-- 配置初始化大小、最小、最大 -->
 43         <property name="initialSize" value="${ds.initialSize}"/>
 44         <property name="minIdle" value="${ds.minIdle}"/>
 45         <property name="maxActive" value="${ds.maxActive}"/>
 46 
 47         <!-- 配置获取连接等待超时的时间 -->
 48         <property name="maxWait" value="${ds.maxWait}"/>
 49 
 50         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
 51         <property name="timeBetweenEvictionRunsMillis" value="${ds.timeBetweenEvictionRunsMillis}"/>
 52 
 53         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
 54         <property name="minEvictableIdleTimeMillis" value="${ds.minEvictableIdleTimeMillis}"/>
 55 
 56         <property name="validationQuery" value="SELECT 'x'"/>
 57         <property name="testWhileIdle" value="true"/>
 58         <property name="testOnBorrow" value="false"/>
 59         <property name="testOnReturn" value="false"/>
 60 
 61         <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
 62         <property name="poolPreparedStatements" value="false"/>
 63         <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
 64 
 65         <!-- 配置监控统计拦截的filters -->
 66         <property name="filters" value="stat"/>
 67     </bean>
 68 
 69     <!-- myBatis文件 -->
 70     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 71         <property name="dataSource" ref="dataSource" />
 72         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
 73         <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
 74         <property name="mapperLocations" value="classpath:mappers/*.xml" />
 75     </bean>
 76 
 77     <!-- spring与mybatis整合配置,扫描所有mapper -->
 78     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 79         <property name="basePackage" value="com.hunter.shiro.web.mapper" />
 80         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
 81     </bean>
 82 
 83     <!-- 对dataSource 数据源进行事务管理 -->
 84     <bean id="transactionManager"
 85         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 86         <property name="dataSource" ref="dataSource" />
 87     </bean>
 88 
 89     <!-- 拦截器方式配置事物 -->
 90     <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
 91         <tx:attributes>
 92             <!-- 对insert,update,delete 开头的方法进行事务管理,只要有异常就回滚 -->
 93             <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Throwable" />
 94             <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Throwable" />
 95             <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Throwable" />
 96             <!-- get,find,select,count开头的方法,开启只读,提高数据库访问性能 -->
 97             <tx:method name="get*" read-only="true" />
 98             <tx:method name="find*" read-only="true" />
 99             <tx:method name="select*" read-only="true" />
100             <tx:method name="count*" read-only="true" />
101             <!-- 对其他方法 使用默认的事务管理 -->
102             <tx:method name="*"/>
103         </tx:attributes>
104     </tx:advice>
105 
106     <!-- 事务 aop 配置 -->
107     <aop:config>
108         <aop:pointcut id="serviceMethods" expression="execution(* com.hunter.shiro.web.service..*(..))"/>
109         <aop:advisor advice-ref="transactionAdvice" pointcut-ref="serviceMethods"/>
110     </aop:config>
111 
112     <!-- 配置使Spring采用CGLIB代理 -->
113     <aop:aspectj-autoproxy proxy-target-class="true"/>
114 
115     <!-- 启用对事务注解的支持 -->
116     <tx:annotation-driven transaction-manager="transactionManager"/>
117 
118     <!-- Cache配置 -->
119     <cache:annotation-driven cache-manager="cacheManager"/>
120     <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
121           p:configLocation="classpath:ehcache.xml"/>
122     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
123           p:cacheManager-ref="ehCacheManagerFactory"/>
124 </beans>
View Code

5. mybatis-config.xml 配置

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <properties>
 7         <property name="dialectClass" value="com.hunter.shiro.core.feature.orm.dialect.MySql5Dialect"/>
 8     </properties>
 9 
10     <!-- 配置mybatis的缓存,延迟加载等等一系列属性 -->
11     <settings>
12 
13         <!-- 全局映射器启用缓存 -->
14         <setting name="cacheEnabled" value="true"/>
15 
16         <!-- 查询时,关闭关联对象即时加载以提高性能 -->
17         <setting name="lazyLoadingEnabled" value="true"/>
18 
19         <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
20         <setting name="multipleResultSetsEnabled" value="true"/>
21 
22         <!-- 允许使用列标签代替列名 -->
23         <setting name="useColumnLabel" value="true"/>
24 
25         <!-- 不允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->
26         <setting name="useGeneratedKeys" value="false"/>
27 
28         <!-- 给予被嵌套的resultMap以字段-属性的映射支持 FULL,PARTIAL -->
29         <setting name="autoMappingBehavior" value="PARTIAL"/>
30 
31         <!-- 对于批量更新操作缓存SQL以提高性能 BATCH,SIMPLE -->
32         <!-- <setting name="defaultExecutorType" value="BATCH" /> -->
33 
34         <!-- 数据库超过25000秒仍未响应则超时 -->
35         <!-- <setting name="defaultStatementTimeout" value="25000" /> -->
36 
37         <!-- Allows using RowBounds on nested statements -->
38         <setting name="safeRowBoundsEnabled" value="false"/>
39 
40         <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn. -->
41         <setting name="mapUnderscoreToCamelCase" value="true"/>
42 
43         <!-- MyBatis uses local cache to prevent circular references and speed up repeated nested queries. By default (SESSION) all queries executed during a session are cached. If localCacheScope=STATEMENT 
44             local session will be used just for statement execution, no data will be shared between two different calls to the same SqlSession. -->
45         <setting name="localCacheScope" value="SESSION"/>
46 
47         <!-- Specifies the JDBC type for null values when no specific JDBC type was provided for the parameter. Some drivers require specifying the column JDBC type but others work with generic values 
48             like NULL, VARCHAR or OTHER. -->
49         <setting name="jdbcTypeForNull" value="OTHER"/>
50 
51         <!-- Specifies which Object's methods trigger a lazy load -->
52         <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
53 
54         <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
55         <setting name="aggressiveLazyLoading" value="false"/>
56 
57     </settings>
58 
59     <typeAliases>
60         <package name="com.hunter.shiro.web.model"/>
61     </typeAliases>
62 
63     <plugins>
64         <plugin interceptor="com.hunter.shiro.core.feature.orm.mybatis.PaginationResultSetHandlerInterceptor"/>
65         <plugin interceptor="com.hunter.shiro.core.feature.orm.mybatis.PaginationStatementHandlerInterceptor"/>
66     </plugins>
67 
68 </configuration>
View Code

6. spring-shiro.xml 配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="
 5        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 6        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
 7 
 8     <description>apache shiro配置</description>
 9 
10     <bean id="credentialsMatcher"
11         class="com.hunter.shiro.web.shiro.credentials.RetryLimitHashedCredentialsMatcher">
12         <constructor-arg ref="shiroEhcacheManager" />
13         <property name="hashAlgorithmName" value="md5" />
14         <property name="hashIterations" value="2" />
15         <property name="storedCredentialsHexEncoded" value="true" />
16     </bean>
17     
18     <!--自定义Realm -->
19     <bean id="securityRealm" class="com.hunter.shiro.web.shiro.SecurityRealm">
20         <property name="credentialsMatcher" ref="credentialsMatcher" />
21         <property name="cachingEnabled" value="false" />
22         <!-- 使用下面配置的缓存管理器 -->
23         <property name="cacheManager" ref="shiroEhcacheManager" />
24     </bean>
25     
26     <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
27         <property name="securityManager" ref="securityManager"/>
28         <property name="loginUrl" value="/login.htm"/>
29         <property name="successUrl" value="/success.htm"/>
30         <property name="unauthorizedUrl" value="/403.htm"/>
31         <property name="filterChainDefinitions">
32             <value>
33                 <!-- 静态资源允许访问 -->
34                 /app/** = anon
35                 /assets/** = anon
36                 <!-- 登录页允许访问 -->
37                 /user/login.htm = anon
38                 <!-- 其他资源需要认证 -->
39                 /** = authc
40             </value>
41         </property>
42     </bean>
43 
44     <!-- 缓存管理器 使用Ehcache实现 -->
45     <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
46         <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"/>
47     </bean>
48 
49     <!-- 会话DAO -->
50     <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.MemorySessionDAO"/>
51 
52     <!-- 会话管理器 -->
53     <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
54         <property name="sessionDAO" ref="sessionDAO"/>
55     </bean>
56 
57     <!-- 安全管理器 -->
58     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
59         <property name="realm" ref="securityRealm" />
60         <property name="sessionManager" ref="sessionManager"/>
61     </bean>
62 
63     <!-- Shiro生命周期处理器 -->
64     <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
65     <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
66           depends-on="lifecycleBeanPostProcessor">
67     </bean>
68 </beans>
View Code

7. 缓存文件配置

7.1 ehcache.xml

1 <?xml version="1.0" encoding="UTF-8"?>
2 <ehcache updateCheck="false" name="txswx-ehcache">
3     <diskStore path="java.io.tmpdir" />
4     <!-- DefaultCache setting. -->
5     <defaultCache maxEntriesLocalHeap="10000" eternal="true"
6         timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true"
7         maxEntriesLocalDisk="100000" />
8 </ehcache>
View Code

7.2 ehcache-shiro.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <ehcache updateCheck="false" name="shiroCache">
 3     <defaultCache
 4             maxElementsInMemory="10000"
 5             eternal="false"
 6             timeToIdleSeconds="120"
 7             timeToLiveSeconds="120"
 8             overflowToDisk="false"
 9             diskPersistent="false"
10             diskExpiryThreadIntervalSeconds="120"
11             />
12 </ehcache>
View Code

8. log4j.properties 配置

# DEBUG,INFO,WARN,ERROR,FATAL
LOG_LEVEL=INFOlog4j.rootLogger=${LOG_LEVEL},CONSOLE,FILElog4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Encoding=utf-8
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
#log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %C{8}@(%F:%L):%m%n 
log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %C{1}@(%F:%L):%m%nlog4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=${catalina.base}/logs/shiro01.log
log4j.appender.FILE.Encoding=utf-8
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
#log4j.appender.FILE.layout=org.apache.log4j.HTMLLayout
log4j.appender.FILE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} %C{8}@(%F\:%L)\:%m%n
View Code

9. web.xml 配置

  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"
  3          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  4          id="WebApp_ID" version="3.0">
  5 
  6     <!-- Spring -->
  7     <!-- 配置Spring配置文件路径 -->
  8     <context-param>
  9         <param-name>contextConfigLocation</param-name>
 10         <param-value>
 11             classpath*:spring-mybatis.xml
 12             classpath*:spring-shiro.xml
 13         </param-value>
 14     </context-param>
 15     <!-- 配置Spring上下文监听器 -->
 16     <listener>
 17         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 18     </listener>
 19     <!-- Spring -->
 20 
 21     <!-- 配置Spring字符编码过滤器 -->
 22     <filter>
 23         <filter-name>encodingFilter</filter-name>
 24         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 25         <init-param>
 26             <param-name>encoding</param-name>
 27             <param-value>UTF-8</param-value>
 28         </init-param>
 29         <init-param>
 30             <param-name>forceEncoding</param-name>
 31             <param-value>true</param-value>
 32         </init-param>
 33     </filter>
 34     <filter-mapping>
 35         <filter-name>encodingFilter</filter-name>
 36         <url-pattern>/*</url-pattern>
 37     </filter-mapping>
 38 
 39     <!-- shiro 安全过滤器 -->
 40     <filter>
 41         <filter-name>shiroFilter</filter-name>
 42         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
 43         <async-supported>true</async-supported>
 44         <init-param>
 45             <param-name>targetFilterLifecycle</param-name>
 46             <param-value>true</param-value>
 47         </init-param>
 48     </filter>
 49     <filter-mapping>
 50         <filter-name>shiroFilter</filter-name>
 51         <url-pattern>/*</url-pattern>
 52     </filter-mapping>
 53 
 54     <!-- 配置log4j配置文件路径 -->
 55     <context-param>
 56         <param-name>log4jConfigLocation</param-name>
 57         <param-value>classpath:log4j.properties</param-value>
 58     </context-param>
 59     <!-- 60s 检测日志配置 文件变化 -->
 60     <context-param>
 61         <param-name>log4jRefreshInterval</param-name>
 62         <param-value>60000</param-value>
 63     </context-param>
 64 
 65     <!-- 配置Log4j监听器 -->
 66     <listener>
 67         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
 68     </listener>
 69  
 70     <!-- Spring MVC 核心控制器 DispatcherServlet 配置 -->
 71     <servlet>
 72         <servlet-name>dispatcher</servlet-name>
 73         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 74         <init-param>
 75             <param-name>contextConfigLocation</param-name>
 76             <param-value>classpath*:spring-mvc.xml</param-value>
 77         </init-param>
 78         <load-on-startup>1</load-on-startup>
 79     </servlet>
 80     <servlet-mapping>
 81         <servlet-name>dispatcher</servlet-name>
 82         <!-- 拦截所有*.htm 的请求,交给DispatcherServlet处理,性能最好 -->
 83         <url-pattern>*.htm</url-pattern>
 84     </servlet-mapping>
 85 
 86     <!-- 首页 -->
 87     <welcome-file-list>
 88         <welcome-file>index.jsp</welcome-file>
 89     </welcome-file-list>
 90 
 91     <!-- 错误页 -->
 92     <error-page>
 93         <error-code>404</error-code>
 94         <location>/404.jsp</location>
 95     </error-page>
 96     <error-page>
 97         <error-code>500</error-code>
 98         <location>/500.jsp</location>
 99     </error-page>
100 </web-app>
View Code

再来看看webapp 下文件路径信息吧

我们先来启动一次项目,确保配置信息正确

直接点击项目运行maven build 在弹出框输入 tomcat:run apply 后直接运行即可。

如果能正确访问到index.jsp页面,说明之前配置信息没得问题啦

 
posted on 2016-05-22 17:37 Java.小学生 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/javatochen/p/5517246.html

相关文章:

【iOS】Socket/TCP 通信 发送 NSString 字符串格式数据

Socket/TCP 原理这里就不阐述了&#xff0c;网上一搜一大堆&#xff0c;直接上关键代码。 【注】iOS 目前有非常著名的第三方库 CocoaAsyncSocket 可以使用&#xff0c;但是我们项目当时做大数据上报要求直接发送 NSString 格式数据&#xff0c;所以自己写了一个简易版 TCP 连接…

[Win]进程间通信——邮槽Mailslot

进程间通信 进程的地址空间是私有的。出于安全性的目的&#xff0c;如果一个进程不具有特殊的权限&#xff0c;是无法访问另外一个进程的内存空间的&#xff0c;也无法知道内存中保存的数据的意义。但是在一些具体的应用情况下需要多个进行相互配合&#xff0c;有时计算机用户也…

OpenStack环境搭建(五:附加项虚拟机文件备份使用)

实验要求&#xff1a; 完成Virtual box平台安装&#xff0c;会应用相关操作&#xff1b;在virtual box虚拟平台上部署Fuel Master节点&#xff1b;在virtual box虚拟平台上部署计算节点Computer&#xff1b;在virtual box虚拟平台上部署控制节点Controller&#xff1b;在web控…

IOS入门-TargetAction

创建一个UIButton 并用Target - Action来监听它的点击事件 Target -- self控制器 Action -- 具体动作&#xff0c;self控制器中的某个方法 forControlEvents:UIControlEventTouchUpInside -- 表示监听的事件 1 - (void)btnclick:(id)sender2 {3 NSLog("点击%" ,…

【iOS_Development】文件操作

原文链接&#xff1a;http://www.jianshu.com/p/c5820ab6836biOS 文件操作 —— 由anticipate_91分享NSFileManager&#xff1a;是用来管理文件系统的&#xff0c;它可以用来进行常见的文件\文件夹操作获取NSFileManager示例[NSFileManager defaultManager] 增删改查 1. 创建文…

仿人智能控制器的参数简化(已发表于《计算机测量与控制》2013年第4期)

转载于:https://www.cnblogs.com/snake-hand/p/3153313.html

OpenStack环境搭建(六:常见问题及解决方案总结)

实验要求&#xff1a; 完成Virtual box平台安装&#xff0c;会应用相关操作&#xff1b;在virtual box虚拟平台上部署Fuel Master节点&#xff1b;在virtual box虚拟平台上部署计算节点Computer&#xff1b;在virtual box虚拟平台上部署控制节点Controller&#xff1b;在web控…

工作区和暂存区

Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念。 先来看名词解释。 工作区&#xff08;Working Directory&#xff09; 就是你在电脑里能看到的目录&#xff0c;比如我的learngit文件夹就是一个工作区&#xff1a; 版本库&#xff08;Repository&#xff09; 工…

Lucene类介绍

IndexWriter:lucene中最重要的的类之一&#xff0c;它主要是用来将文档加入索引&#xff0c;同时控制索引过程中的一些参数使用。 Analyzer luceneAnalyzer new StandardAnalyzer(); IndexWriter indexWriter new IndexWriter(indexDir, luceneAnalyzer, true );…

iOS 开发之沙盒机制 文件操作 (NSFielManager)

原文链接&#xff1a;http://www.jianshu.com/p/349855b5a8aeiOS APP 可以在自己的沙盒里读写文件&#xff0c;但是&#xff0c;不可以访问其他 APP 的沙盒。每一个 APP 都是一个信息孤岛&#xff0c;相互是不可以进行通信的&#xff0c;唯独可以通过 URL Scheme。沙盒里面的文…

手动部署OpenStack环境(一:Virtual Box 5.1 环境的安装及配置)

任务一、Virtual Box 5.1 环境的安装及配置 1.1、安装环境检查 1.2、创建安装目录 1.3、安装及配置 实验目的及要求 完成Virtual box平台安装&#xff0c;会应用相关操作&#xff1b;在virtual box虚拟平台上部署网络节点Network&#xff1b;在virtual box虚拟平台上部署计算…

iOS动画系列之九:实现点赞的动画及播放起伏指示器

iOS动画系列&#xff0c;共十篇。现在写到第九篇啦。感兴趣的可以通过下面的传输门进到其他几篇文章里面。 第一篇&#xff1a;iOS动画系列之一&#xff1a;通过实战学习CALayer和透视的原理。做一个带时分秒指针的时钟动画(上) 第二篇&#xff1a;iOS动画系列之二&#xff1a;…

MySql5.7环境搭建

1. 安装mysql的linux系统 [rootgrewan ~]# cat /etc/redhat-release CentOS release 6.7 (Final) [rootgrewan ~]# uname -a Linux grewan 2.6.32-573.26.1.el6.x86_64 #1 SMP Wed May 4 00:57:44 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux [rootgrewan ~]# 注意&#xff1a;l…

Toad 修改起始窗口

Toad默认窗口时Editor&#xff0c; 如果想要修改为Schema Browser可以通过以下步骤进行修改&#xff08;以Toad9.6为例&#xff09;&#xff1a; 1&#xff0c;点击菜单栏上的View下拉菜单 2&#xff0c;选择Toad Options...菜单 3&#xff0c;在打开窗口的左边功能列表中找到并…

手动部署OpenStack环境(二:CentOS6.6虚拟机的安装及配置)

任务二、CentOS 6.6虚拟机的安装及配置 2.1、安装环境检查 2.2、安装及配置controller0节点 2.3、安装及配置computer0节点 2.4、安装及配置network0节点 2.5、对各节点进行基础配置及服务安装 任务二&#xff1a;CentOS6.6虚拟机的安装及配置 2.1、安装环境检查 2.1.1在…

练习-----查询

第一步&#xff1a;建表 1 create table student #学生表2 (3 Sno varchar(20) primary key, #学号&#xff0c;主键4 Sname varchar(20) not null, #学生姓名5 Ssex varchar(20) not null, #学生性别6 Sbirthday datetime, #学生出生日期7 Class …

UIActivityViewController使用

苹果从iOS6开始&#xff0c;提供了一个活动列表视图&#xff0c;为分享和操作数据提供了一个统一的服务接口&#xff0c;通过UIActivityViewController来控制它的呈现和关闭&#xff0c;凡是继承UIActivity抽象类的子类对象都可以放在列表中呈现出来。如下图所示&#xff1a; 活…

SQL Server系统表sysobjects介绍与使用

关于SQL Server数据库的一切信息都保存在它的系统表格里。我怀疑你是否花过比较多的时间来检查系统表格&#xff0c;因为你总是忙于用户表格。但是&#xff0c;你可能需要偶尔做一点不同寻常的事&#xff0c;例如数据库所有的触发器。你可以一个一个地检查表格&#xff0c;但是…

手动部署OpenStack环境(三:OpenStack环境预配置)

任务三、OpenStack环境预配置 3.1、本地OpenStack yum源制作 任务三&#xff1a;OpenStack环境预配置 3.1、本地OpenStack yum 源制作 3.1.1、拷贝镜像文件源到本地 3.1.2、查看createrepo是否安装&#xff0c;并使用yum方法安装。 &#xff08;此操作只在controller0主机进…

Java 集合框架(二)—— ArrayList

二、数组列表 —— ArrayList 1、构造方法 ArrayList 是 Java 中的动态数组&#xff0c;底层实现就是对象数组&#xff0c;只不过数组的容量会根据情况来改变。 它有个带 int 类型参数的构造方法&#xff0c;根据传入的参数&#xff0c;扩展初始化的数组容量&#xff0c;这个方…

Linux X Window System运行原理和启动过程

本文主要说明X Window System的基本运行原理&#xff0c;其启动过程&#xff0c;及常见的跨网络运行X Window System。 一) 基本运行原理 X Window System采用C/S结构&#xff0c;但和我们常见的C/S不同。常见的C/S结构中&#xff0c;称提供服务的一方为server&#xff0c;即服…

悬浮球 / 悬浮按钮 / 辅助按钮

原文链接&#xff1a;https://github.com/jinht/FloatingBall类似于 iOS 系统自带的 AssistiveTouch / 京东 / 聚划算 / 建行等的辅助按钮 —— 由anticipate_91分享FloatingBall Function Description 这是一个类似于iOS系统自带的AssistiveTouch/京东《我的》部分的悬浮按钮等…

手动部署OpenStack环境(四:安装控制器必备软件)

任务四、安装控制器必备组件 4.1、安装MySQL服务&#xff08;controller0&#xff09; 4.2、安装Rabbitmq消息队列&#xff08;controller0&#xff09; 4.3、Keystone认证&#xff08;controller0&#xff09; 4.4、glance的安装与配置&#xff08;controller0&#xff09; 4.…

cocoaPods安装、更新第三方库

pod install 换成 pod install --verbose --no-repo-update pod update 换成 pod update --verbose --no-repo-update这是因为&#xff1a;目前&#xff0c;cocoaPods正在被墙中......转载于:https://www.cnblogs.com/hello-Huashan/p/5542456.html

iOS 性能优化总结

原文链接&#xff1a;https://github.com/skyming/iOS-Performance-Optimization关于 iOS 性能优化梳理&#xff1a; 基本工具、业务优化、内存优化、卡顿优化、布局优化、电量优化、 安装包瘦身、启动优化、网络优化等 —— 由_skyming_分享关于iOS 性能优化梳理&#xff1a; …

TCP/IP协议分析

一;前言 学习过TCP/IP协议的人多有一种感觉&#xff0c;这东西太抽象了&#xff0c;没有什么数据实例&#xff0c;看完不久就忘了。本文将介绍一种直观的学习方法&#xff0c;利用协议分析工具学习TCP/IP&#xff0c;在学习的过程中能直观的看到数据的具体传输过程。 为了初学者…

手动部署OpenStack环境(五:新建网络及部署虚拟机)

任务五、新建网络及部署虚拟机 5.1、配置安全组规则 5.2、新建网络 5.3、创建云主机 任务五、新建网络及部署虚拟机 5.1、配置安全组规则 5.1.1、配置安全组&#xff1b; 5.2、新建网络。 5.2.1、创建外部网络&#xff1b; 5.2.2、网络地址为外部网络连接的子网地址&#xff1b…

C++基础day01 程序设计方法的发展历程

类把属性和方法作了封装&#xff01; 总结&#xff1a; 面向过程程序设计&#xff1a;数据结构 算法 主要解决科学计算问题&#xff0c;用户需求简单而固定 特点&#xff1a; 分析解决问题所需要的步骤 利用函数实现各个步骤 依次调用函数解决问题 问题&#xff1a; 软件可重用…

【android】android中activity的生命周期

activity生命周期&#xff1a; 实例代码&#xff1a; 1 public class DemoActivity extends Activity {2 3 //1、activity第一次被创建的时候&#xff0c;执行4 Override5 public void onCreate(Bundle savedInstanceState) {6 super.onCreate(savedIn…

Xcode消除编译器警告

Whenever&#xff0c;Xcode警告对于我们来说都相当重要&#xff0c;提醒我们可能存在的错误。但是有时候&#xff0c;我们知道一切都好&#xff0c;everything is in the palm of my hand&#xff0c;我们想要消除那些警告。自己项目的警告 比如我们定义一个designated initial…