Shiro整合SSH開發1:整合Shiro+Struts2+Spring+Hibernate 初步整合配置【基於Maven】

整合SSH(Struts2 + Spring + Hibernate):這個是基於配置文件在spring中配置Shiro的bean

前言:

     我看的視頻教程中使用的是SSM(Spring+SpringMVC+mybaties),但是我目前用的是SSH,特別是Struts2,在學習的時候特別不方便,後面在網絡上面查找對應的文章,大多都是SpringMVC的,關於SSH的寫的東西都不夠詳盡,以我的基礎還不能夠理解。因此我自己根據視頻教程中的配置流程一步步轉化爲關於SSH的配置。(雖然初步配置完成後發現和Controller層的關係不大,但是寫下這篇文章還是覺得挺有成就感的。)
     這是一個基礎的配置,直到基礎配置完成爲止,我並沒有進行一個詳細的認證和授權的流程,但是也希望通過這篇文章能幫助到大家。如果大家覺得有哪裏不正確的可以在下面的評論區提出,我會進行審查和更改,希望和各位一起進步!
老貓

此文老貓原創,轉載請加本文連接:http://blog.csdn.net/nthack5730/article/details/51002218
更多有關老貓的文章:http://blog.csdn.net/nthack5730



加入依賴

     首先要在Maven中加入Shiro對Spring和WEB的依賴。下面給出所有Shiro需要的依賴【SSH原本的依賴在這裏爲了節省篇幅就不貼上來了】,pom.xml:

Edit
<!-- 給出Shiro的版本 -->
<properties>
    <shiro.version>1.2.3</shiro.version>
</properties>

<!-- Shiro的依賴 -->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-core</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-web</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
</dependency>




     注意:和當使用Shiro的時候去除原工程的授權和攔截器。【用shiro就不要用其他的了,不然後期很難維護,規範很重要】



配置web.xml:

     配置Shiro的filter,因爲在web系統中,Shiro也是通過filter進行攔截。【Shiro會提供很多filter】
     下面的filter攔截後,會將操作權給我等等在下面貼出的applicationContext-shiro.xml(Spring中配置的filter,過濾鏈)
     在web.xml中加入Shiro的配置:
Edit
<!-- Shiro的filter -->
<!-- shiro過慮器,DelegatingFilterProx會從spring容器中找shiroFilter -->
<filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

    <!-- 設置true由servlet容器控制filter的生命週期 -->
    <init-param>
        <param-name>targetFilterLifecycle</param-name>
        <param-value>true</param-value>
    </init-param>

    <!-- 設置spring容器filter的beanID,如果不設置則默認自動找與filter name一致的bean,指定更加穩定快速 -->
    <init-param>
        <param-name>targetFilterLifecycle</param-name>
        <param-value>shiroFilter</param-value>
    </init-param>
</filter>

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




在Spring中配置filter:

     剛剛上面是在web.xml中配置filter的,現在要Spring中配置filter:applicationContext-shiro.xml【在下面會講講Shiro的內置過濾器】
     在applicationContext-shiro.xml中配置在web.xml中對應的spring容器中的bean,在這裏我用的是spring 4.0,所以xml文件對應的schema也是4.0版本的,需要配置的內容有:
Edit
<!-- web.xml中shiro的filter對應的bean -->
<!-- SecurityManager -->
<!-- Realm -->


     具體配置如下:【整合的時候最基礎的配置】
Edit
<?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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-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/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd"
>

    <!-- web.xml中shiro的filter對應的bean -->
    <!-- Shiro 的Web過濾器 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!-- 指定安全管理器 -->
        <property name="securityManager" ref="securityManager" />

        <!-- 如果沒有認證將要跳轉的登陸地址,http可訪問的url,如果不在表單認證過慮器FormAuthenticationFilter中指定此地址就爲身份認證地址 -->
        <!-- loginUrl認證提交地址,如果沒有通過認證將會請求此地址進行認證,請求地址醬油formAuthenticationFilter進行表單認證 -->
        <property name="loginUrl" value="/login.action" />

        <!-- 認證通過後,指定如果沒有權限跳轉的地址 -->
        <property name="unauthorizedUrl" value="/refuse.jsp" />

        <!-- shiro攔截器配置 -->
        <property name="filters">
            <map>
                <entry key="authc" value-ref="formAuthenticationFilter" />
            </map>
        </property>

        <!-- 真正的filter,也是過濾鏈,從上向下順序執行,一般都將/**放在最下邊 -->
        <property name="filterChainDefinitions">
            <value>
                <!-- 所有的URL都可以匿名訪問 -->
                /** = anon
            </value>
        </property>
    </bean>


    <!-- 安全管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!-- 注入realm -->
        <property name="realm" ref="userRealm" />
    </bean>


    <!-- 自定義 realm -->
    <bean id="userRealm" class="cn.marer.shiro.CustomRealm"></bean>
</beans>


此文老貓原創,轉載請加本文連接:http://blog.csdn.net/nthack5730/article/details/51002218
更多有關老貓的文章:http://blog.csdn.net/nthack5730

     記住要在web.xml中加入applicationContext-shiro.xml的讀取,否則上面的配置是無效的。
     【沒有加入這個配置文件的話會拋出:No Bean name "shiroFilter"...  異常】
Edit
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    <param-value>classpath:applicationContext-shiro.xml</param-value>
</context-param>

     也可以使用通配進行配置,不過爲了系統的穩定性,一般我都不採用這種方法,畢竟在後面使用的都是註解的方式配置Spring的bean,配置文件不多,就慢慢打上去吧。
Edit
<param-value>classpath:applicationContext-*.xml</param-value>




Shiro的內置過濾器:【概述】

anon(匿名)  org.apache.shiro.web.filter.authc.AnonymousFilter
authc(身份驗證)       org.apache.shiro.web.filter.authc.FormAuthenticationFilter
authcBasic(http基本驗證)    org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
logout(退出)        org.apache.shiro.web.filter.authc.LogoutFilter
noSessionCreation(不創建session) org.apache.shiro.web.filter.session.NoSessionCreationFilter
perms(許可驗證)  org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
port(端口驗證)   org.apache.shiro.web.filter.authz.PortFilter
rest  (rest方面)  org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
roles(權限驗證)  org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
ssl (ssl方面)   org.apache.shiro.web.filter.authz.SslFilter
user (用戶方面)  org.apache.shiro.web.filter.authc.UserFilter



 完成:

     好了,至此,基礎的初步配置已經完成,自己也在tomcat上面跑了一下,是可以正常運行和訪問index的。至於具體的權限認證和授權操作,我會在一步步學習中給大家總結和po出我的文章。希望大家繼續支持!

此文老貓原創,轉載請加本文連接:http://blog.csdn.net/nthack5730/article/details/51002218
更多有關老貓的文章:http://blog.csdn.net/nthack5730






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章