Spring boot authenticationProvider通过ReST API调用从spring security对用户进行身份验证
我现在正在探索微服务中的用户身份验证。为此,我创建了身份验证服务-checkUserAuthentication。还提供微服务。这已经部署在云中。 现在,我正在创建具有特定业务逻辑的新服务。在该服务中,需要使用spring security中的authenticationProvider来验证和检查用户访问该端点的授权 为此,我正在阅读并探索以下教程 在这里,它们是CustomAuthenticationProvider类中的AuthenticationProvider实现 他们接收用户名和密码的方法如下:Spring boot authenticationProvider通过ReST API调用从spring security对用户进行身份验证,spring-boot,spring-security,Spring Boot,Spring Security,我现在正在探索微服务中的用户身份验证。为此,我创建了身份验证服务-checkUserAuthentication。还提供微服务。这已经部署在云中。 现在,我正在创建具有特定业务逻辑的新服务。在该服务中,需要使用spring security中的authenticationProvider来验证和检查用户访问该端点的授权 为此,我正在阅读并探索以下教程 在这里,它们是CustomAuthenticationProvider类中的AuthenticationProvider实现 他们接收用户
public Authentication authenticate(Authentication authentication) throws
AuthenticationException {
String name = authentication.getName();
String password = authentication.getCredentials().toString();
Optional<User> optionalUser = users.stream().filter(u -> u.index(name,
password)).findFirst();
if (!optionalUser.isPresent()) {
logger.error("Authentication failed for user = " + name);
throw new BadCredentialsException("Authentication failed for user = " + name);
}
// find out the exited users
List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
grantedAuthorities.add(new SimpleGrantedAuthority(optionalUser.get().role));
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(name, password,
grantedAuthorities);
logger.info("Succesful Authentication with user = " + name);
return auth;
}
公共身份验证(身份验证)抛出
身份验证异常{
String name=authentication.getName();
字符串密码=authentication.getCredentials().toString();
可选选项user=users.stream().filter(u->u.index(名称、,
密码().findFirst();
如果(!optionalUser.isPresent()){
logger.error(“user=“+name”的身份验证失败);
抛出新的BadCredentialsException(“user=“+name”的身份验证失败);
}
//查找已退出的用户
List GrantedAuthories=new ArrayList();
添加(新的SimpleGrantedAuthority(optionalUser.get().role));
UsernamePasswordAuthenticationToken auth=新的UsernamePasswordAuthenticationToken(名称、密码、,
授权机构);
logger.info(“使用user=“+name”成功验证);
返回auth;
}
这些是文档中的代码。我需要用不同的方法来代替这种方法。在这里,我添加了我的要求:
我的要求:我需要从API请求接收用户名和密码。要检查此用户名和密码,我需要调用部署的API checkUserAuthentication和checkUserAuthorization
我对此表示怀疑:
boolean isAuthenticated()
方法,以确定用户是否可以访问所需的资源
现在,当您得到所有答案后,我的建议是使用Oauth2进行引导微服务。有很多例子可以说明如何实现它并根据您的需求定制它。(基本上,你必须实现你的授权服务器,它将使用你的服务checkUserAuthentication对用户进行身份验证,并生成accesstoken。你的微服务的每个消费者都需要发送他们从授权服务器获得的accesstoken,你需要在你的微服务中验证它。所以我们的微服务将充当资源服务器)
希望这会有帮助