Spring security 3.2 - @PostAuthorize表达式,用于根据方法的返回对象计算主体

时间:2014-08-20 12:58:08

标签: java spring spring-security spring-el spring-4

我使用spring security来保护和评估@PostAuthorize和@PreAuthorize注释中的表达式,以授权服务方法。我需要检查返回的对象是否具有已登录系统并且正在调用此方法的相同用户ID。

// this allows the ids of other users too
@PostAuthorize("returnObject!=null?returnObject.userId==principal.account.acid:true")
public AudioClip findAudioClip(int clipId) {
     .....
     AudioClip clip = dao.findById(clipId);
     // may also be null
     return clip;
}

AudioClip.java

@Component
public class AudioClip implements java.io.Serializable {

private java.math.BigDecimal id;
private java.lang.Integer    userId;
.....
}

数据库中提取的对象包含创建此db对象的userId。因此,只有他才有资格访问该组件。如何比较returnObject.userId和已登录系统的用户ID?

2 个答案:

答案 0 :(得分:2)

@holmis,正如你所指出的,罪魁祸首是代码中的#!这段代码

@PostAuthorize("returnObject!=null?returnObject.userId==principal.account.aid:true")

作品!

答案 1 :(得分:0)

同样的东西,但是我发现这比@vijay的答案读起来更好。

# -*- coding: utf-8 -*-
import cv2
import numpy as np
# input images
fig1 = cv2.imread("fig1.jpg", cv2.IMREAD_UNCHANGED)
# fig2 = cv2.imread("fig2.jpg", cv2.IMREAD_UNCHANGED)
# fig3 = cv2.imread("fig3.jpg", cv2.IMREAD_UNCHANGED)
# fig4 = cv2.imread("fig4.jpg", cv2.IMREAD_UNCHANGED)


def connected_components(img):
    # working with a copy of the image just for good practice
    im = img.copy()
    im = cv2.bitwise_not(im) # inverting image to have white as 0 = not objects
    # normalizing image to have a proper binary image
    im = cv2.normalize(im, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
    im = cv2.copyMakeBorder(im, 1, 1, 1, 1, cv2.BORDER_CONSTANT, 0) # add padding to work ouside borders
    labels = np.zeros((im.shape[0], im.shape[1]), dtype=int) # create label matrix with the same size of the image
    corr = [0] # create a  correlation array that already includes 0 as being 0
    # this array works like this: position corr[15] = 1 if the label 15 is equivalent to the label 1

    NextLabel = 1 # counts the label that will be placed on the next pixel with no labeled neighbors 
    
        
    # first pass - rastering through the image
    for row in range(1, im.shape[0]):
        for column in range(1, im.shape[1]):
            if im[row, column] > 0: # if the current pixel is not a backgroud pixel            
                if im[row, column - 1] == 0 and im[row - 1, column] == 0: # if both neighboring pixels are 0
                    labels[row, column] = NextLabel
                    corr.append(NextLabel)
                    NextLabel += 1
                elif im[row, column - 1] != 0 and im[row - 1, column] == 0: # if up pixel is 0 and left pixel is 1
                    labels[row, column] = labels[row, column - 1]
                elif im[row, column - 1] == 0 and im[row - 1, column] != 0: # if up pixel is 1 and left pixel is 0
                    labels[row, column] = labels[row - 1, column]
                elif im[row, column - 1] != 0 and im[row - 1, column] != 0: # if both are 1
                    labels[row, column] = min(labels[row, column - 1], labels[row - 1, column])
                    if corr[min(labels[row, column - 1], labels[row - 1, column])] != min(labels[row, column - 1], labels[row - 1, column]): # will explain later
                        corr[max(labels[row, column - 1], labels[row - 1, column])] = corr[min(labels[row, column - 1], labels[row - 1, column])]
                    else:
                        corr[max(labels[row, column - 1], labels[row - 1, column])] = min(labels[row, column - 1], labels[row - 1, column])
 

    #
    for s in range(2): # cheat for the correspondances to work
        for i in range(1, labels.shape[0]-1): # normal way to assign the correspondant value to the label
            for j in range(1, labels.shape[1]-1):
                if labels[i, j] != corr[labels[i, j]]:
                    labels[i, j] = corr[labels[i, j]]

    elementos = [] # list of elements to fix number of elements on the picture
    for i in range(labels.shape[0]):
        for j in range(labels.shape[1]):
            if labels[i, j] not in elementos:
                elementos.append(labels[i, j])

    for i in range(1, labels.shape[0]-1): # fix elements numbers
        for j in range(1, labels.shape[1]-1):
           labels[i, j]  = elementos.index(labels[i, j])
    
    return labels[1:labels.shape[0]-1, 1:labels.shape[1]-1] # returns original image without padding
# , im[1:labels.shape[0]-1, 1:labels.shape[1]-1]



a = connected_components(fig1)
# print(a)

请参阅6.5.14 The Elvis Operator