如何通过参数注解实现Quarkus拦截器

问题描述 投票:0回答:1

这是我的代码,我想编写一个可以接收一些角色(数组)的拦截器,但我在接受参数时遇到困难

import jakarta.annotation.Priority
import jakarta.interceptor.AroundInvoke
import jakarta.interceptor.Interceptor
import jakarta.interceptor.InterceptorBinding
import jakarta.interceptor.InvocationContext
import jakarta.ws.rs.core.HttpHeaders
import java.lang.annotation.Inherited


@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@InterceptorBinding
@Inherited
annotation class RolesRequired( val rolesRequired: Array<String>)

@Interceptor
@RolesRequired(["role1", "role2"])
@Priority(Interceptor.Priority.PLATFORM_BEFORE + 1)
class RolesRequiredInterceptor(
    private val header: HttpHeaders
) {
    @AroundInvoke
    fun intercept(context: InvocationContext): Any? {
        println("[check-role]")
        val rolesRequired = this.javaClass.getAnnotation(
            RolesRequired::class.java
        ).rolesRequired
        println("[roles]: ${rolesRequired.size}")
//        TODO check roles
        return context.proceed()
    }
}

让我困惑的是

@RolesRequired(["role1", "role2"])
行,如果我写了这一行,那么这个注释
@RolesRequired
将不起作用,除非它的参数是
["role1", "role2"]
,似乎参数
rolesRequired
的值定义在
RolesRequiredInterceptor
是唯一可以让此注释工作的值。
我尝试从类中删除 @RolesRequired(["role1", "role2"]) 

RolesRequired

但 IDE 告诉“@Interceptor 必须指定至少一个拦截器绑定”。定义类

RolesRequiredInterceptor
时,参数
rolesRequired
似乎是必需的。
在以下情况下,拦截器 

RolesRequiredInterceptor

似乎未执行,当我调用

@RolesRequired
 时,quarkus 会打印并指出这一点
login()

仅在这种情况下,夸库斯打印

@RolesRequired(rolesRequired = ["role1"]) fun login(input: LoginData): LoginResponse {...} @RolesRequired(rolesRequired = ["role2"]) fun login(input: LoginData): LoginResponse {...} @RolesRequired(rolesRequired = ["role2", "role1"]) fun login(input: LoginData): LoginResponse {...}

[check-role] 
[roles]: 2

kotlin parameters annotations quarkus interceptor
1个回答
0
投票
试试这个告诉我是否有效

© www.soinside.com 2019 - 2024. All rights reserved.