Spring+SpringMVC+shiro+mysql

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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. 依赖库信息配置

 技术分享View Code

  1.3. 编译及tomcat部署配置

 技术分享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的缓存,延迟加载等等一系列属性 -->

以上是关于Spring+SpringMVC+shiro+mysql的主要内容,如果未能解决你的问题,请参考以下文章

shiro整合SSM(spring_springmvc_mybatis)

Shiro-继承Spring

Spring MVC + Shiro + Junit 测试

SpringMVC+Spring Data JPA+Shiro+EasyUI简单权限管理系统

请教Apache Shiro 与spring3登录问题

Shiro权限框架与SpringMVC整合