我可以在Quarkus静态地访问当前用户吗?

发布于 2025-01-22 13:45:16 字数 273 浏览 2 评论 0原文

我需要以静态方式访问当前用户(以前由containerRequestFilter),以避免在每种方法中的每种方法中@context SecurityContext控制器。

我想实现一些目标,在春季,

SecurityContextHolder.getContext().getAuthentication();

除了在Quarkus中使用Spring Security之外,我是否会做任何方法?

I need to do access to the current user (previously set up by a ContainerRequestFilter) in a static way, to avoid the passage of the @Context SecurityContext in every method of every controller.

I want to achieve something that, in Spring, I would do with

SecurityContextHolder.getContext().getAuthentication();

Is there any way to do it, besides using Spring Security in Quarkus?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁〃客ぐ 2025-01-29 13:45:16

解决方案是使用此依赖项

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-oidc</artifactId>
</dependency>

,然后您可以操纵securityidentity的实例“使其静态”

@Startup
@ApplicationScoped
public class UserUtilsService {

@Inject
private SecurityIdentity securityIdentity;

private static SecurityIdentity instance;

/**
 * Gets the custom user.
 *
 * @return the custom user
 */
public static CustomUser getCustomUser() {
    return (CustomUser) instance.getPrincipal();
}

@PostConstruct
private void setUp() {
    instance = this.securityIdentity;
}

}

@startup 在应用程序开始时实例化了BEAN(而不是懒惰) 。

然后,您可以使用userutilsservice.getCustomuser(); stally stat上访问主体。

The solution is to use this dependency

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-oidc</artifactId>
</dependency>

Then you can manipulate the instance of SecurityIdentity to "make it static"

@Startup
@ApplicationScoped
public class UserUtilsService {

@Inject
private SecurityIdentity securityIdentity;

private static SecurityIdentity instance;

/**
 * Gets the custom user.
 *
 * @return the custom user
 */
public static CustomUser getCustomUser() {
    return (CustomUser) instance.getPrincipal();
}

@PostConstruct
private void setUp() {
    instance = this.securityIdentity;
}

}

@StartUp does instantiate the bean on application start (instead of lazily).

You can then access to the Principal statically using UserUtilsService.getCustomUser();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文