Spring+Struts+Mybatis+Shiro整合配置

Jar包

 

 

 

 

 

 

----------------------------------------------------Spring配置---------------------------------

applicationContext-dao.xml

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

    <!-- 0.连接池属性设置读取指定的properties文件 -->
    <context:property-placeholder location="classpath:db.properties" />    

    <!-- 1.将连接池放入spring容器 -->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    
    
    
    <!--2. 配置 Mybatis的会话工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 配置Mybatis的核心 配置文件所在位置 -->
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
    </bean>
    
    
    
    <!-- 3.1  mapper代理配置方法一   这种方法需要大量重复的配置代理对象
    MapperFactoryBean:根绝mapper接口生成代理对象
    
    <bean id="selectUser" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="cn.qlq.core.dao.SelectUser"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
     -->
     
     
     
     
     <!-- 3.2通过MapperScannerConfigurer扫描进行批量生成代理对象 
         遵循规范:mapper.java和mapper.xml名字一样且在同一个目录下
        自动扫描出来的代理对象的id为mapper类类名(首字母小写)     
     -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <!-- 指定扫描的包名,如果有多个,用半角逗号分隔 -->
         <property name="basePackage" value="cn.qlq.mapper"></property>
         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
     </bean>
     
     
    <!-- 4.配置事务管理器 -->
    <!-- 事务核心管理器,封装了事务操作,依赖于连接池 -->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 5.开启注解管理aop事务 -->
    <tx:annotation-driven/>
    
    
    
    <!-- 事务模板对象,依赖于事务核心管理器 -->
    <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager"></property>
    </bean>    

    <!-- ················开始使用XML管理事务························  --> 
    <!--  配置事务通知(无论哪种方式都要用到事务的核心管理器)-->
    <tx:advice transaction-manager="transactionManager" id="firstTx">
        <tx:attributes>
            <!--以方法为单位,指定方法应用事务什么属性
             isolation:隔离级别
             read-only:只读属性
             propagation:传播行为
             -->
             <!-- 企业中运用通配符命名规则。两套增删改查(8种) -->
            <tx:method name="save*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
            <tx:method name="persist*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
            <tx:method name="delete*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
            <tx:method name="remove*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
            <tx:method name="update*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
            <tx:method name="modify*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
            <tx:method name="get*" isolation="DEFAULT" read-only="true" propagation="REQUIRED"/>
            <tx:method name="find*" isolation="DEFAULT" read-only="true" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 配置织入 -->
    <aop:config>
        <!-- 配置切点表达式 -->
        <aop:pointcut expression="execution(* cn.qlq.Service.*ServiceImpl.*(..))" id="texPc"/>
        <!-- 配置切面:切点+通知
        advice-ref:通知名称
        pointcut-ref:切点名称
         -->
        <aop:advisor advice-ref="firstTx" pointcut-ref="texPc"/>
    </aop:config>
</beans>

 applicationContext-action.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    
    <!-- 与struts2整合的配置 -->
    <context:component-scan base-package="cn.qlq.Action"></context:component-scan>



</beans>

 

 

applicationContext-service.xml

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

    <!--4.注解扫描service  -->
    <!-- 4.开启组件自动扫描,也就是启用注解。前提是导入spring-context-3.2.xsd约束和引入新的命名空间,注解扫描service出错了 -->
    
        <context:component-scan base-package="cn.qlq.service"></context:component-scan> 
    
    

</beans>

 

 

----------------------------------------Mybatis配置----------------------------------------------------

sqlMapConfig.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>

    <!--  只需要定义个别名,这个应该有-->
     <!-- <typeAliases >
         <package name="cn.qlq.mapper"/>
     </typeAliases>       
    动态代理也不需要配置这个扫描,留着也行
    <mappers>
        原始DAO开发使用这个手动加载xml
        <package name="cn.xm.mapper"/> 
    </mappers> -->
    
</configuration>

 

----------------------------------------Struts配置----------------------------------------------

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
    <!-- 错误处理页面,其他配置文件可以继承这个package -->
    <package name="default" namespace="/" extends="struts-default">
        <!-- 全局结果集,所有action都可以使用 -->
        <global-results>
            <result name="myerror">/page/error/myerror.jsp</result>
            <result name="error">/page/error/error.jsp</result>
        </global-results>
        <global-exception-mappings>
            <!-- 可以配置多个,如果捕获的异常类型不知道会被error2拦截 ,优先匹配明确类型的exception-->
            <!-- 自定义异常 -->
            <exception-mapping result="myerror"    exception="cn.qlq.Exception.MyException"></exception-mapping>
            <!-- 未知异常 -->
            <exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
        </global-exception-mappings>
    </package>
    
    
    
    <include file="struts/error.xml"></include>
    <include file="struts/permission.xml"></include>
    <include file="struts/paper.xml"></include>
</struts>

 

 -----------------------------------------Shiro配置-------------------------------------

applicationContext-shiro.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- web.xml中shiro的filter对应的bean -->
    <!-- Shiro 的Web过滤器 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager" />
        <!-- loginUrl认证提交地址,如果没有认证将会请求此地址进行认证,请求此地址将由formAuthenticationFilter进行表单认证 -->
        <property name="loginUrl" value="/login.action" />
        <!-- 认证成功统一跳转到first.action,建议不配置,shiro认证成功自动到上一个请求路径 -->
        <property name="successUrl" value="/first.action" />
        <!-- 通过unauthorizedUrl指定没有权限操作时跳转页面 -->
        <property name="unauthorizedUrl" value="/refuse.jsp" />
        <!-- 自定义filter配置 -->
        <property name="filters">
            <map>
                <!-- 将自定义 的FormAuthenticationFilter注入shiroFilter中 -->
                <entry key="authc" value-ref="formAuthenticationFilter" />
            </map>
        </property>
        <!-- 过虑器链定义,从上向下顺序执行,一般将/**放在最下边 -->
        <property name="filterChainDefinitions">
            <value>
                <!-- 静态资源放行 -->
                <!-- /images/** = anon /js/** = anon /styles/** = anon 对错误页面放行 /error.jsp 
                    = anon -->
                <!-- 验证码,可匿名访问  -->
                /validatecode.jsp = anon

                <!-- 配置记住我或认证通过可以访问的地址 -->
                /index.jsp = user
                /first.action = user
                /welcome.jsp = user


                <!--增加试卷使用注解需要有增加权限 -->
                <!-- /paper_addSt.action = perms[paper:create] -->
                <!-- /user_findUser.action = perms[user:find] -->
                <!-- 删除用户需要有删除用户的权限 -->
                /user_deleteUser.action = perms[user:delete]
                

                <!-- 请求 logout.action地址,shiro去清除session -->
                /logout.action = logout

                <!-- /** = authc 所有url都必须认证通过才可以访问 -->
                /** = authc

                <!-- 所有url都可以匿名访问 -->
                <!-- /** = anon -->
            </value>
        </property>
    </bean>

    <!-- securityManager安全管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!-- 注入realm -->
        <property name="realm" ref="customRealm" />
        <!-- 注入缓存管理器 -->
        <property name="cacheManager" ref="cacheManager" />
        <!-- 注入session管理器 -->
        <property name="sessionManager" ref="sessionManager" />
        <!-- 记住我 -->
        <property name="rememberMeManager" ref="rememberMeManager" />
    </bean>

    <!-- realm -->
    <bean id="customRealm" class="cn.qlq.Shiro.CustomRealm">
        <!-- 将凭证匹配器设置到realm中,realm按照凭证匹配器的要求进行散列 -->
        <property name="credentialsMatcher" ref="credentialsMatcher" />
    </bean>

    <!-- 凭证匹配器 -->
    <bean id="credentialsMatcher"
        class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
        <property name="hashAlgorithmName" value="md5" />
        <property name="hashIterations" value="1" />
    </bean>

    <!-- 缓存管理器 -->
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml" />
    </bean>

    <!-- 会话管理器 -->
    <bean id="sessionManager"
        class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
        <!-- session的失效时长,单位毫秒 -->
        <property name="globalSessionTimeout" value="600000" />
        <!-- 删除失效的session -->
        <property name="deleteInvalidSessions" value="true" />
    </bean>

    <!-- 自定义form认证过虑器 -->
    <!-- 基于Form表单的身份验证过滤器,不配置将也会注册此过虑器,表单中的用户账号、密码及loginurl将采用默认值,建议配置 -->
    <bean id="formAuthenticationFilter" class="cn.qlq.Shiro.CustomFormAuthenticationFilter">
        <!-- 表单中账号的input名称 -->
        <property name="usernameParam" value="username" />
        <!-- 表单中密码的input名称 -->
        <property name="passwordParam" value="password" />
        <!-- 记住我input的名称 -->
        <property name="rememberMeParam" value="rememberMe" />
    </bean>


    <!-- rememberMeManager管理器,写cookie,取出cookie生成用户信息 -->
    <bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
        <property name="cookie" ref="rememberMeCookie" />
    </bean>
    <!-- 记住我cookie -->
    <bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
        <!-- rememberMe是cookie的名字 -->
        <constructor-arg value="rememberMe" />
        <!-- 记住我cookie生效时间30天 -->
        <property name="maxAge" value="2592000" />
    </bean>

</beans>

 

 shiro-ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
    <!--diskStore:缓存数据持久化的目录 地址  -->
    <diskStore path="F:\develop\ehcache" />
    <defaultCache 
        maxElementsInMemory="1000" 
        maxElementsOnDisk="10000000"
        eternal="false" 
        overflowToDisk="false" 
        diskPersistent="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120" 
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">
    </defaultCache>
</ehcache>

 

 

--------------------------------------数据库配置---------------------------------------------------------------

db.properties

;;;;;;;;;;;;;;;;;;;;
;DataBaseConnection;
;;;;;;;;;;;;;;;;;;;;


jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/shiro
jdbc.username=sa
jdbc.password=123456

 

 

----------------------------------------------Log4j配置---------------------------------------------------

log4j.properties

#设置logger级别DEBUG、INFO、WRNING、ERROR和输出格式A、B、C或D
log4j.rootLogger=DEBUG, A

#输出到控制台
log4j.appender.A=org.apache.log4j.ConsoleAppender
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n

#输出到E盘的log.log文件
log4j.appender.B=org.apache.log4j.FileAppender
log4j.appender.B.File=E:\\log.log
log4j.appender.B.layout=org.apache.log4j.SimpleLayout

#输出到E盘的log.html文件
log4j.appender.C=org.apache.log4j.RollingFileAppender
log4j.appender.C.File=E:\\log.html
log4j.appender.C.MaxFileSize=1000KB
log4j.appender.C.MaxBackupIndex=10
log4j.appender.C.layout=org.apache.log4j.HTMLLayout

log4j.appender.D=org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File=E:\\log.log
log4j.appender.D.layout=org.apache.log4j.TTCCLayout

 

posted @ 2017-07-31 22:49  QiaoZhi  阅读(713)  评论(0编辑  收藏  举报