1. 程式人生 > >shiro + springMVC + spring + mybatis (maven 整合)

shiro + springMVC + spring + mybatis (maven 整合)

1.大綱 :
a.編寫pom.xml,引入各種依賴包, 以下有模板
b.編寫applicationContext.xml檔案
c.編寫springMVC-servlet.xml檔案
d.編寫spring-shiro.xml檔案
e.編寫jdbc.properties檔案
f.編寫log4j.properties檔案
g.編寫ehcache-shiro.xml檔案(快取檔案配置)
h.編寫java檔案
i.project結構截圖 :
這裡寫圖片描述
(1).編寫pom.xml, 主要新增shiro-core.jar, shiro-web.jar, shiro-spring.jar, shiro-ehcache.jar, ehcache-core.jar等依賴包

這裡是pom.xml的模板
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.shiroTest</groupId>
  <artifactId
>
TestShiro</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 打包方式為war包 --> <packaging>war</packaging> <properties> <webVersion>3.0</webVersion> </properties> <!-- 引入依賴包 --> <dependencies> <!-- javaee的api -->
<dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <!-- spring依賴包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument-tomcat</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc-portlet</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.5</version> </dependency> <!-- c3p0的資料來源 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>5.2.12.Final</version> </dependency> <!-- 日誌依賴包 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency> <!-- commons包 --> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.0.4</version> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> </dependency> <!-- json依賴包 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.37</version> </dependency> <!-- 資料庫驅動依賴包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <!-- mybatis依賴包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.0</version> </dependency> <!-- 加入shrio依賴包 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.0</version> </dependency> <!-- 使用者儲存shiro認證資訊快取的 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.11</version> </dependency> </dependencies> <build> <plugins> <!-- 新增Tomcat容器 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8080</port> <server>tomcat</server> <username>admin</username> <password>admin</password> </configuration> </plugin> </plugins> <!--這裡是打包成war包的時候不用過濾的xml, properties檔案, 保證打包之後war包中有這些xml, properties檔案--> <resources> <resource> <directory>${basedir}/src/main/java</directory> <includes> <!--這裡主要是mybatics的mapper.xml--> <include>**/*.xml</include> </includes> </resource> <resource> <directory>${basedir}/src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> </resources> </build> </project>

(2)編寫applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
  xmlns:p="http://www.springframework.org/schema/p"
  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-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.0.xsd">

  <!-- 使用註解式注入 -->
  <context:annotation-config/>

  <!--掃描dao層和service層的包-->
  <context:component-scan base-package="com.shirotest.test.service.impl"/>

  <!-- 載入jdbc配置檔案的bean -->
  <bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:jdbc.properties</value>
    </property>
  </bean>

  <!-- 配置資料來源 -->
  <bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name = "driverClass" value = "${jdbc.driverClass}"></property>
  <property name = "jdbcUrl" value = "${jdbc.jdbcUrl}"></property>
  <property name = "user" value = "${jdbc.user}"></property>
  <property name = "password" value = "${jdbc.password}"></property>
  <!-- 初始化資料連線池的連線數 -->
  <property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
  <!-- 初始化連線最大空閒時間 -->
  <property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
  <!-- 初始化連線池的最大連線數 -->
  <property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
  <!-- 初始化連線池的最少連線數 -->
  <property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
  </bean>


  <!-- 配置會話工廠 -->
  <bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref = "dataSource"/>
      <!-- 自動掃描XXXmapper.xml檔案-->
      <property name="mapperLocations" value = "classpath:com/shirotest/mapper/UserinfoMapper.xml"/>
  </bean>

  <!-- 配置對映介面位置 -->
  <bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer">
      <!--掃描mapper下面的所有介面, 建立介面的動態代理-->
      <property name="basePackage" value = "com.shirotest.mapper"/>
      <property name="sqlSessionFactory" ref = "sqlSessionFactory"/>
  </bean>

  <!-- 將DataSource關聯到事務管理功能 -->
  <bean id= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref = "dataSource"/>
  </bean>
  <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

(3)編寫springMVC-servlect.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:mvc="http://www.springframework.org/schema/mvc"
  xmlns:p="http://www.springframework.org/schema/p"
  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-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.0.xsd">

  <!-- -->
  <mvc:annotation-driven/>
  <mvc:default-servlet-handler/>

  <!-- 啟動Spring MVC的註解功能,完成請求和註解POJO的對映 -->
  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

  <!--程式碼掃描ssm.controller包中帶有@Controller註解的控制層類-->

  <context:component-scan base-package="com.shirotest.test.controller"/>

  <!--程式碼對控制層進行事務代理AOP支援-->
  <aop:aspectj-autoproxy proxy-target-class="true"/>

</beans>

(4)編寫spring-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:mvc="http://www.springframework.org/schema/mvc"
  xmlns:p="http://www.springframework.org/schema/p"
  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-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- 啟用shiro授權註解攔截方式 -->
    <bean id = "shiroFilter" class = "org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!-- 裝配securityManager -->
        <property name="securityManager" ref = "securityManager"/>
        <!-- 配置登入頁面 -->
        <property name="loginUrl" value = "/html/login.html"/>
        <!-- 配置登陸成功後的頁面 -->
        <property name="successUrl" value = "/html/success.html"/>
        <!-- 配置未合法的的頁面 -->
        <property name="unauthorizedUrl" value = "/html/unauthorized.html"/>
        <!-- 配置哪些頁面訪問需要攔截 -->
        <property name="filterChainDefinitions">
            <value>
                /html/login.html = anon<!-- 無需認證或者許可權 -->
                /html/success.html = anon
                /html/fail.html = anon
                /html/user.html = roles[user]<!-- 必須認證並且是擁有user角色才能訪問 -->
                /html/admin.html = roles[admin]<!-- 必須認證並且擁有admin角色才能訪問 -->
                /html/logout = logout
            </value>
        </property>
    </bean>

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

    <!-- 配置進行授權和認證的Realm, 自己實現的Realm, 亦可以使用官方的Realm, 後續會說明Realm的寫法作用 -->
    <bean id = "shiroRealm" class = "com.shirotest.util.ShiroRealm">
        <!--在自定義的Realm中使用自定義的業務邏輯進行認證-->
        <property name="userService" ref = "userService"></property>
    </bean>

    <!-- 配置Shiro的securityManager bean, 核心的安全管理器-->
    <bean id = "securityManager" class = "org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!-- <property name="cacheManager"  ref = "cacheManager"/> -->
        <property name="realm" ref = "shiroRealm"/>
        <property name="sessionMode" value = "native"/>
    </bean>

    <!-- 配置 Bean 後置處理器: 會自動的呼叫和 Spring 整合後各個元件的生命週期方法 -->
    <bean id = "lifecycleBeanPostProcessor" class = "org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

</beans>

(5)編寫jdbc.properties檔案

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/testssm?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.user=root
jdbc.password=root
c3p0.initialPoolSize=10
c3p0.maxIdleTime=30
c3p0.maxPoolSize=100
c3p0.minPoolSize=10

(6)編寫log4j.properties檔案

#輸出的登記為INFO, 輸出型別是console, 輸出的目的地是logfile\
log4j.rootCategory=INFO,console,logfile

#配置控制檯的日誌
#輸出的目的地是控制檯
log4j.appender.console=org.apache.log4j.ConsoleAppender
#設定輸出時候的端的佈局是哪種佈局
log4j.appender.console.layout=org.apache.log4j.PatternLayout
#指定輸出的具體資訊, 以及具體格式
log4j.appender.console.layout.ConversionPattern= %p %d{yyyy-MM-dd HH:mm:ss} - %t - [%c] : %m %n

#配置指定日誌列印日誌檔案
#配置日誌檔案的路徑
log4j.appender.logfile.File=D\:\\Workspaces\\MyEclipse 2017 CI\\.metadata\\.me_tcat85\\logs\\TestShiro_log\\modeltest.log
#配置日誌檔案每天產生一個
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
#配置日誌檔案的日期格式
log4j.appender.logfile.DatePattern=.yyyy-MM-dd
#配置日誌檔案的佈局格式
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
# 配置日誌檔案日誌列印的格式
log4j.appender.logfile.layout.ConversionPattern=%p %d{yyyy-MM-dd HH:mm:ss} - %t - [%c] : %m %n

(7)編寫ehcache-shiro.xml檔案

<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false"  name="shirocache"> 
    <diskStore path="java.io.tmpdir"/>
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
        />
    <!-- 登入記錄快取 鎖定10分鐘 -->
    <cache name="passwordRetryCache"
           maxEntriesLocalHeap="2000"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>
    <cache name="authorizationCache"
           maxEntriesLocalHeap="2000"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>
    <cache name="authenticationCache"
           maxEntriesLocalHeap="2000"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>
    <cache name="shiro-activeSessionCache"
           maxEntriesLocalHeap="2000"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>