为了账号安全,请及时绑定邮箱和手机立即绑定

micronaut 的 @AuthenticationPrincipal 替代方案是什么?

micronaut 的 @AuthenticationPrincipal 替代方案是什么?

哈士奇WWW 2022-06-15 16:31:51
我试图得到UserDetails像下面这样的对象。但是,我有一些困难,不可能得到对象,UserDetails所以只有JSONObject. authentication.getAttributes()在 micronaut 中有没有其他方法可以获取UserDetails对象?自定义UserDetails对象:public class MyUserPrincipal implements UserDetails {    private Account account;    public MyUserPrincipal(Account account) {        this.account = account;    }    public Account getAccount() {        return getAccount();    }}休息API://micronaut@Post(value = "/echo")@Status(HttpStatus.OK)public Long echo(@Nullable Authentication authentication) {    Long accountId = (Long)((JSONObject)authentication.getAttributes().get("account")).get("id");    return accountId;}例如,在 Spring Security@AuthenticationPrincipal中,参数中的注释很容易。休息API:@GETpublic ResponseEntity<?> echo(@AuthenticationPrincipal MyUserPrincipal user) {    return new ResponseEntity<>(user.getAccount().getAccountId(), HttpStatus.OK);}
查看完整描述

2 回答

?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

如果您仍在寻找解决方案,这就是有效的方法。您必须提供一个实现JwtAuthenticationFactory并替换 default DefaultJwtAuthenticationFactory。


像这样的东西(下面的代码在 Kotlin 中):


@Singleton

@Replaces(bean = DefaultJwtAuthenticationFactory::class)

class CustomJwtAuthenticationFactory() : JwtAuthenticationFactory {

    override fun createAuthentication(token: JWT?): Optional<Authentication> {

        try {

            val builder = JWTClaimsSet.Builder()

            builder.claim("username", token?.jwtClaimsSet?.getStringClaim("username"))


            return Optional.of(AuthenticationJWTClaimsSetAdapter(jwtClaims))

        } catch (e: Exception) {

            throw RuntimeException("ParseException creating authentication", e)

        }

    }

}

使用构建器添加的所有声明都将添加到Authentication对象中,并且可以在任何控制器中访问,例如:


@Get("/hello-world")

fun hello(authentication: Authentication): String =

    authentication["username"] as String

如果您使用的是 Kotlin,还可以在 Authentication 方法上添加扩展方法以获取您添加到 Authentication 类的属性,例如: fun Authentication.username(): String = this.attributes["username"]


注意:username只是一个例子。它可用作name身份验证实例上的实例变量。


查看完整回答
反对 回复 2022-06-15
?
守着一只汪

TA贡献1872条经验 获得超4个赞

UserDetails 认证后不存在。唯一可用的对象是身份验证。如果您想标准化上面的代码,您可以创建一个处理该特定属性注入的 bean。


您可以使用注释来指定注入,方法是创建注释以及AnnotatedRequestArgumentBinder. 类似于以下内容:


public class Temp implements AnnotatedRequestArgumentBinder<YourAnnotation, Long> {


    @Override

    public Class<YourAnnotation> getAnnotationType() {

        return YourAnnotation.class;

    }


    @Override

    public BindingResult<Long> bind(ArgumentConversionContext<Long> context, HttpRequest<?> source) {

        if (source.getAttributes().contains(OncePerRequestHttpServerFilter.getKey(SecurityFilter.class))) {

            final Optional<Authentication> authentication = source.getUserPrincipal(Authentication.class);

            if (authentication.isPresent()) {

                return () -> (Long)((JSONObject)authentication.getAttributes().get("account")).get("id");

            }

        }


        return ArgumentBinder.BindingResult.EMPTY;

    }

}


查看完整回答
反对 回复 2022-06-15
  • 2 回答
  • 0 关注
  • 163 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号