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

基于Maven的Spring/SpringMVC/Mybatis/Mybatis-plus/Apache-Shiro基础环境配置

时间:2016-11-02 14:40:49      阅读:600      评论:0      收藏:0      [点我收藏+]

标签:2.x   seconds   sage   apach   通过   jsp   run   index   ntc   

记录一下,以便不时之需。

pom.xml

  1 <properties>
  2         <servlet-api.version>2.5</servlet-api.version>
  3         <spring.version>4.2.5.RELEASE</spring.version>
  4         <aspectjweaver.version>1.8.9</aspectjweaver.version>
  5 
  6         <mybatis.version>3.4.0</mybatis.version>
  7         <mybatis.spring.version>1.3.0</mybatis.spring.version>
  8 
  9         <!-- log -->
 10         <logback-classic.version>1.1.7</logback-classic.version>
 11         <logback-ext-spring.version>0.1.4</logback-ext-spring.version>
 12         <jcl-over-slf4j.version>1.7.21</jcl-over-slf4j.version>
 13 
 14         <!-- data -->
 15         <druid.version>1.0.18</druid.version>
 16         <mysql-connector-java.version>5.1.38</mysql-connector-java.version>
 17 
 18         <!-- shiro -->
 19         <shiro.version>1.2.2</shiro.version>
 20 
 21         <!-- test -->
 22         <junit.version>4.12</junit.version>
 23     </properties>
 24 
 25     <dependencies>
 26         <dependency>
 27             <groupId>javax.servlet</groupId>
 28             <artifactId>servlet-api</artifactId>
 29             <version>${servlet-api.version}</version>
 30             <scope>provided</scope>
 31         </dependency>
 32         <!-- https://mvnrepository.com/artifact/jstl/jstl -->
 33         <dependency>
 34             <groupId>jstl</groupId>
 35             <artifactId>jstl</artifactId>
 36             <version>1.2</version>
 37         </dependency>
 38         <dependency>
 39             <groupId>javax.mail</groupId>
 40             <artifactId>mail</artifactId>
 41             <version>1.4.7</version>
 42         </dependency>
 43 
 44         <dependency>
 45             <groupId>org.aspectj</groupId>
 46             <artifactId>aspectjweaver</artifactId>
 47             <version>${aspectjweaver.version}</version>
 48         </dependency>
 49         <dependency>
 50             <groupId>org.springframework</groupId>
 51             <artifactId>spring-core</artifactId>
 52             <version>${spring.version}</version>
 53         </dependency>
 54 
 55         <dependency>
 56             <groupId>org.springframework</groupId>
 57             <artifactId>spring-web</artifactId>
 58             <version>${spring.version}</version>
 59         </dependency>
 60         <dependency>
 61             <groupId>org.springframework</groupId>
 62             <artifactId>spring-oxm</artifactId>
 63             <version>${spring.version}</version>
 64         </dependency>
 65         <dependency>
 66             <groupId>org.springframework</groupId>
 67             <artifactId>spring-tx</artifactId>
 68             <version>${spring.version}</version>
 69         </dependency>
 70 
 71         <dependency>
 72             <groupId>org.springframework</groupId>
 73             <artifactId>spring-jdbc</artifactId>
 74             <version>${spring.version}</version>
 75         </dependency>
 76 
 77         <dependency>
 78             <groupId>org.springframework</groupId>
 79             <artifactId>spring-webmvc</artifactId>
 80             <version>${spring.version}</version>
 81         </dependency>
 82         <dependency>
 83             <groupId>org.springframework</groupId>
 84             <artifactId>spring-aop</artifactId>
 85             <version>${spring.version}</version>
 86         </dependency>
 87 
 88         <dependency>
 89             <groupId>org.springframework</groupId>
 90             <artifactId>spring-context-support</artifactId>
 91             <version>${spring.version}</version>
 92         </dependency>
 93 
 94         <dependency>
 95             <groupId>org.springframework</groupId>
 96             <artifactId>spring-test</artifactId>
 97             <version>${spring.version}</version>
 98         </dependency>
 99         <dependency>
100             <groupId>org.mybatis</groupId>
101             <artifactId>mybatis</artifactId>
102             <version>${mybatis.version}</version>
103         </dependency>
104         <dependency>
105             <groupId>org.mybatis</groupId>
106             <artifactId>mybatis-spring</artifactId>
107             <version>${mybatis.spring.version}</version>
108         </dependency>
109 
110 
111         <!-- log begin -->
112         <dependency>
113             <groupId>ch.qos.logback</groupId>
114             <artifactId>logback-classic</artifactId>
115             <version>${logback-classic.version}</version>
116         </dependency>
117         <dependency>
118             <groupId>org.logback-extensions</groupId>
119             <artifactId>logback-ext-spring</artifactId>
120             <version>${logback-ext-spring.version}</version>
121         </dependency>
122         <dependency>
123             <groupId>org.slf4j</groupId>
124             <artifactId>jcl-over-slf4j</artifactId>
125             <version>${jcl-over-slf4j.version}</version>
126         </dependency>
127         <!-- log end -->
128 
129         <!-- data begin -->
130         <dependency>
131             <groupId>com.alibaba</groupId>
132             <artifactId>druid</artifactId>
133             <version>${druid.version}</version>
134         </dependency>
135         <dependency>
136             <groupId>mysql</groupId>
137             <artifactId>mysql-connector-java</artifactId>
138             <version>${mysql-connector-java.version}</version>
139         </dependency>
140         <!-- data end -->
141 
142         <!-- test begin -->
143         <dependency>
144             <groupId>junit</groupId>
145             <artifactId>junit</artifactId>
146             <version>${junit.version}</version>
147             <scope>test</scope>
148         </dependency>
149         <!-- test end -->
150 
151         <!-- json start -->
152         <dependency>
153             <groupId>com.fasterxml.jackson.core</groupId>
154             <artifactId>jackson-core</artifactId>
155             <version>2.1.0</version>
156         </dependency>
157         <dependency>
158             <groupId>com.fasterxml.jackson.core</groupId>
159             <artifactId>jackson-databind</artifactId>
160             <version>2.1.0</version>
161         </dependency>
162         <dependency>
163             <groupId>com.fasterxml.jackson.core</groupId>
164             <artifactId>jackson-annotations</artifactId>
165             <version>2.1.0</version>
166         </dependency>
167         <dependency>
168             <groupId>com.alibaba</groupId>
169             <artifactId>fastjson</artifactId>
170             <version>1.2.9</version>
171         </dependency>
172         <!-- json end -->
173 
174 
175         <!-- shiro start -->
176         <dependency>
177             <groupId>org.apache.shiro</groupId>
178             <artifactId>shiro-core</artifactId>
179             <version>1.2.2</version>
180         </dependency>
181         <dependency>
182             <groupId>org.apache.shiro</groupId>
183             <artifactId>shiro-web</artifactId>
184             <version>1.2.2</version>
185         </dependency>
186         <dependency>
187             <groupId>org.apache.shiro</groupId>
188             <artifactId>shiro-spring</artifactId>
189             <version>1.2.2</version>
190         </dependency>
191         <!-- shiro end -->
192         <!-- fileupload start -->
193         <dependency>
194             <groupId>commons-fileupload</groupId>
195             <artifactId>commons-fileupload</artifactId>
196             <version>1.2.1</version>
197         </dependency>
198         <dependency>
199             <groupId>commons-io</groupId>
200             <artifactId>commons-io</artifactId>
201             <version>2.5</version>
202         </dependency>
203         <!-- fileupload end -->
204         
205         <!-- poi -->
206         <dependency>
207             <groupId>org.apache.poi</groupId>
208             <artifactId>poi-ooxml</artifactId>
209             <version>3.9</version>
210         </dependency>
211 
212 
213 
214     </dependencies>
215 
216     <profiles>
217         <profile>
218             <id>jdk16</id>
219             <activation>
220                 <activeByDefault>true</activeByDefault>
221                 <jdk>1.6</jdk>
222             </activation>
223             <properties>
224                 <maven.compiler.source>1.6</maven.compiler.source>
225                 <maven.compiler.target>1.6</maven.compiler.target>
226                 <maven.compiler.compilerVersion>1.6</maven.compiler.compilerVersion>
227                 <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
228             </properties>
229         </profile>
230     </profiles>
231 
232     <build>
233         <finalName>gzwlw</finalName>
234         <resources>
235             <resource>
236                 <directory>src/main/java</directory>
237                 <includes>
238                     <include>**/*.xml</include>
239                 </includes>
240             </resource>
241             <resource>
242                 <directory>src/main/resources</directory>
243             </resource>
244         </resources>
245         <plugins>
246             <plugin>
247                 <artifactId>maven-compiler-plugin</artifactId>
248                 <configuration>
249                     <source>1.6</source>
250                     <target>1.6</target>
251                     <encoding>UTF-8</encoding>
252                 </configuration>
253             </plugin>
254             <!--jetty 插件,mvn jetty:run -->
255             <plugin>
256                 <groupId>org.eclipse.jetty</groupId>
257                 <artifactId>jetty-maven-plugin</artifactId>
258                 <version>9.3.7.v20160115</version>
259                 <configuration>
260                     <webApp>
261                         <contextPath>/</contextPath>
262                         <descriptor>${basedir}/src/main/webapp/WEB-INF/web.xml</descriptor>
263                     </webApp>
264                     <stopKey>exit</stopKey>
265                     <stopPort>9091</stopPort>
266                     <webAppSourceDirectory>${project.basedir}/src/main/webapp</webAppSourceDirectory>
267                     <scanIntervalSeconds>0</scanIntervalSeconds>
268                     <httpConnector>
269                         <port>8080</port>
270                     </httpConnector>
271                 </configuration>
272             </plugin>
273             <!-- tomcat 插件,mvn tomcat:run -->
274             <plugin>
275                 <groupId>org.codehaus.mojo</groupId>
276                 <artifactId>tomcat-maven-plugin</artifactId>
277                 <version>1.1</version>
278                 <configuration>
279                     <path>/</path>
280                     <port>8080</port>
281                     <uriEncoding>UTF-8</uriEncoding>
282                     <warFile>${project.build.directory}/${project.build.finalName}.war</warFile>
283                 </configuration>
284             </plugin>
285         </plugins>
286 
287     </build>

jdbc.properties

## MySqlConfig
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="true" scanPeriod="1000 seconds">
    <timestamp key="date" datePattern="yyyyMMdd" />
    <!-- 输出到控制台 -->
    <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>[%d{yyyy-MM-dd HH:mm:ss} %level %c:%L]\n%msg%n\n</pattern>
<!--             <charset>UTF-8</charset> -->
        </encoder>
    </appender>
    <!-- 输出到文件 -->
    <appender name="File"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>./logs/ssm.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>
                ./logs/ssm.%d{yyyy-MM-dd}.log
            </FileNamePattern>
            <!-- 保存前10天的日志文件 -->
            <MaxHistory>10</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>[%d{yyyy-MM-dd HH:mm:ss} %level %c:%L]\n%msg%n\n</pattern>
<!--             <charset>UTF-8</charset> -->
        </encoder>
    </appender>

    <!-- 输出到数据库 -->
    <appender name="Database" class="ch.qos.logback.classic.db.DBAppender">
        <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
            <dataSource class="com.alibaba.druid.pool.DruidDataSource">
                <driverClass>com.mysql.jdbc.Driver</driverClass>
                <url>jdbc:mysql://127.0.0.1:3306/test</url>
                <user>root</user>
                <password>root</password>
            </dataSource>
        </connectionSource>
    </appender>
    
    <!-- 打印级别,大小写无关:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF 默认是DEBUG -->
    <root level="DEBUG">
        <appender-ref ref="Console" />
        <!-- 不写入文件和数据库(影响程序效率)
        <appender-ref ref="File" />
        <appender-ref ref="Database" /> -->
    </root>
    <logger name="org.springframework" level="ERROR" />
    
    <!-- 不需要打印日志的包-->
    <logger name="org.apache.shiro" level="OFF" />
</configuration>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 
     | 全局配置设置
     |
     | 可配置选项                                        默认值, 描述
     |
     | aggressiveLazyLoading     true, 当设置为‘true’的时候,懒加载的对象可能被任何懒属性全部加载。否则,每个属性都按需加载。
     | multipleResultSetsEnabled true, 允许和不允许单条语句返回多个数据集(取决于驱动需求)
     | useColumnLabel            true, 使用列标签代替列名称。不同的驱动器有不同的作法。参考一下驱动器文档,或者用这两个不同的选项进行测试一下。
     | useGeneratedKeys          false, 允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。
     | autoMappingBehavior       PARTIAL, 指定MyBatis 是否并且如何来自动映射数据表字段与对象的属性。PARTIAL将只自动映射简单的,没有嵌套的结果。FULL 将自动映射所有复杂的结果。
     | defaultExecutorType       SIMPLE, 配置和设定执行器,SIMPLE 执行器执行其它语句。REUSE 执行器可能重复使用prepared statements 语句,BATCH执行器可以重复执行语句和批量更新。
     | defaultStatementTimeout   null, 设置一个时限,以决定让驱动器等待数据库回应的多长时间为超时
     | -->
    <settings>
        <!-- 这个配置使全局的映射器启用或禁用缓存 -->
        <setting name="cacheEnabled" value="true" />
        <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载 -->
        <setting name="lazyLoadingEnabled" value="true" />
        <setting name="multipleResultSetsEnabled" value="true" />
        <setting name="useColumnLabel" value="true" />
        <setting name="defaultExecutorType" value="REUSE" />
        <setting name="defaultStatementTimeout" value="25000" />
    </settings>
    
    <!-- mybatis 性能拦截器
    <plugins>
        <plugin interceptor="org.jeeweb.interceptor.PerformanceInterceptor"></plugin>
    </plugins> -->
</configuration>

spring-framework.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <!-- 激活自动代理功能 -->
    <aop:aspectj-autoproxy proxy-target-class="true" /> 
    
    <!-- 自动装载 @Component, @Repository, @Service, @Controller, @RestController, @ControllerAdvice-->
    <context:component-scan base-package="com.runnet">
        <!-- 排除@Controller, 该组件由servlet-context.xml单独扫描 -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <!-- 排除@Service, 该组件由app-transaction.xml单独扫描 -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>
</beans>

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
  
    <mvc:annotation-driven />
    <!-- 扫描 controller -->
    <context:component-scan base-package="org.jeeweb.controller" />

    <!-- 静态资源放行 -->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/js/**" location="/js/" />

    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <!-- Jackson转换器 -->
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"  > 
        <property name="messageConverters">
           <list>
              <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
           </list>
        </property>
     </bean>
    
    <mvc:annotation-driven>
        <!-- 编码转换 -->
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven> 
    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="32505856" /><!-- 上传文件大小限制为31M,31*1024*1024 -->
        <property name="maxInMemorySize" value="4096" />
    </bean>
</beans>

 

app-datasource.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

    <!-- 引入属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>

    <!-- 配置Druid数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <!-- MyBatis SqlSessionFactoryBean 配置 -->
    <bean id="sqlSessionFactory" class="org.jeeweb.core.mybatisplus.spring.MybatisSqlSessionFactoryBean">
           <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <property name="mapperLocations" value="classpath:org/jeeweb/**/dao/mapper/*Mapper.xml" />
        <property name="plugins">
            <array>
                <!-- 分页插件配置 -->
                <bean id="paginationInterceptor" class="org.jeeweb.core.mybatisplus.plugins.PaginationInterceptor">
                    <property name="dialectType" value="mysql" />
                </bean>
            </array>
        </property>
    </bean>
    
    <!-- 加载 mapper.xml 接口 配置文件 -->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.jeeweb.dao" />
    </bean>

</beans>

app-transaction.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

    <!-- 扫描 service -->
    <context:component-scan base-package="org.jeeweb.service" />
    
    <!-- 使用annotation注解方式配置事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 使用JDBC事物 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- 定义事务通知 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="select*" read-only="true" propagation="REQUIRED" />
            <tx:method name="delete*"  propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    
    <!-- 定义aop切面,并织入事务通知 -->
    <aop:config>
        <aop:pointcut id="transactionPointcut" expression="execution(* com.runnet.*.service.*.*(..))"/>
        <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice"/>
    </aop:config>
</beans>

app-shiro.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="  
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd  
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 自定义realm-->
    <bean id="myRealm" class="org.jeeweb.framework.authority.ShiroDataBaseRealm" />

    <!-- 配置权限管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="myRealm" />
        <!-- 缓存管理器 -->
        <property name="cacheManager" ref="cacheManager" />
    </bean>

    <!-- shiro的过滤器工厂类,id- shiroFilter和web.xml中配置的过滤器一致 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!-- 权限管理器 -->
        <property name="securityManager" ref="securityManager" />
        <!-- 登陆页面 -->
        <property name="loginUrl" value="/targetToLogin.do" />
        <!-- 登录成功后的跳转地址,如果你访问的是非/login地址,则跳到您访问的地址 -->
        <property name="successUrl" value="/main.do" />
        <!-- 如果您请求的资源不再您的权限范围,则跳转到/denied请求地址 -->
        <property name="unauthorizedUrl" value="/denied.do" />
        <!-- 权限配置 -->
        <property name="filterChainDefinitions">
            <value>
                <!-- anon表示此地址不需要任何权限即可访问 -->
                /resources/**=anon
                /login.do=anon
                /targetToLogin.do=anon
                /doLogin.do=anon
                /index.jsp=anon
                <!--所有的请求(除去配置的静态资源请求或请求地址为anon的请求)都要通过登录验证,如果未登录则跳到/login -->
                /** = authc
            </value>
        </property>
    </bean>

    <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<!-- Spring 配置路径 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/spring-framework.xml,classpath:spring/app-*.xml</param-value>
	</context-param>

	<!-- LogBack 日志管理文件 -->
	<context-param>
		<param-name>logbackConfigLocation</param-name>
		<param-value>classpath:logback.xml</param-value>
	</context-param>
	<listener>
		<listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
	</listener>

	<!-- Spring 监听启动 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>springServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springServlet</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<!-- 检测开发环境,/druid/index.html -->
	<servlet>
		<servlet-name>druidStatView</servlet-name>
		<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>druidStatView</servlet-name>
		<url-pattern>/druid/*</url-pattern>
	</servlet-mapping>

	<!-- Shiro配置 -->
	<filter>
		<filter-name>shiroFilter</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>shiroFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

  

基于Maven的Spring/SpringMVC/Mybatis/Mybatis-plus/Apache-Shiro基础环境配置

标签:2.x   seconds   sage   apach   通过   jsp   run   index   ntc   

原文地址:http://www.cnblogs.com/wuwz/p/6022638.html

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