gpt4 book ai didi

java - 自定义权限评估器 Spring

转载 作者:行者123 更新时间:2023-12-04 20:13:16 29 4
gpt4 key购买 nike

我想创建一个自定义权限评估器,以便使用自定义方法@PreAuthorize REST 端点。
我将 Spring Boot 1.5.3 与 Web 和安全启动器一起使用。

我的进一步用例是检查登录用户是否有权查看指定的 ID。

调用 REST 端点后,我收到以下错误:

org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method hasPermission(null) cannot be found on org.springframework.security.access.expression.method.MethodSecurityExpressionRoot type

我的自定义权限评估器:
@Component
class CustomPermissionsEvaluator implements PermissionEvaluator {

public boolean hasPermission(String id) {
return id.equals("correct");
}

@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
return false;
}

@Override
public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {
return false;
}
}

我的安全配置:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
class SecurityConfig extends GlobalMethodSecurityConfiguration {

@Override
public MethodSecurityExpressionHandler createExpressionHandler() {
DefaultMethodSecurityExpressionHandler methodSecurityExpressionHandler = new DefaultMethodSecurityExpressionHandler();
methodSecurityExpressionHandler.setPermissionEvaluator(new CompanyPermissionsEvaluator());
return methodSecurityExpressionHandler;
}
}

我的休息 Controller :
@RestController
class RestControllerToProtect {

@PreAuthorize("hasPermission(#id)")
@GetMapping
public String methodToProtect(String id) {
return "Authenticated";
}
}

堆栈跟踪:
org.springframework.expression.spel.SpelEvaluationException: EL1004E:
Method call: Method hasPermission(null) cannot be found on
org.springframework.security.access.expression.method.MethodSecurityExpressionRoot type

最佳答案

您不能使用不是 PermissionEvaluator 成员的重载方法无需额外配置(如果您想重新配置 PermissionEvaluator 模式,请参阅 this 答案)。
hasPermission默认情况下,调用应匹配以下签名之一:

hasPermission(Authentication authentication, Object targetDomainObject, Object permission);

hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission);

例子:
public class CustomPermissionEvaluator implements PermissionEvaluator {

private Logger log = LoggerFactory.getLogger(CustomPermissionEvaluator.class);

@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
CustomUserDetails customUserDetails = (CustomUserDetails) authentication.getPrincipal();
AbstractEntity abstractEntity = (AbstractEntity) targetDomainObject;
log.debug("User {} trying to access {}-{} with permission {}",
customUserDetails.getUsername(),
abstractEntity.getClass().getSimpleName(),
abstractEntity.getId(),
permission.toString());
return false;
}

@Override
public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {
CustomUserDetails customUserDetails = (CustomUserDetails) authentication.getPrincipal();
log.debug("User {} trying to access {}-{} with permission {}",
customUserDetails.getUsername(),
targetType,
targetId,
permission.toString());
return false;
}
}

Controller :
@RestController
public class RestControllerToProtect {
// passing targetDomainObject and permission, authentication is detected by SecurityExpressionRoot
@PreAuthorize("hasPermission(#abstractEntity, 'create')")
public String methodToProtect(@RequestBody AbstractEntity abstractEntity) {
return "Authenticated";
}
}

关于java - 自定义权限评估器 Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44209924/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com