gpt4 book ai didi

java - Spring 为带有附加数据的主体自定义 AuthenticationProvider

转载 作者:太空宇宙 更新时间:2023-11-04 06:50:02 25 4
gpt4 key购买 nike

我对 Spring 还很陌生。

我的目标是拥有一些带有自定义authenticate方法的AuthenticationProvider,该方法将Authenticate对象作为参数,而不仅仅是用户名和密码。假设有一个领域名称(当用户位于不同的领域时,可以有 2 个具有相同用户名的用户)。我已经在寻找问题的答案,但有关身份验证问题的简单答案仅解释了如何扩展 AbstractUserDetailsAuthenticationProvider,这不能满足我的需求,因为检索方法仅接受用户名作为参数,而我还需要领域名称来检索用户。复杂的是在不解释上下文的情况下扩展或实现各种不同的 Spring 类和接口(interface)。

所以简单的问题是:

如何实现/扩展 AuthenticationProvider 才能从 Authentication 对象中读取自定义数据?

我的调用如下所示(是的,我想获得 OAuth2 token ):

curl -vX POST http://localhost:9001/oauth/token \
-d "client_id=myId&client_secret=secret&grant_type=password&username=myUser&password=1234&realm=realm3"

请注意末尾的 realm=realm3

当只有一个领域时,没有额外数据且使用我自己的 AbstractUserDetailsAuthenticationProvider 子类的调用已经可以工作。

提前致谢!

最佳答案

How do I have to implement / extend an AuthenticationProvider to be able to read custom data out of an Authentication object?

RealmAuthenticationProvider

public class RealmAuthenticationProvider implements AuthenticationProvider {

private RUPAuthenticator rupAuthenticator;

public RealmAuthenticationProvider(RUPAuthenticator rupAuthenticator) {
this.rupAuthenticator = rupAuthenticator;
}

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

Object principal = authentication.getPrincipal();
Object credentials = authentication.getCredentials();
Object realm = authentication.getDetails();
if (rupAuthenticator.authenticate(principal, credentials, realm)) {
List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER")); //use any GrantedAuthorities you need
return new RealmAuthenticationToken(principal, credentials, realm, grantedAuths);
};
return null;
}

@Override
public boolean supports(Class<?> authentication) {
return RealmAuthenticationToken.class.isAssignableFrom(authentication);
}
}

RealmAuthenticationToken

public class RealmAuthenticationToken extends UsernamePasswordAuthenticationToken {

private Object realm;

public RealmAuthenticationToken(Object principal, Object credentials, Object realm, Collection<? extends GrantedAuthority> authorities) {
super(principal,credentials, authorities);
this.realm = realm;
}
}

RUPAuthenticator

public interface RUPAuthenticator {
boolean authenticate(Object username, Object password, Object realm);
}

您只需提供 RUPAuthenticator 的实现来说明用户名、密码、领域组合是否正确。

然后将自定义的AuthenticationProvider(RealmAuthenticationProvider)注册为bean。下面是接受来自特定用户的请求的身份验证提供程序的示例:

@Bean
public AuthenticationManager authenticationManager() {
List<AuthenticationProvider> providers = new ArrayList<AuthenticationProvider>();
providers.add(new RealmAuthenticationProvider(new RUPAuthenticator() {
@Override
public boolean authenticate(Object username, Object password, Object realm) {
return (username.equals("sa") && password.equals("sa") && realm.equals("realm2"));
}
}));
return new ProviderManager(providers);
}

我希望这就是您正在寻找的内容。

关于java - Spring 为带有附加数据的主体自定义 AuthenticationProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23433373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com