第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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中,參數(shù)中的注釋很容易。休息API:@GETpublic ResponseEntity<?> echo(@AuthenticationPrincipal MyUserPrincipal user) {    return new ResponseEntity<>(user.getAccount().getAccountId(), HttpStatus.OK);}
查看完整描述

2 回答

?
牧羊人nacy

TA貢獻(xiàn)1862條經(jīng)驗 獲得超7個贊

如果您仍在尋找解決方案,這就是有效的方法。您必須提供一個實現(xiàn)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)

        }

    }

}

使用構(gòu)建器添加的所有聲明都將添加到Authentication對象中,并且可以在任何控制器中訪問,例如:


@Get("/hello-world")

fun hello(authentication: Authentication): String =

    authentication["username"] as String

如果您使用的是 Kotlin,還可以在 Authentication 方法上添加擴(kuò)展方法以獲取您添加到 Authentication 類的屬性,例如: fun Authentication.username(): String = this.attributes["username"]


注意:username只是一個例子。它可用作name身份驗證實例上的實例變量。


查看完整回答
反對 回復(fù) 2022-06-15
?
守著一只汪

TA貢獻(xiàn)1872條經(jīng)驗 獲得超4個贊

UserDetails 認(rèn)證后不存在。唯一可用的對象是身份驗證。如果您想標(biāo)準(zhǔn)化上面的代碼,您可以創(chuàng)建一個處理該特定屬性注入的 bean。


您可以使用注釋來指定注入,方法是創(chuàng)建注釋以及AnnotatedRequestArgumentBinder. 類似于以下內(nèi)容:


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;

    }

}


查看完整回答
反對 回復(fù) 2022-06-15
  • 2 回答
  • 0 關(guān)注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號