最近要做个后台管理系统,就会设计到权限的管理控制,于是就想到 shiro ,下面是自己对Spring+shiro的一点点理解,记录下来,一起多探讨:
项目结构
1. pom.xml 配置
1.1. 版本属性信息配置
1 <properties> 2 3 <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> 4 <project.build.locales>zh_CNproject.build.locales> 5 <project.build.jdk>1.8project.build.jdk> 6 7 8 <mybatis.generator.generatorConfig.xml>${basedir}/src/test/java/generatorConfig.xmlmybatis.generator.generatorConfig.xml> 9 <mybatis.generator.generatorConfig.properties>file:///${basedir}/src/test/java/generatorConfig.propertiesmybatis.generator.generatorConfig.properties> 10 11 12 <plugin.mybatis.generator>1.3.1plugin.mybatis.generator> 13 <plugin.maven-compiler>3.1plugin.maven-compiler> 14 <plugin.maven-surefire>2.18.1plugin.maven-surefire> 15 <skipTests>trueskipTests> 16 17 18 <junit.version>4.11junit.version> 19 <spring.version>4.0.2.RELEASEspring.version> 20 <mybatis.version>3.2.2mybatis.version> 21 <mybatis.spring.version>1.2.2mybatis.spring.version> 22 <mysql.connector.version>5.1.6mysql.connector.version> 23 <slf4j.version>1.6.6slf4j.version> 24 <log4j.version>1.2.12log4j.version> 25 <httpclient.version>4.1.2httpclient.version> 26 <jackson.version>1.9.13jackson.version> 27 <druid.version>1.0.5druid.version> 28 <jstl.version>1.2jstl.version> 29 <google.collections.version>1.0google.collections.version> 30 <cglib.version>3.1cglib.version> 31 <shiro.version>1.2.3shiro.version> 32 <commons.fileupload.version>1.3.1commons.fileupload.version> 33 <commons.codec.version>1.9commons.codec.version> 34 <commons.net.version>3.3commons.net.version> 35 <aspectj.version>1.6.12aspectj.version> 36 <netty.version>4.0.18.Finalnetty.version> 37 <hibernate.validator.version>5.1.1.Finalhibernate.validator.version> 38 properties>
1.2. 依赖库信息配置
1 <dependencies> 2 3 <dependency> 4 <groupId>org.springframeworkgroupId> 5 <artifactId>spring-coreartifactId> 6 <version>${spring.version}version> 7 dependency> 8 9 <dependency> 10 <groupId>org.springframeworkgroupId> 11 <artifactId>spring-webartifactId> 12 <version>${spring.version}version> 13 dependency> 14 15 <dependency> 16 <groupId>org.springframeworkgroupId> 17 <artifactId>spring-oxmartifactId> 18 <version>${spring.version}version> 19 dependency> 20 21 <dependency> 22 <groupId>org.springframeworkgroupId> 23 <artifactId>spring-txartifactId> 24 <version>${spring.version}version> 25 dependency> 26 27 <dependency> 28 <groupId>org.springframeworkgroupId> 29 <artifactId>spring-jdbcartifactId> 30 <version>${spring.version}version> 31 dependency> 32 33 <dependency> 34 <groupId>org.springframeworkgroupId> 35 <artifactId>spring-webmvcartifactId> 36 <version>${spring.version}version> 37 dependency> 38 39 <dependency> 40 <groupId>org.springframeworkgroupId> 41 <artifactId>spring-aopartifactId> 42 <version>${spring.version}version> 43 dependency> 44 45 <dependency> 46 <groupId>org.springframeworkgroupId> 47 <artifactId>spring-context-supportartifactId> 48 <version>${spring.version}version> 49 dependency> 50 51 <dependency> 52 <groupId>org.springframeworkgroupId> 53 <artifactId>spring-testartifactId> 54 <version>${spring.version}version> 55 dependency> 56 57 58 59 <dependency> 60 <groupId>org.apache.shirogroupId> 61 <artifactId>shiro-springartifactId> 62 <version>${shiro.version}version> 63 dependency> 64 <dependency> 65 <groupId>org.apache.shirogroupId> 66 <artifactId>shiro-ehcacheartifactId> 67 <version>${shiro.version}version> 68 dependency> 69 <dependency> 70 <groupId>org.apache.shirogroupId> 71 <artifactId>shiro-coreartifactId> 72 <version>${shiro.version}version> 73 dependency> 74 <dependency> 75 <groupId>org.apache.shirogroupId> 76 <artifactId>shiro-webartifactId> 77 <version>${shiro.version}version> 78 dependency> 79 <dependency> 80 <groupId>org.apache.shirogroupId> 81 <artifactId>shiro-quartzartifactId> 82 <version>${shiro.version}version> 83 dependency> 84 85 86 <dependency> 87 <groupId>org.mybatisgroupId> 88 <artifactId>mybatisartifactId> 89 <version>${mybatis.version}version> 90 dependency> 91 92 <dependency> 93 <groupId>org.mybatisgroupId> 94 <artifactId>mybatis-springartifactId> 95 <version>${mybatis.spring.version}version> 96 dependency> 97 98 99 100 <dependency> 101 <groupId>mysqlgroupId> 102 <artifactId>mysql-connector-javaartifactId> 103 <version>${mysql.connector.version}version> 104 dependency> 105 106 107 <dependency> 108 <groupId>com.alibabagroupId> 109 <artifactId>druidartifactId> 110 <version>${druid.version}version> 111 dependency> 112 113 114 <dependency> 115 <groupId>org.codehaus.jacksongroupId> 116 <artifactId>jackson-mapper-aslartifactId> 117 <version>${jackson.version}version> 118 dependency> 119 120 121 <dependency> 122 <groupId>log4jgroupId> 123 <artifactId>log4jartifactId> 124 <version>${log4j.version}version> 125 dependency> 126 <dependency> 127 <groupId>org.slf4jgroupId> 128 <artifactId>slf4j-apiartifactId> 129 <version>${slf4j.version}version> 130 dependency> 131 <dependency> 132 <groupId>org.slf4jgroupId> 133 <artifactId>slf4j-log4j12artifactId> 134 <version>${slf4j.version}version> 135 dependency> 136 137 138 139 <dependency> 140 <groupId>commons-fileuploadgroupId> 141 <artifactId>commons-fileuploadartifactId> 142 <version>${commons.fileupload.version}version> 143 dependency> 144 145 <dependency> 146 <groupId>org.apache.httpcomponentsgroupId> 147 <artifactId>httpclientartifactId> 148 <version>${httpclient.version}version> 149 dependency> 150 151 <dependency> 152 <groupId>commons-codecgroupId> 153 <artifactId>commons-codecartifactId> 154 <version>${commons.codec.version}version> 155 dependency> 156 157 <dependency> 158 <groupId>commons-netgroupId> 159 <artifactId>commons-netartifactId> 160 <version>${commons.net.version}version> 161 dependency> 162 163 <dependency> 164 <groupId>commons-logginggroupId> 165 <artifactId>commons-loggingartifactId> 166 <version>1.1.3version> 167 dependency> 168 <dependency> 169 <groupId>commons-collectionsgroupId> 170 <artifactId>commons-collectionsartifactId> 171 <version>3.2.1version> 172 dependency> 173 174 175 176 <dependency> 177 <groupId>com.google.collectionsgroupId> 178 <artifactId>google-collectionsartifactId> 179 <version>${google.collections.version}version> 180 dependency> 181 182 183 <dependency> 184 <groupId>cglibgroupId> 185 <artifactId>cglib-nodepartifactId> 186 <version>${cglib.version}version> 187 dependency> 188 189 190 <dependency> 191 <groupId>org.aspectjgroupId> 192 <artifactId>aspectjweaverartifactId> 193 <version>${aspectj.version}version> 194 dependency> 195 <dependency> 196 <groupId>org.aspectjgroupId> 197 <artifactId>aspectjrtartifactId> 198 <version>${aspectj.version}version> 199 dependency> 200 201 202 <dependency> 203 <groupId>org.hibernategroupId> 204 <artifactId>hibernate-validatorartifactId> 205 <version>${hibernate.validator.version}version> 206 dependency> 207 208 209 <dependency> 210 <groupId>io.nettygroupId> 211 <artifactId>netty-allartifactId> 212 <version>${netty.version}version> 213 dependency> 214 215 216 <dependency> 217 <groupId>junitgroupId> 218 <artifactId>junitartifactId> 219 <version>${junit.version}version> 220 <scope>testscope> 221 dependency> 222 223 224 <dependency> 225 <groupId>javax.servletgroupId> 226 <artifactId>javax.servlet-apiartifactId> 227 <version>3.0.1version> 228 <scope>providedscope> 229 dependency> 230 231 232 <dependency> 233 <groupId>javax.servletgroupId> 234 <artifactId>jstlartifactId> 235 <version>${jstl.version}version> 236 dependency> 237 dependencies>
1.3. 编译及tomcat部署配置
1 <build> 2 <finalName>shiro01finalName> 3 <plugins> 4 5 <plugin> 6 <groupId>org.apache.maven.pluginsgroupId> 7 <artifactId>maven-war-pluginartifactId> 8 <version>2.6version> 9 <configuration> 10 <webXml>src/main/webapp/WEB-INF/web.xmlwebXml> 11 configuration> 12 plugin> 13 14 <plugin> 15 <groupId>org.apache.tomcat.mavengroupId> 16 <artifactId>tomcat7-maven-pluginartifactId> 17 <version>2.2version> 18 <configuration> 19 <url>http://localhost:8080/managerurl> 20 <server>tomcatserver> 21 <username>hunterusername> 22 <password>hunterpassword> 23 <path>/shiro01path> 24 <contextReloadable>truecontextReloadable> 25 configuration> 26 plugin> 27 28 <plugin> 29 <groupId>org.mybatis.generatorgroupId> 30 <artifactId>mybatis-generator-maven-pluginartifactId> 31 <version>${plugin.mybatis.generator}version> 32 <configuration> 33 <configurationFile>${mybatis.generator.generatorConfig.xml}configurationFile> 34 <overwrite>trueoverwrite> 35 <verbose>trueverbose> 36 configuration> 37 plugin> 38 plugins> 39 build>
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 16 <context:component-scan base-package="com.hunter.shiro.web.controller"/> 17 18 19 20 <mvc:annotation-driven validator="validator"/> 21 22 23 <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> 24 <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/> 25 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 34 <value>classpath:messagesvalue> 35 <value>classpath:org/hibernate/validator/ValidationMessagesvalue> 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 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-8value> 64 <value>application/json;charset=UTF-8value> 65 list> 66 property> 67 bean> 68 69 70 71 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 72 p:prefix="/WEB-INF/views/" p:suffix=".jsp"/> 73 74 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>
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
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 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.propertiesvalue> 31 list> 32 property> 33 bean> 34 35 36 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 37 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 62 <property name="poolPreparedStatements" value="false"/> 63 <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> 64 65 66 <property name="filters" value="stat"/> 67 bean> 68 69 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 74 <property name="mapperLocations" value="classpath:mappers/*.xml" /> 75 bean> 76 77 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 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 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 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 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 113 <aop:aspectj-autoproxy proxy-target-class="true"/> 114 115 116 <tx:annotation-driven transaction-manager="transactionManager"/> 117 118 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>
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 11 <settings> 12 13 14 <setting name="cacheEnabled" value="true"/> 15 16 17 <setting name="lazyLoadingEnabled" value="true"/> 18 19 20 <setting name="multipleResultSetsEnabled" value="true"/> 21 22 23 <setting name="useColumnLabel" value="true"/> 24 25 26 <setting name="useGeneratedKeys" value="false"/> 27 28 29 <setting name="autoMappingBehavior" value="PARTIAL"/> 30 31 32 33 34 35 36 37 38 <setting name="safeRowBoundsEnabled" value="false"/> 39 40 41 <setting name="mapUnderscoreToCamelCase" value="true"/> 42 43 45 <setting name="localCacheScope" value="SESSION"/> 46 47 49 <setting name="jdbcTypeForNull" value="OTHER"/> 50 51 52 <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/> 53 54 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>
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 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 45 <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> 46 <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"/> 47 bean> 48 49 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 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>
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 5 <defaultCache maxEntriesLocalHeap="10000" eternal="true" 6 timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" 7 maxEntriesLocalDisk="100000" /> 8 ehcache>
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>
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
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 7 8 <context-param> 9 <param-name>contextConfigLocationparam-name> 10 <param-value> 11 classpath*:spring-mybatis.xml 12 classpath*:spring-shiro.xml 13 param-value> 14 context-param> 15 16 <listener> 17 <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> 18 listener> 19 20 21 22 <filter> 23 <filter-name>encodingFilterfilter-name> 24 <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> 25 <init-param> 26 <param-name>encodingparam-name> 27 <param-value>UTF-8param-value> 28 init-param> 29 <init-param> 30 <param-name>forceEncodingparam-name> 31 <param-value>trueparam-value> 32 init-param> 33 filter> 34 <filter-mapping> 35 <filter-name>encodingFilterfilter-name> 36 <url-pattern>/*url-pattern> 37 filter-mapping> 38 39 40 <filter> 41 <filter-name>shiroFilterfilter-name> 42 <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class> 43 <async-supported>trueasync-supported> 44 <init-param> 45 <param-name>targetFilterLifecycleparam-name> 46 <param-value>trueparam-value> 47 init-param> 48 filter> 49 <filter-mapping> 50 <filter-name>shiroFilterfilter-name> 51 <url-pattern>/*url-pattern> 52 filter-mapping> 53 54 55 <context-param> 56 <param-name>log4jConfigLocationparam-name> 57 <param-value>classpath:log4j.propertiesparam-value> 58 context-param> 59 60 <context-param> 61 <param-name>log4jRefreshIntervalparam-name> 62 <param-value>60000param-value> 63 context-param> 64 65 66 <listener> 67 <listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class> 68 listener> 69 70 71 <servlet> 72 <servlet-name>dispatcherservlet-name> 73 <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> 74 <init-param> 75 <param-name>contextConfigLocationparam-name> 76 <param-value>classpath*:spring-mvc.xmlparam-value> 77 init-param> 78 <load-on-startup>1load-on-startup> 79 servlet> 80 <servlet-mapping> 81 <servlet-name>dispatcherservlet-name> 82 83 <url-pattern>*.htmurl-pattern> 84 servlet-mapping> 85 86 87 <welcome-file-list> 88 <welcome-file>index.jspwelcome-file> 89 welcome-file-list> 90 91 92 <error-page> 93 <error-code>404error-code> 94 <location>/404.jsplocation> 95 error-page> 96 <error-page> 97 <error-code>500error-code> 98 <location>/500.jsplocation> 99 error-page> 100 web-app>
再来看看webapp 下文件路径信息吧
我们先来启动一次项目,确保配置信息正确
直接点击项目运行maven build 在弹出框输入 tomcat:run apply 后直接运行即可。
如果能正确访问到index.jsp页面,说明之前配置信息没得问题啦