分享

Spring+SpringMVC+shiro+mysql解析

 quasiceo 2018-08-19

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

项目结构

1. pom.xml 配置

1.1. 版本属性信息配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
36         <netty.version>4.0.18.Final</netty.version>
37         <hibernate.validator.version>5.1.1.Final</hibernate.validator.version>
38     </properties>

1.2. 依赖库信息配置

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

 

1.3. 编译及tomcat部署配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
1     <build>
 2         <finalname>shiro01</finalname>
 3         <plugins>
 4             <!-- web.xml 配置 -->
 5             <plugin>
 6                 <groupid>org.apache.maven.plugins</groupid>
 7                 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                 tomcat7-maven-plugin</artifactid>
17                 <version>2.2</version>
18                 <configuration>
19                   <url>https://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                 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>

 

2. spring-mvc.xml 核心配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
1 <!--?xml version="1.0" encoding="UTF-8"?-->
10         https://www./schema/beans https://www./schema/beans/spring-beans.xsd
11         https://www./schema/context https://www./schema/context/spring-context.xsd
12         https://www./schema/mvc https://www./schema/mvc/spring-mvc.xsd
13         https://www./schema/tx https://www./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 class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" id="validator">
24         <property name="providerClass" value="org.hibernate.validator.HibernateValidator">
25         <!-- 如果不加默认到 使用classpath下的 ValidationMessages.properties -->
26         <property name="validationMessageSource" ref="messageSource">
27     </property></property></bean>
28
29     <!-- 国际化的消息资源文件(本系统中主要用于显示/错误消息定制) -->
30     <bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource">
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     </property></property></property></bean>
42
43     <mvc:interceptors>
44         <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
45     </bean></mvc:interceptors>
46
47     <bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver">
48         <property name="defaultLocale" value="zh_CN">
49     </property></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             </ref></list>
57         </property>
58     </bean>
59     <bean 60="" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" id="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 72="" class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp">
73
74     <!-- 启用shrio授权注解拦截方式 -->
75     </aop:config>
76     <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
77         <property name="securityManager" ref="securityManager">
78     </property></bean>
79 </bean></mvc:annotation-driven></context:component-scan></beans>

 

3. server.properties 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
##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

 

4. spring-mybatis.xml 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  1 <!--?xml version="1.0" encoding="UTF-8"?-->
  2 <beans 3="" 4="" 5="" 6="" 7="" xmlns="https://www./schema/beans" xmlns:aop="https://www./schema/aop" xmlns:cache="https://www./schema/cache" xmlns:context="https://www./schema/context" xmlns:jdbc="https://www./schema/jdbc" xmlns:p="https://www./schema/p" xmlns:tx="https://www./schema/tx" xmlns:util="https://www./schema/util" xmlns:xsi="https://www./2001/XMLSchema-instance" xsi:schemalocation="
  8     https://www./schema/context
  9     https://www./schema/context/spring-context.xsd
 10     https://www./schema/beans
 11     https://www./schema/beans/spring-beans.xsd
 12     https://www./schema/tx
 13     https://www./schema/tx/spring-tx.xsd
 14     https://www./schema/jdbc
 15     https://www./schema/jdbc/spring-jdbc.xsd
 16     https://www./schema/cache
 17     https://www./schema/cache/spring-cache.xsd
 18     https://www./schema/aop
 19     https://www./schema/aop/spring-aop.xsd
 20     https://www./schema/util
 21     https://www./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 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="propertyConfigurer">
 28         <property name="locations">
 29             <list>
 30                 <value>classpath*:server.properties</value>
 31             </list>
 32         </property>
 33     </bean>
 34
 35     <!-- dataSource 配置 -->
 36     <bean class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" id="dataSource" init-method="init">
 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     </property></property></property></property></property></property></property></property></property></property></property></property></property></property></property></property></bean>
 68
 69     <!-- myBatis文件 -->
 70     <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
 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     </property></property></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     </property></property></bean>
 82
 83     <!-- 对dataSource 数据源进行事务管理 -->
 84     <bean 85="" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
 86         <property name="dataSource" ref="dataSource">
 87     </property></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:method></tx:method></tx:method></tx:method></tx:method></tx:method></tx:method></tx:method></tx:attributes>
104     </tx:advice>
105
106     <!-- 事务 aop 配置 -->
107    
108        
109        
110     </aop:advisor></aop:pointcut></aop:config>
111
112     <!-- 配置使Spring采用CGLIB代理 -->
113    
114
115     <!-- 启用对事务注解的支持 -->
116     <tx:annotation-driven transaction-manager="transactionManager">
117
118     <!-- Cache配置 -->
119     <cache:annotation-driven cache-manager="cacheManager">
120     <bean 121="" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" id="ehCacheManagerFactory" p:configlocation="classpath:ehcache.xml">
122     <bean 123="" class="org.springframework.cache.ehcache.EhCacheCacheManager" id="cacheManager" p:cachemanager-ref="ehCacheManagerFactory">
124 </bean></bean></cache:annotation-driven></tx:annotation-driven></aop:aspectj-autoproxy></beans>

 

5. mybatis-config.xml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
1 <!--?xml version="1.0" encoding="UTF-8" ?-->
 2
 5 <configuration>
 6     <properties>
 7         <property name="dialectClass" value="com.hunter.shiro.core.feature.orm.dialect.MySql5Dialect">
 8     </property></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         <!-- 不允许使用自定义的主键&#20540;(比如由程序生成的UUID 32位编码作为键&#20540;),数据表的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     </setting></setting></setting></setting></setting></setting></setting></setting></setting></setting></setting></setting></settings>
58
59     <typealiases>
60         <package name="com.hunter.shiro.web.model">
61     </package></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     </plugin></plugin></plugins>
67
68 </configuration>

 

6. spring-shiro.xml配置

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

 

7. 缓存文件配置

7.1ehcache.xml

1
2
3
4
5
6
1 <!--?xml version="1.0" encoding="UTF-8"?-->
2 <ehcache name="txswx-ehcache" updatecheck="false">
3     <diskstore path="java.io.tmpdir">
4     <!-- DefaultCache setting. -->
5     <defaultcache 6="" 7="" eternal="true" maxentrieslocaldisk="100000" maxentrieslocalheap="10000" overflowtodisk="true" timetoidleseconds="300" timetoliveseconds="600">
8 </defaultcache></diskstore></ehcache>

7.2 ehcache-shiro.xml

1
2
3
4
1 <!--?xml version="1.0" encoding="UTF-8"?-->
 2 <ehcache name="shiroCache" updatecheck="false">
 3     <defaultcache 10="" 11="" 4="" 5="" 6="" 7="" 8="" 9="" diskexpirythreadintervalseconds="120" diskpersistent="false" eternal="false" maxelementsinmemory="10000" overflowtodisk="false" timetoidleseconds="120" timetoliveseconds="120">
12 </defaultcache></ehcache>

 

8. log4j.properties配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# DEBUG,INFO,WARN,ERROR,FATAL
LOG_LEVEL=INFO
log4j.rootLogger=${LOG_LEVEL},CONSOLE,FILE
log4j.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%n
log4j.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

 

9. web.xml 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  1 <!--?xml version="1.0" encoding="utf-8"?-->
  2 <web-app 3="" 4="" id="WebApp_ID" version="3.0" xmlns="https://java./xml/ns/javaee" xmlns:xsi="https://www./2001/XMLSchema-instance" xsi:schemalocation="https://java./xml/ns/javaee https://java./xml/ns/javaee/web-app_3_0.xsd">
  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         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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约