使用 Quarkus 剩余数据 Panache 如何从@PrePersist 中的请求 header 访问用户名
When using Quarkus rest data Panache how to access username from request header in @PrePersist
我正在尝试记录修改实体的用户。反向代理正在请求中设置用户名 header.
当使用rest Panache暴露JAX-RS CRUD
时,如何从一个独立的class读取请求和headers
下面是我尝试过的示例,但我得到的是空指针或空指针 exep
package org.acme.manrest;
import javax.enterprise.context.RequestScoped;
import javax.persistence.PrePersist;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import org.acme.jpa.TBusiness;
import org.jboss.logging.Logger;
@RequestScoped
public class HeaderValue {
private static final Logger LOG = Logger.getLogger(HeaderValue.class);
@Context
HttpServletRequest request;
public HttpServletRequest getRequest() {
return request;
}
@Context
HttpHeaders httpHeaders;
@HeaderParam("x-remote-user")
String userName;
public String getUserName() {
LOG.info("xxx: " + getRequest().getHeader("x-remote-user"));
LOG.info("userName: " + userName);
LOG.info("getUserName2: " + getUserName2());
LOG.info("getUserName3: " + getUserName3());
return userName;
}
public String getUserName2() {
return httpHeaders.getRequestHeader("x-remote-user").get(0);
}
public String getUserName3() {
LOG.info("getUserName from class " + request.getHeader("x-remote-user"));
return request.getHeader("x-remote-user");
}
@PrePersist
void onPrePersist(TBusiness myEntity) {
LOG.info("getUserName: " + userName);
myEntity.createdBy = userName;
myEntity.updatedBy = userName;
}
}
切换到 JWT 并在 Quarkus 中使用 JWT RBAC 可以解决吗?
通过添加 undertow 依赖项使其工作:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-undertow</artifactId>
</dependency>
然后在 EntityListener
class 下面创建 PrePersist
方法:
@RequestScoped
// @Path("api/v1/Header")
public class AuditingEntityListener {
private static final Logger LOG = Logger.getLogger(AuditingEntityListener.class);
// Inject the bean so that Quarkus does not remove it at build time (IMPORTANT)
@Inject
HttpServletRequest requestNotUsed;
@PrePersist
void onPrePersist(TBusiness myEntity) {
HttpServletRequest HSR = CDI.current().select(HttpServletRequest.class).get();
LOG.info("HSR getRequestHeader user: " + HSR.getHeader("x-remote-user"));
}
}
在 Quarkus 中使用 JWT RBAC 也有效:
@PrePersist
void onPrePersist(TBusiness myEntity) {
JsonWebToken context = CDI.current().select(JsonWebToken.class).get();
LOG.info("context: " + context.getClaim("preferred_username"));
}
我正在尝试记录修改实体的用户。反向代理正在请求中设置用户名 header.
当使用rest Panache暴露JAX-RS CRUD
时,如何从一个独立的class读取请求和headers下面是我尝试过的示例,但我得到的是空指针或空指针 exep
package org.acme.manrest;
import javax.enterprise.context.RequestScoped;
import javax.persistence.PrePersist;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import org.acme.jpa.TBusiness;
import org.jboss.logging.Logger;
@RequestScoped
public class HeaderValue {
private static final Logger LOG = Logger.getLogger(HeaderValue.class);
@Context
HttpServletRequest request;
public HttpServletRequest getRequest() {
return request;
}
@Context
HttpHeaders httpHeaders;
@HeaderParam("x-remote-user")
String userName;
public String getUserName() {
LOG.info("xxx: " + getRequest().getHeader("x-remote-user"));
LOG.info("userName: " + userName);
LOG.info("getUserName2: " + getUserName2());
LOG.info("getUserName3: " + getUserName3());
return userName;
}
public String getUserName2() {
return httpHeaders.getRequestHeader("x-remote-user").get(0);
}
public String getUserName3() {
LOG.info("getUserName from class " + request.getHeader("x-remote-user"));
return request.getHeader("x-remote-user");
}
@PrePersist
void onPrePersist(TBusiness myEntity) {
LOG.info("getUserName: " + userName);
myEntity.createdBy = userName;
myEntity.updatedBy = userName;
}
}
切换到 JWT 并在 Quarkus 中使用 JWT RBAC 可以解决吗?
通过添加 undertow 依赖项使其工作:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-undertow</artifactId>
</dependency>
然后在 EntityListener
class 下面创建 PrePersist
方法:
@RequestScoped
// @Path("api/v1/Header")
public class AuditingEntityListener {
private static final Logger LOG = Logger.getLogger(AuditingEntityListener.class);
// Inject the bean so that Quarkus does not remove it at build time (IMPORTANT)
@Inject
HttpServletRequest requestNotUsed;
@PrePersist
void onPrePersist(TBusiness myEntity) {
HttpServletRequest HSR = CDI.current().select(HttpServletRequest.class).get();
LOG.info("HSR getRequestHeader user: " + HSR.getHeader("x-remote-user"));
}
}
在 Quarkus 中使用 JWT RBAC 也有效:
@PrePersist
void onPrePersist(TBusiness myEntity) {
JsonWebToken context = CDI.current().select(JsonWebToken.class).get();
LOG.info("context: " + context.getClaim("preferred_username"));
}