第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

首頁(yè) 慕課教程 Spring Security Spring Security 安全對(duì)象實(shí)現(xiàn)

Spring Security 安全對(duì)象實(shí)現(xiàn)

1. 前言

上節(jié)我們討論了如何利用表達(dá)式簡(jiǎn)化權(quán)限規(guī)則的配置,本節(jié)將討論對(duì)象級(jí)的權(quán)限管理。

安全對(duì)象(Security Object)指任何可以有安全限制的對(duì)象,常見(jiàn)的比如方法調(diào)用,或者 Web 請(qǐng)求等。

每一個(gè)安全對(duì)象都有自己的一個(gè)攔截器實(shí)現(xiàn)。

圖片描述

本節(jié),我們將討論安全對(duì)象的鑒權(quán)如何實(shí)現(xiàn)。

2. AOP 安全攔截器

在 Spring Security 2.0 之前,想要對(duì)「安全對(duì)象」進(jìn)行保護(hù),需要使用大量的「樣板模式」來(lái)實(shí)現(xiàn)。

所謂樣板模式,就是在父類(lèi)(抽象類(lèi))中公開(kāi)定義某對(duì)象的執(zhí)行方法,該方法可以被其子類(lèi)重寫(xiě),但是該方法的調(diào)用需要在父類(lèi)的方法中完成,也就是由父類(lèi)決定行為,由子類(lèi)決定過(guò)程細(xì)節(jié),封裝不變的部分,擴(kuò)展可變的部分。

在新版本的 Spring Security 中,采用了「基于命名空間配置」的方式實(shí)現(xiàn)安全保護(hù),通過(guò)這種方式,Bean 對(duì)象可以自動(dòng)配置出安全策略,而不必過(guò)多關(guān)注細(xì)節(jié)。

這其中主要涉及了以下一些類(lèi):

方法的安全性通過(guò) MethodSecurityInterceptor 實(shí)現(xiàn),用來(lái)保護(hù)方法執(zhí)行過(guò)程的安全。攔截器可以配置為指定單一的 Bean 對(duì)象或者在多個(gè) Bean 對(duì)象之間共享,它使用 MethodSecurityMetadataSource 實(shí)例里維護(hù)配置屬性,用來(lái)應(yīng)用到一個(gè)具體的方法調(diào)用上。MapBasedMethodSecurityMetadataSource 實(shí)例用于儲(chǔ)存那些以方法名為主鍵的配置屬性,并且當(dāng)屬性在應(yīng)用上下文中被用于 <intercept-methods><protect-point> 元素時(shí)會(huì)在內(nèi)部直接調(diào)用這些屬性。其他實(shí)現(xiàn)則基于注釋的配置。

我們可以在應(yīng)用上下文中顯示的配置 Spring AOP 攔截器。

例如:

<bean id="bankManagerSecurity" class=
    "org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="accessDecisionManager" ref="accessDecisionManager"/>
<property name="afterInvocationManager" ref="afterInvocationManager"/>
<property name="securityMetadataSource">
    <sec:method-security-metadata-source>
    <sec:protect method="com.mycompany.BankManager.delete*" access="ROLE_SUPERVISOR"/>
    <sec:protect method="com.mycompany.BankManager.getBalance" access="ROLE_TELLER,ROLE_SUPERVISOR"/>
    </sec:method-security-metadata-source>
</property>
</bean>

3. AspectJ 安全攔截器

AspectJ 安全攔截器和 AOP 聯(lián)盟安全攔截器類(lèi)似,但仍有一些不同。

AspectJ 安全攔截器的應(yīng)用類(lèi)名為 AspectJSecurityInterceptor。不同于 AOP 聯(lián)盟安全攔截器,它不是基于 Spring 應(yīng)用上下文來(lái)激活攔截器,它通過(guò) AspectJ 編譯器實(shí)現(xiàn)。多數(shù)情況下,同一應(yīng)用會(huì)出現(xiàn)這兩種安全攔截器,AspectJ 用于域?qū)ο蟮陌踩刂?,AOP 聯(lián)盟安全攔截器用于服務(wù)層的安全。

AspectJSecurityInterceptor 的配置方式如下:

<bean id="bankManagerSecurity" class=
    "org.springframework.security.access.intercept.aspectj.AspectJMethodSecurityInterceptor">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="accessDecisionManager" ref="accessDecisionManager"/>
<property name="afterInvocationManager" ref="afterInvocationManager"/>
<property name="securityMetadataSource">
    <sec:method-security-metadata-source>
    <sec:protect method="com.mycompany.BankManager.delete*" access="ROLE_SUPERVISOR"/>
    <sec:protect method="com.mycompany.BankManager.getBalance" access="ROLE_TELLER,ROLE_SUPERVISOR"/>
    </sec:method-security-metadata-source>
</property>
</bean>

可見(jiàn),除了類(lèi)名之外,AspectJ 方式與 AOP 聯(lián)盟方式配置幾乎一樣。不僅如此,這兩個(gè)攔截器可以共用 securityMetadataSource 對(duì)象。

下一步,我們需要定義 AspectJ 的 aspect,例如:

package org.springframework.security.samples.aspectj;

import org.springframework.security.access.intercept.aspectj.AspectJSecurityInterceptor;
import org.springframework.security.access.intercept.aspectj.AspectJCallback;
import org.springframework.beans.factory.InitializingBean;

public aspect DomainObjectInstanceSecurityAspect implements InitializingBean {

    private AspectJSecurityInterceptor securityInterceptor;

    pointcut domainObjectInstanceExecution(): target(PersistableEntity)
        && execution(public * *(..)) && !within(DomainObjectInstanceSecurityAspect);

    Object around(): domainObjectInstanceExecution() {
        if (this.securityInterceptor == null) {
            return proceed();
        }

        AspectJCallback callback = new AspectJCallback() {
            public Object proceedWithObject() {
                return proceed();
            }
        };

        return this.securityInterceptor.invoke(thisJoinPoint, callback);
    }

    public AspectJSecurityInterceptor getSecurityInterceptor() {
        return securityInterceptor;
    }

    public void setSecurityInterceptor(AspectJSecurityInterceptor securityInterceptor) {
        this.securityInterceptor = securityInterceptor;
    }

    public void afterPropertiesSet() throws Exception {
        if (this.securityInterceptor == null)
            throw new IllegalArgumentException("securityInterceptor required");
        }
    }
}

這段代碼中,安全攔截器被應(yīng)用每一個(gè) PersistableEntity 實(shí)例。AspectJCallback 被用于執(zhí)行 proceed() ,該調(diào)用只有在 around() 方法中才能得以執(zhí)行,當(dāng)我們需要目標(biāo)對(duì)象繼續(xù)執(zhí)行時(shí),這些匿名的回調(diào)函數(shù)會(huì)被調(diào)用。

下一步,我們需要配置 Spring 加載 aspect 并關(guān)聯(lián)到 AspectJSecurityInterceptor 攔截器中,如下:

<bean id="domainObjectInstanceSecurityAspect"
    class="security.samples.aspectj.DomainObjectInstanceSecurityAspect"
    factory-method="aspectOf">
<property name="securityInterceptor" ref="bankManagerSecurity"/>
</bean>

到這里為止,我們可以隨意的創(chuàng)建自己的 bean 對(duì)象了,他們都將被安全攔截器覆蓋。

4. 小結(jié)

本節(jié)討論了對(duì)象級(jí)的安全配置策略,主要內(nèi)容有:

  • Spring Security 中的安全對(duì)象指所有需要被安全限制的對(duì)象;
  • Spring Security 通過(guò)配置攔截器的方式保護(hù)安全對(duì)象不受非法訪問(wèn);
  • Spring Security 的安全對(duì)象攔截器分為 AOP 方式和 AspectJ 方式,兩種方式的執(zhí)行時(shí)期不同,前置在程序運(yùn)行過(guò)程中動(dòng)態(tài)啟用,后者在編譯時(shí)靜態(tài)啟用;
  • 兩種攔截器并不影響,通??梢砸黄鹗褂?。

下節(jié)我們討論「域?qū)ο蟆沟臋?quán)限配置。