Spring boot authenticationProvider通过ReST API调用从spring security对用户进行身份验证

Spring boot authenticationProvider通过ReST API调用从spring security对用户进行身份验证,spring-boot,spring-security,Spring Boot,Spring Security,我现在正在探索微服务中的用户身份验证。为此,我创建了身份验证服务-checkUserAuthentication。还提供微服务。这已经部署在云中。 现在,我正在创建具有特定业务逻辑的新服务。在该服务中,需要使用spring security中的authenticationProvider来验证和检查用户访问该端点的授权 为此,我正在阅读并探索以下教程 在这里,它们是CustomAuthenticationProvider类中的AuthenticationProvider实现 他们接收用户

我现在正在探索微服务中的用户身份验证。为此,我创建了身份验证服务-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

    我对此表示怀疑:

  • 我可以在“公共身份验证(Authentication)”方法中直接调用这些API吗
  • 我如何从收到的请求中接收用户名和密码
  • 为什么要使用用户名密码身份验证令牌,如果我们发送的是JWT令牌而不是用户名和密码,那么哪个类将用于提供回复
  • 因为我刚开始使用SpringSecurity,所以我对安全世界还很陌生

  • 我可以在“公共身份验证(Authentication)”方法中直接调用这些API吗
  • 我如何从收到的请求中接收用户名和密码
  • 和他们在authenticate方法中所做的相同

  • 为什么使用UsernamePasswordAuthenticationToken,如果我们发送的是JWT令牌而不是用户名和密码,那么是哪个类 将用于提供回复
  • UsernamePasswordAuthenticationToken由spring security在内部使用。这 在春季创建会话时出现。它包含用户信息(如电子邮件等)和权限(角色)。例如,当您在应用程序中收到JWT令牌时,您将验证JWT令牌(签名等),在成功验证JWT后,您将创建UsernamePasswordAuthenticationToken对象,spring将在会话中保存它。对于每个传入的请求,spring将调用此对象上的
    boolean isAuthenticated()
    方法,以确定用户是否可以访问所需的资源

    现在,当您得到所有答案后,我的建议是使用Oauth2进行引导微服务。有很多例子可以说明如何实现它并根据您的需求定制它。(基本上,你必须实现你的授权服务器,它将使用你的服务checkUserAuthentication对用户进行身份验证,并生成accesstoken。你的微服务的每个消费者都需要发送他们从授权服务器获得的accesstoken,你需要在你的微服务中验证它。所以我们的微服务将充当资源服务器

    希望这会有帮助