shiro(2)-集成spring

? 加入 Spring 和 Shiro 的 jar 包
? 配置 Spring 及 SpringMVC
? 參照:1.3.2\shiro-root-1.3.2-source-
release\shiro-root-1.3.2\samples\spring 配置
web.xml 文件和 Spring 的配置文件

1 pom.xml

        <!-- 持久層依賴 開始 -->
        <!-- spring ioc組件需要的依賴包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <!-- spring 事務(wù)管理和JDBC依賴包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <!-- mysql數(shù)據(jù)庫(kù)驅(qū)動(dòng)包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>

        <!-- dbcp連接池的依賴包 -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <!-- mybatis依賴 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>

        <!-- mybatis和spring的整合依賴 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!-- 持久層依賴 結(jié)束 -->
        <!-- 業(yè)務(wù)層依賴 開始 -->
        <!-- 基于AspectJ的aop依賴 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- 業(yè)務(wù)層依賴 結(jié)束 -->


        <!-- 表現(xiàn)層依賴 開始 -->
        <!-- spring MVC依賴包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <!-- jstl 取決于視圖對(duì)象是否是JSP -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.6</version>
        </dependency>


        <!-- 表現(xiàn)層依賴 結(jié)束 -->

        <!-- spring 單元測(cè)試組件包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <!-- 單元測(cè)試Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!-- 文件上傳 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!-- shiro-all -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-all</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!-- ehcache-core -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.4.3</version>
        </dependency>

2 web.xml

   <!-- 配置前端控制器加載spring子容器 -->
    <servlet>
        <servlet-name>ssm</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ssm</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 配置ContextLoaderListener監(jiān)聽器加載spring父容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- <param-value> classpath:spring/applicationContext-dao.xml, classpath:spring/applicationContext-service.xml,
            classpath:spring/applicationContext-tx.xml </param-value> -->
        <!-- <param-value> classpath:spring/import.xml </param-value> -->
        <param-value>
            classpath:spring/applicationContext-*.xml
        </param-value>
    </context-param>

    <!-- 監(jiān)聽器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- POST亂碼過濾器 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Shiro Filter is defined in the spring application context: -->
    <!--
    1. 配置  Shiro 的 shiroFilter.
    2. DelegatingFilterProxy 實(shí)際上是 Filter 的一個(gè)代理對(duì)象. 默認(rèn)情況下, Spring 會(huì)到 IOC 容器中查找和
    <filter-name> 對(duì)應(yīng)的 filter bean. 也可以通過 targetBeanName 的初始化參數(shù)來配置 filter bean 的 id.
    -->
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

3 db.properties

db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://xxx/online_retailer?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false
db.username=root
db.password=root

4 log4j.properties

#dev env [debug] product env [info]
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

5 ehcache.xml

<ehcache>

    <!-- Sets the path to the directory where cache .data files are created.

         If the path is a Java System Property it is replaced by
         its value in the running VM.

         The following properties are translated:
         user.home - User's home directory
         user.dir - User's current working directory
         java.io.tmpdir - Default temp file path -->
    <diskStore path="java.io.tmpdir"/>
    
    <cache name="authorizationCache"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>

    <cache name="authenticationCache"

           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>

    <cache name="shiro-activeSessionCache"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>

    <!--Default Cache configuration. These will applied to caches programmatically created through
        the CacheManager.

        The following attributes are required for defaultCache:

        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"


        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU"
        />

    <!--Predefined caches.  Add your cache configuration settings here.
        If you do not have a configuration for your cache a WARNING will be issued when the
        CacheManager starts

        The following attributes are required for defaultCache:

        name              - Sets the name of the cache. This is used to identify the cache. It must be unique.
        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->

    <!-- Sample cache named sampleCache1
        This cache contains a maximum in memory of 10000 elements, and will expire
        an element if it is idle for more than 5 minutes and lives for more than
        10 minutes.

        If there are more than 10000 elements it will overflow to the
        disk cache, which in this configuration will go to wherever java.io.tmp is
        defined on your system. On a standard Linux system this will be /tmp"
        -->
    <!--<cache name="sampleCache1"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
        />-->

    <!-- Sample cache named sampleCache2
        This cache contains 1000 elements. Elements will always be held in memory.
        They are not expired. -->
    <!--<cache name="sampleCache2"
        maxElementsInMemory="1000"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
        /> -->

    <!-- Place configuration for your caches following -->

</ehcache>

6 applicationContext-dao.xml

<!-- 讀取java配置文件丢郊,替換占位符數(shù)據(jù) -->
    <context:property-placeholder
        location="classpath:db.properties" />

    <!-- 配置數(shù)據(jù)源 -->
    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName"
            value="${db.driverClassName}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>
    <!-- 配置SqlSessionFactory -->
    <bean id="sqlSessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入dataSource -->
        <property name="dataSource" ref="dataSource"></property>

        <!-- mybatis批量別名配置 -->
        <property name="typeAliasesPackage" value="com.kkb.ssm.po"></property>

    </bean>

    <!-- 批量代理對(duì)象的生成 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定需要生成代理的接口所在的包名 -->
        <property name="basePackage" value="com.kkb.ssm.mapper"></property>

    </bean>

7 applicationContext-service.xml

<!-- 掃描業(yè)務(wù)bean -->
    <context:component-scan base-package="com.spring.shiro.service"></context:component-scan>

8 applicationContext-tx.xml

    <!-- 掃描業(yè)務(wù)bean -->
    <context:component-scan base-package="com.spring.shiro.service"></context:component-scan>

9 springmvc.xml

    <!-- 處理器類的掃描 -->
    <context:component-scan
        base-package="com.spring.shiro.controller" />

    <!-- 配置注解的適配器和映射器盔沫,同時(shí)還注入了很多其他的bean -->
    <!-- 處理器適配器會(huì)去調(diào)用conversion-service -->
    <!--<mvc:annotation-driven conversion-service="conversionService" />-->
    
    <!-- 顯式的配置視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    

    
    <!-- 配置多部件解析器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 限制上傳文件的大小 單位是byte-->
        <property name="maxUploadSize" value="5000000"></property>
    </bean>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市枫匾,隨后出現(xiàn)的幾起案子架诞,更是在濱河造成了極大的恐慌,老刑警劉巖干茉,帶你破解...
    沈念sama閱讀 211,817評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件谴忧,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡等脂,警方通過查閱死者的電腦和手機(jī)俏蛮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,329評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來上遥,“玉大人搏屑,你說我怎么就攤上這事》鄢” “怎么了辣恋?”我有些...
    開封第一講書人閱讀 157,354評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)模软。 經(jīng)常有香客問我伟骨,道長(zhǎng),這世上最難降的妖魔是什么燃异? 我笑而不...
    開封第一講書人閱讀 56,498評(píng)論 1 284
  • 正文 為了忘掉前任携狭,我火速辦了婚禮,結(jié)果婚禮上回俐,老公的妹妹穿的比我還像新娘逛腿。我一直安慰自己,他們只是感情好仅颇,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,600評(píng)論 6 386
  • 文/花漫 我一把揭開白布单默。 她就那樣靜靜地躺著,像睡著了一般忘瓦。 火紅的嫁衣襯著肌膚如雪搁廓。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,829評(píng)論 1 290
  • 那天耕皮,我揣著相機(jī)與錄音境蜕,去河邊找鬼。 笑死凌停,一個(gè)胖子當(dāng)著我的面吹牛粱年,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播苦锨,決...
    沈念sama閱讀 38,979評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼逼泣,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼趴泌!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起拉庶,我...
    開封第一講書人閱讀 37,722評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤嗜憔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后氏仗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吉捶,經(jīng)...
    沈念sama閱讀 44,189評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,519評(píng)論 2 327
  • 正文 我和宋清朗相戀三年皆尔,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了呐舔。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,654評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡慷蠕,死狀恐怖珊拼,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情流炕,我是刑警寧澤澎现,帶...
    沈念sama閱讀 34,329評(píng)論 4 330
  • 正文 年R本政府宣布,位于F島的核電站每辟,受9級(jí)特大地震影響剑辫,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜渠欺,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,940評(píng)論 3 313
  • 文/蒙蒙 一妹蔽、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧挠将,春花似錦胳岂、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,762評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽闹击。三九已至镶蹋,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間赏半,已是汗流浹背贺归。 一陣腳步聲響...
    開封第一講書人閱讀 31,993評(píng)論 1 266
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留断箫,地道東北人剩膘。 一個(gè)月前我還...
    沈念sama閱讀 46,382評(píng)論 2 360
  • 正文 我出身青樓厢岂,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親垂寥。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,543評(píng)論 2 349

推薦閱讀更多精彩內(nèi)容

  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架巍棱,建立于...
    Hsinwong閱讀 22,358評(píng)論 1 92
  • spring官方文檔:http://docs.spring.io/spring/docs/current/spri...
    牛馬風(fēng)情閱讀 1,650評(píng)論 0 3
  • 原文鏈接:https://docs.spring.io/spring-boot/docs/1.4.x/refere...
    pseudo_niaonao閱讀 4,680評(píng)論 0 9
  • 名幻的大象消失了 森林自腳下開始卷起 進(jìn)水的腦子流出的虽另,是水 我的酒窩要盛滿 燈光亮起的時(shí)候, 人們起身散場(chǎng) 從不...
    文姬閱讀 207評(píng)論 0 1
  • 人饺谬,螻蟻般地在人潮中來來回回的閃動(dòng)著捂刺,固定的主體曲線,十年難變的根據(jù)地點(diǎn)募寨。 將就的活著族展,拼搏著過活;在一段時(shí)間內(nèi)爭(zhēng)...
    翱藍(lán)閱讀 110評(píng)論 0 0