简体   繁体   English

如何在spring boot security中的另一个微服务中调用我的身份验证微服务

[英]How to call my authentication microservice in another microservice in spring boot security

I have implemented an authentication service using Spring Security which has access to a database which stores user data.我已经使用 Spring Security 实现了一个身份验证服务,它可以访问存储用户数据的数据库。 Now I want to implement another service (open a totally new project) where I only specify the url of my authentication service.现在我想实现另一个服务(打开一个全新的项目),我只指定我的身份验证服务的 url。 I used .loginProcessingUrl() and changed the login page in the spring security configuration but that does not work.我使用了.loginProcessingUrl()并更改了 spring 安全配置中的登录页面,但这不起作用。 How do I make the new service authenticate using my authentication service?如何使用我的身份验证服务对新服务进行身份验证?

You could use a custom AuthenticationProvider to do the authentication for you.您可以使用自定义AuthenticationProvider为您进行身份验证。

Here's a barebones example:这是一个准系统示例:

  1. Create a CustomRemoteAuthenticationProvider that calls your authentication service:创建一个调用您的身份验证服务的CustomRemoteAuthenticationProvider
public class CustomRemoteAuthenticationProvider implements AuthenticationProvider {

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

        String username = authentication.getName();
        String password = authentication.getCredentials().toString();

        // call your authentication service
        // ... and return a UsernamePasswordAuthenticationToken

    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }
}
  1. Declare your CustomRemoteAuthenticationProvider as a bean (which you could do in WebSecurityConfigurerAdapter ), which will get picked up and added to the AuthenticationManager automatically:将您的CustomRemoteAuthenticationProvider声明为 bean(您可以在WebSecurityConfigurerAdapter ),它将被自动提取并添加到AuthenticationManager
@Bean
public CustomRemoteAuthenticationProvider customRemoteAuthenticationProvider() {
    return new CustomRemoteAuthenticationProvider();
}

Note : You could do all of this in one step by adding @Component directly to CustomRemoteAuthenticationProvider .注意:您可以通过将@Component直接添加到CustomRemoteAuthenticationProvider一步完成所有这些操作。 Also, check out the javadoc for AuthenticationProvider for a list of AuthenticationProvider s if you want more ideas on how to write one.此外, 检查出的javadocAuthenticationProvider对列表AuthenticationProvider ■如果您想了解如何编写一个更多的想法。 DaoAuthenticationProvider is commonly used with JdbcDaoImpl to authenticate against a database. DaoAuthenticationProvider通常与JdbcDaoImpl一起用于对数据库进行身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 测试需要调用另一个微服务的Spring Boot微服务 - Test a spring boot microservice that needs to call another microservice 如何使用 Spring Security 实现具有两级令牌认证的 Spring Boot 微服务? - How to implement Spring Boot Microservice with two levels of token authentication using Spring Security? 在 Spring Boot 中禁用或绕过 OAuth 微服务到微服务通信的安全性 - Disable or bypass OAuth security for microservice-to-microservice communication in Spring Boot Spring Boot从请求中获取承载令牌并调用另一个微服务 - spring boot get bearer token from request and call another microservice 如何架构 Spring 启动微服务 - how to architecture Spring Boot Microservice 具有OAuth 2和JWT for Security的Spring Boot微服务 - Spring boot microservice with OAuth 2 and JWT for Security Spring Boot - 有没有办法从另一个微服务扩展微服务中的 yml 文件? - Spring Boot - Is there a way to extend yml file in a microservice from another microservice? 在 Spring 启动时在 Gateway 服务处处理微服务的认证 - Handling authentication of microservice at Gateway service in Spring boot ,来自 spring boot 微服务的 web api 调用 - ,web api call from spring boot microservice 如何让我的 Spring Boot 微服务使用 HTTPS 运行? - How to make my Spring Boot microservice run using HTTPS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM