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

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

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

1. 前言

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

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

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

圖片描述

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

2. AOP 安全攔截器

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

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

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

這其中主要涉及了以下一些類:

方法的安全性通過 MethodSecurityInterceptor 實(shí)現(xiàn),用來保護(hù)方法執(zhí)行過程的安全。攔截器可以配置為指定單一的 Bean 對象或者在多個 Bean 對象之間共享,它使用 MethodSecurityMetadataSource 實(shí)例里維護(hù)配置屬性,用來應(yīng)用到一個具體的方法調(diào)用上。MapBasedMethodSecurityMetadataSource 實(shí)例用于儲存那些以方法名為主鍵的配置屬性,并且當(dāng)屬性在應(yīng)用上下文中被用于 <intercept-methods><protect-point> 元素時會在內(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)盟安全攔截器類似,但仍有一些不同。

AspectJ 安全攔截器的應(yīng)用類名為 AspectJSecurityInterceptor。不同于 AOP 聯(lián)盟安全攔截器,它不是基于 Spring 應(yīng)用上下文來激活攔截器,它通過 AspectJ 編譯器實(shí)現(xiàn)。多數(shù)情況下,同一應(yīng)用會出現(xiàn)這兩種安全攔截器,AspectJ 用于域?qū)ο蟮陌踩刂疲珹OP 聯(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>

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

下一步,我們需要定義 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)用每一個 PersistableEntity 實(shí)例。AspectJCallback 被用于執(zhí)行 proceed() ,該調(diào)用只有在 around() 方法中才能得以執(zhí)行,當(dāng)我們需要目標(biāo)對象繼續(xù)執(zhí)行時,這些匿名的回調(diào)函數(shù)會被調(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 對象了,他們都將被安全攔截器覆蓋。

4. 小結(jié)

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

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

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