ssm+shiro基础框架搭建(一)

菜鸟一枚,只能按部就班,不喜勿喷。本人用myeclipse,非maven项目,自己导入包,项目所需包下载地址:http://download.csdn.net/detail/lyw19930812/9662074

一、创建web项目,对应web.xml如下:


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    
    <welcome-file-list>
        <welcome-file>/tologin.htmlwelcome-file>
    welcome-file-list>
    
    <display-name>Archetype Created Web Applicationdisplay-name>
    
    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring.xml,classpath:spring-mybatis.xml, classpath:spring-shiro.xmlparam-value>
    context-param>
    
    
    <filter>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
     
    
    
    <filter>
        <filter-name>shiroFilterfilter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
        <init-param>
            <param-name>targetFilterLifecycleparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>shiroFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
     
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
    listener>
    
    <servlet>
        <description>spring mvc servletdescription>
        <servlet-name>springMvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring-mvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>springMvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.cssurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.gifurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.jpgurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.xlsurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.pngurl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.jsurl-pattern>
    servlet-mapping>
    
    <session-config>
        <session-timeout>15session-timeout>
    session-config>
    <error-page>
        <error-code>404error-code>
        <location>/WEB-INF/views/error/404.jsplocation>
    error-page>
    <error-page>
        <error-code>401error-code>
        <location>/WEB-INF/views/error/401.jsplocation>
    error-page>
web-app>

二、spring配置文件:spring.xml


<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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="true">
    
    <context:property-placeholder location="classpath:config.properties" />
    <context:annotation-config />
    
    <context:component-scan base-package="cn.ssms" />
    
    
    
    <aop:aspectj-autoproxy proxy-target-class="true"/>
beans>

三、spring-mvc


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

    
    <context:component-scan base-package="cn.ssms.controller" />
     <mvc:annotation-driven/>
    
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8value>
            list>
        property>
    bean>

    
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />
            list>
        property>
    bean>

    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" />
    bean>

    <mvc:resources location="/sources/**" mapping="/sources/" cache-period="31536000"/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding">
            <value>UTF-8value>
        property>
        <property name="maxUploadSize">
            <value>32505856value>
        property>
        <property name="maxInMemorySize">
            <value>4096value>
        property>
    bean>

beans>

四、spring-shiro


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans                 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-lazy-init="true">

    <description>Shiro Configurationdescription>
    
    <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.MemorySessionDAO"/>
    
    <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
    <property name="sessionDAO" ref="sessionDAO"/>
    bean>

    
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="shiroDbRealm" />
        <property name="cacheManager" ref="cacheManager" />
        <property name="sessionManager" ref="sessionManager" />
    bean>
    
    <bean id="forceLogoutFilter" class="cn.ssms.util.ForceLogoutFilter">bean>
    
    <bean id="shiroDbRealm" class="cn.ssms.realm.ShiroDbRealm">
        <property name="cacheManager" ref="cacheManager" />
    bean>
    
    
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager" />
        <property name="loginUrl" value="/tologin.html" />
        <property name="successUrl" value="/view/index.html" />
        <property name="unauthorizedUrl" value="/error/noperms.jsp" />
        <property name="filters">  
            <map>  
                <entry key="roleF" value-ref="forceLogoutFilter"/>
            map>  
        property> 
        <property name="filterChainDefinitions">
            <value>
                /forceLogout/*=roleF
                /sources/css/*=anon
                /sources/js/*=anon
                /sources/img/*=anon
                /sources/excel/*=anon
                /login.html = anon
                /tologin.html = anon
                /logout.html = anon
                /** = authc
            value>
        property>
    bean>

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

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

      
  
  
  

    
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
        depends-on="lifecycleBeanPostProcessor">
        <property name="proxyTargetClass" value="true" />
    bean>

    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager" />
    bean>
beans>

五、数据库配置文件config.properties

#hibernate.dialect=org.hibernate.dialect.OracleDialect
#driverClassName=oracle.jdbc.driver.OracleDriver
#validationQuery=SELECT 1 FROM DUAL
#jdbc_url=jdbc:oracle:thin:@localhost:1521:orcl
#jdbc_username=sypro
#jdbc_password=sypro

#hibernate.dialect=org.hibernate.dialect.MySQLDialect
#driverClassName=com.mysql.jdbc.Driver
#validationQuery=SELECT 1
#jdbc_url=jdbc:mysql://localhost:3306/shiro?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
#jdbc_username=root
#jdbc_password=root



hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
driverClassName=org.postgresql.Driver
validationQuery=SELECT 'x'
jdbc_url=jdbc:postgresql://localhost:5432/数据库名?useUnicode=true&characterEncoding=utf-8
jdbc_username=username
jdbc_password=passpword


#hibernate.dialect=org.hibernate.dialect.SQLServerDialect
#driverClassName=net.sourceforge.jtds.jdbc.Driver
#validationQuery=SELECT 1
#jdbc_url=jdbc:jtds:sqlserver://127.0.0.1:1433/sy
#jdbc_username=sa
#jdbc_password=123456

六、log日志log4j.properties

log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingFile.File=${catalina.home}/logs/springmvcshiro.log
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.org.apache.ibatis=DEBUG,RollingFile
log4j.additivity.org.apache.ibatis=false
log4j.logger.org.apache.shiro=ERROR,RollingFile
log4j.additivity.org.apache.shiro=true
log4j.logger.org.springframework.web.filter.DelegatingFilterProxy=ERROR,RollingFile
log4j.additivity.org.springframework.web.filter.DelegatingFilterProxy=false

七、spring-mybatis


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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-3.0.xsd 
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    
    

    

<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  destroy-method="close">

        <property name="driverClassName" value="${driverClassName}" />
        <property name="url" value="${jdbc_url}" />
        <property name="username" value="${jdbc_username}" />
        <property name="password" value="${jdbc_password}" />

        
        <property name="initialSize" value="1" />
        
        <property name="maxActive" value="20" />
        
        
        
        <property name="minIdle" value="0" />
        
        <property name="maxWait" value="60000" />

        

        <property name="validationQuery" value="${validationQuery}" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />

        
        <property name="timeBetweenEvictionRunsMillis" value="3000" />
        
        <property name="minEvictableIdleTimeMillis" value="300000" />

        
        <property name="removeAbandoned" value="true" />
        
        <property name="removeAbandonedTimeout" value="1800" />
        
        <property name="logAbandoned" value="true" />

        
        
        <property name="filters" value="mergeStat" />
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        
        <property name="mapperLocations" value="classpath:cn/ssms/mapping/*.xml" />
    bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.ssms.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    bean>

    
    <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="add*" propagation="REQUIRED" />
            <tx:method name="append*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="repair" propagation="REQUIRED" />
            <tx:method name="delAndRepair" propagation="REQUIRED" />

            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="load*" propagation="SUPPORTS" />
            <tx:method name="search*" propagation="SUPPORTS" />
            <tx:method name="datagrid*" propagation="SUPPORTS" />

            <tx:method name="*" propagation="SUPPORTS" />
        tx:attributes>
    tx:advice>
    <aop:config>
        <aop:pointcut id="transactionPointcut" expression="execution(* cn.ssms.service..*Impl.*(..))" />
        <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
    aop:config>

    
    <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
    bean>
    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
        <property name="patterns">
            <list>
                <value>cn.ssms.service.*value>
            list>
        property>
    bean>
    <aop:config>
        <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
    aop:config>

beans>

基本配置已经完成

你可能感兴趣的:(ssm+shiro)