【发布时间】:2019-02-26 11:12:30
【问题描述】:
我正在使用 spring-boot-1.5.10,并且在我的应用程序中使用 spring-security。我想创建一个自定义注释,并且应该使用 securityContext holder...让我用示例代码详细说明我的问题。
curl -X GET -H "roles: READ" -H "Content-Type: application/json" -H “接受:应用程序/json” -H “应用程序名称:样本” -H “应用程序 ID:样本” -H "customer-id: 123" -H "market: EN" -H "country-code: EN" -H "Accept-Language: application/json" -H "Cache-Control: no-cache" "http://localhost:9992/api/v1/apps"
控制器
@GetMapping("/apps")
@PreAuthorize("hasAnyAuthority('ROLE_READ', 'ROLE_WRITE')")
public ResponseEntity<List<Apps>> getApps(@AuthenticationPrincipal AppAuthentication appAuthentication) {
if(appAuthentication.isGrantedAnyOf("ROLE_READ") && isBlank(appAuthentication.getAppContext().customerId())) {
throw new IllegalArgumentException("Missing header customerId");
}
if(appAuthentication.isGrantedAnyOf("ROLE_WRITE") && isBlank(appAuthentication.getAppContext().customerId()) && isBlank(appAuthentication.getAppContext().appId())) {
throw new IllegalArgumentException("Missing header customerId & AppId");
}
//write business logic here
}
spring-security preAuthorize 只会检查角色是否被允许。此外,我可以增强 preAuthorize 注释,但它对于许多微服务来说很常见,而且我无权触及安全领域。所以,我想创建一个自定义注释。我们应该配置角色和标头以验证特定角色。如下所示
@GetMapping("/apps")
@PreAuthorize("hasAnyAuthority('ROLE_READ', 'ROLE_WRITE')")
@ValidateHeaders("role=ROLE_READ",value={"customerId","app-id"})
public ResponseEntity<List<Apps>> getApps(@AuthenticationPrincipal AppAuthentication appAuthentication) {
//write business logic here
}
任何提示都会非常明显。
【问题讨论】:
-
希望这会有所帮助:baeldung.com/…
-
听起来您正在寻找的是 Spring Security ACL。
-
@MebinJoe 感谢您的快速链接。这解释了有关 preAuthorize 的更多信息。正如我在问题中提到的,我无权增强 preAuthorize 注释。是否可以使用自定义注释?
-
@VelNaga 您在寻找自定义过滤器或自定义注释吗?
-
enhance the preAuthorize annotation 是什么意思? if 表达式可以很容易地写为表达式的一部分,这就是它的用途。因此,与其试图在原地硬塞一些额外的/新的东西,不如扩展表达式,因为这是唯一正确的方法。
标签: java spring spring-boot spring-mvc spring-security