我现在正在探索微服务中用户的身份验证。为此,我创建了身份验证服务- checkUserAuthentication。同时也提供微型服务。这已经部署在云中了。现在,我正在创建具有特定业务逻辑的新服务。在此服务中,需要通过使用spring安全性中的authenticationProvider来验证和检查用户访问此端点的授权。
为此,我阅读并探讨了以下教程,
在这里,它们是在类AuthenticationProvider中实现的CustomAuthenticationProvider。
在方法上,他们接收用户名和密码如下,
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;
}
这些是文件中的代码。而不是这种方法,我需要用不同的方式来做。在这里,我要添加我的要求:
我的要求:我需要从API Request.And接收用户名和密码来检查这个用户名和密码,我需要调用已部署的API checkUserAuthentication和checkUserAuthorization。
我对此的怀疑:
因为我只从Security开始,所以我对安全世界很陌生。
发布于 2018-04-03 15:51:57
是。
与他们在身份验证方法中所做的相同。
UsernamePasswordAuthenticationToken在内部由spring安全性使用。当您在春季创建一个会话时,这会出现在图片中。它包含用户信息(例如。电子邮件等)和权威(角色).For示例,当您在应用程序中接收到JWT令牌时,您将验证JWT令牌(签名等)。在成功验证JWT之后,您将创建一个UsernamePasswordAuthenticationToken对象,spring将在会话中保存它。对于每个传入的请求,spring将调用该对象上的boolean isAuthenticated()
方法,以查找用户是否可以访问所需的资源。
现在,当您已经得到所有答案时,我建议您使用Oauth2进行引导微服务。有很多的例子,如何实现它和定制它为您的需求。(基本上,您必须实现您的授权服务器,它将使用您的服务checkUserAuthentication对用户进行身份验证,并生成访问权限。您的微服务的每个使用者都需要发送从授权服务器获得的这个访问权限,并且您需要在您的微服务中验证它。因此,您的微服务将充当资源服务器)。
希望能帮上忙。
https://stackoverflow.com/questions/49633064
复制相似问题