Java micronaut的@AuthenticationPrincipal替代方案是什么?
我正在尝试获取如下所示的Java micronaut的@AuthenticationPrincipal替代方案是什么?,java,authentication,micronaut,Java,Authentication,Micronaut,我正在尝试获取如下所示的UserDetails对象。但是,我有一些困难,无法获取UserDetails对象,因此authentication.getAttributes()中只有JSONObject。micronaut中是否有其他方法获取UserDetails对象 自定义UserDetails对象: public class MyUserPrincipal implements UserDetails { private Account account; public MyUs
UserDetails
对象。但是,我有一些困难,无法获取UserDetails
对象,因此authentication.getAttributes()
中只有JSONObject
。micronaut中是否有其他方法获取UserDetails
对象
自定义UserDetails
对象:
public class MyUserPrincipal implements UserDetails {
private Account account;
public MyUserPrincipal(Account account) {
this.account = account;
}
public Account getAccount() {
return getAccount();
}
}
Rest 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;
}
@GET
public ResponseEntity<?> echo(@AuthenticationPrincipal MyUserPrincipal user) {
return new ResponseEntity<>(user.getAccount().getAccountId(), HttpStatus.OK);
}
例如,在Spring Security中,在参数中使用@AuthenticationPrincipal
注释很容易
Rest 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;
}
@GET
public ResponseEntity<?> echo(@AuthenticationPrincipal MyUserPrincipal user) {
return new ResponseEntity<>(user.getAccount().getAccountId(), HttpStatus.OK);
}
@GET
公共响应属性回显(@AuthenticationPrincipal MyUserPrincipal user){
返回新的响应属性(user.getAccount().getAccountId(),HttpStatus.OK);
}
用户详细信息在身份验证后不存在。唯一可用的对象是身份验证。如果您想标准化上面的代码,您可以创建一个bean来处理特定属性的注入
通过创建注释以及AnnotatedRequestStarGumentBinder
的实现,可以使用注释来指定注入。如下所示:
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;
}
}
公共类临时实现AnnotatedRequestStarGumentBinder{
@凌驾
公共类getAnnotationType(){
返回您的annotation.class;
}
@凌驾
公共BindingResult绑定(ArgumentConversionContext上下文,HttpRequest源){
if(source.getAttributes()包含(OncePerRequestHttpServerFilter.getKey(SecurityFilter.class))){
最终可选身份验证=source.getUserPrincipal(authentication.class);
if(authentication.isPresent()){
return()->(Long)((JSONObject)authentication.getAttributes().get(“account”).get(“id”);
}
}
返回ArgumentBinder.BindingResult.EMPTY;
}
}
用户详细信息在身份验证后不存在。唯一可用的对象是身份验证。如果您想标准化上面的代码,您可以创建一个bean来处理特定属性的注入
通过创建注释以及AnnotatedRequestStarGumentBinder
的实现,可以使用注释来指定注入。如下所示:
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;
}
}
公共类临时实现AnnotatedRequestStarGumentBinder{
@凌驾
公共类getAnnotationType(){
返回您的annotation.class;
}
@凌驾
公共BindingResult绑定(ArgumentConversionContext上下文,HttpRequest源){
if(source.getAttributes()包含(OncePerRequestHttpServerFilter.getKey(SecurityFilter.class))){
最终可选身份验证=source.getUserPrincipal(authentication.class);
if(authentication.isPresent()){
return()->(Long)((JSONObject)authentication.getAttributes().get(“account”).get(“id”);
}
}
返回ArgumentBinder.BindingResult.EMPTY;
}
}
如果您仍在寻找解决方案,以下是有效的方法。
您必须提供JwtAuthenticationFactory
的实现,并替换默认的DefaultJwtAuthenticationFactory
类似这样的代码(下面的代码在Kotlin中):
如果您使用的是Kotlin,use还可以在Authentication method上添加扩展方法,以获取添加到Authentication类的属性,例如:
fun Authentication.username():String=this.attributes[“username”]
注意:
username
只是一个例子。它作为身份验证实例上的name
实例变量提供。如果您仍在寻找解决方案,以下是有效的方法。
您必须提供JwtAuthenticationFactory
的实现,并替换默认的DefaultJwtAuthenticationFactory
类似这样的代码(下面的代码在Kotlin中):
如果您使用的是Kotlin,use还可以在Authentication method上添加扩展方法,以获取添加到Authentication类的属性,例如:
fun Authentication.username():String=this.attributes[“username”]
注意:
username
只是一个例子。它作为身份验证实例上的name
instance变量提供。“在micronaut中获取用户详细信息并不容易”-这不是一个很好的开始提问的方式,真正的问题是询问是否有一种在micronaut中获取用户详细信息的简单方法。谢谢,我已经纠正了您也没有显示出您所期望的等效内容。在spring示例“在micronaut中获取用户详细信息并不容易”中,您如何检索帐户id?这不是一个很好的开始提问的方式,真正的问题是,是否有一种在micronaut中获取用户详细信息的简单方法。谢谢,我已经更正了,您也没有显示出与您期望的相同的内容。在spring示例中如何检索帐户id它不好,这就是为什么,我们在AuthenticationProvider
中将UserDetails
发送给JWT(返回Flowable.just(new UserDetails((String)authenticationRequest.getIdentity(),new ArrayList())
),但无法获取。我不需要的只是帐户id
,我需要的整个UserDetails
对象。也许我必须注册这个活页夹@JamesKleeh我怎样才能注册这个活页夹?它不好,这就是为什么,我们在AuthenticationProvider
中向JWT发送UserDetails
(返回Flowable.just(new UserDetails((String)authenticationRequest.getIdentity(),new ArrayList())
),但无法获取。我不需要的只是accountId
,我需要的整个UserDetails
对象。也许我必须注册这个活页夹@JamesKleeh如何注册此活页夹?