天天看點

Spring Security的xml詳細配置

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans

    xmlns="http://www.springframework.org/schema/security"

    xmlns:beans="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

    xsi:schemaLocation="http://www.springframework.org/schema/beans 

                                                http://www.springframework.org/schema/beans/spring-beans.xsd

                                                http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

                                                http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

    <!-- 不需要認證的頁面,和靜态資源不被攔截  -->  <!---->

    <http pattern="/*.html" security="none"></http>

    <http pattern="/css/**" security="none"></http>

    <http pattern="/img/**" security="none"></http>

    <http pattern="/js/**" security="none"></http>

    <http pattern="/plugins/**" security="none"></http>

    <!-- 要先增加使用者,不然沒使用者登入,放開添加使用者的方法不被攔截 -->

    <http pattern="/seller/add.do" security="none"></http>

    <!-- 頁面的攔截規則 use-expressions:是否啟動SPEL表達式 預設是true -->

    <http use-expressions="false">

     <!-- 目前使用者必須有ROLE_開頭, ROLE_USER的角色 才可以通路根目錄及所屬子目錄的資源 -->

      <intercept-url pattern="/**" access="ROLE_SELLER" />

<!-- 開啟表單登陸功能 -->

        <form-login login-page="/login.html"

            default-target-url="/admin/index.html"

            authentication-failure-url="/login.html"

            always-use-default-target="true" />

        <!--關閉跨域請求限制 -->

        <csrf disabled="true" />

        <!-- Spring Security下,X-Frame-Options預設為DENY,非Spring Security環境下,X-Frame-Options的預設大多也是DENY,這種情況下,浏覽器拒絕目前頁面加載任何Frame頁面,設定含義如下: 

                DENY:浏覽器拒絕目前頁面加載任何Frame頁面     SAMEORIGIN:frame頁面的位址隻能為同源域名下的頁面     ALLOW-FROM:origin為允許frame加載的頁面位址。 -->

        <headers>

            <frame-options policy="SAMEORIGIN" />

        </headers>

        <!--登出 不配置跳到頁面,就預設跳到登入頁面 -->

        <logout />

        <!-- logout-success-url 指定成功登出後要重定向的URL。 通過logout元素的logout-url屬性來改變登出的預設位址。 -->

        <!-- <logout logout-url="/login.html" logout-success-url="/login.html"/> -->

    </http>

    <!-- 配置授權資訊認證管理 -->

    <authentication-manager>

        <!--使用自定義認證服務-->  <!-- user-service-ref="userDetailService"自己指定的認證類 -->

        <authentication-provider user-service-ref="userDetailService">

        <!--把密碼的加密的類走到認證的提供者裡面  -->  <!--指定加密算法-->

        <password-encoder ref="bcryptEncoder"></password-encoder>

        </authentication-provider>

    </authentication-manager>

    <!-- 認證類 -->

    <beans:bean id="userDetailService"

        class="com.qingfeng.service.UserDetailsServiceImpl">

        <!-- 在 認證類裡通過set注入,注入了 

                private SellerService sellerService;

                public void setSellerService(SellerService sellerService) {

                    this.sellerService = sellerService;

                }-->

        <beans:property name="sellerService" ref="sellerService"></beans:property>

    </beans:bean>

    <!-- 引用dubbo 服務 -->

    <dubbo:application name="qingfeng-shop-web" />

    <dubbo:registry address="zookeeper://192.168.25.135:2181" />

    <!--dubbo引用接口    相當是一個bean-->

    <dubbo:reference id="sellerService" inter></dubbo:reference>

    <!--這是密碼加密的類  -->

    <beans:bean id="bcryptEncoder"

        class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />

</beans:beans>

繼續閱讀