【问题标题】:I need to understand how to connect a API-Gateway with Authentication-Microservice and other Microservies我需要了解如何将 API 网关与身份验证微服务和其他微服务连接
【发布时间】:2020-04-30 16:27:40
【问题描述】:

758/5000 大家好,我有几个问题在网上没有找到确切的答案。你应该知道,在微服务方面,我是一个血腥的初学者。

我创建了几个相互连接的微服务。一方面是 Zuul-Api-Gateway,一个用于登录的单独微服务和两个称为模板和联系人的微服务。

我也可以通过zuul来寻址所有的微服务,但是我真的只希望登录微服务可以被寻址。 另一方面,登录微服务应该能够在成功登录后访问微服务模板和联系人。

问题1)如何禁止API网关直接寻址各种服务?

这是我的 ZuulFilter

@Component
public class ZuulLoggingFilter extends ZuulFilter {

    private Logger logger= LoggerFactory.getLogger(this.getClass());    

    @Override
    public boolean shouldFilter() {

        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext.getCurrentContext();
        HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
        logger.info("request ->{} request uri -> {}", request, request.getRequestURI());

        return null;
    }

我来自 Zuul 的 Application.properties

spring.application.name=zuul
server.port=8800
eureka.client.serviceUrl.defaultZone = http://localhost:8762/eureka

#zuul.routes.contacts.path = /contacts/**
#zuul.routes.contacts.url = http://localhost:8100/

#zuul.routes.templates.path = /templates/**
#zuul.routes.templates.url = http://localhost:8200/

我的安全配置

@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{

    @Autowired
    UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
        auth.userDetailsService(userDetailsService);
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.authorizeRequests() 
        .antMatchers("/users").hasRole("ADMIN")
        .antMatchers("/user").hasAnyRole("ADMIN","USER")
        .antMatchers(HttpMethod.POST,"/users").hasRole("ADMIN")
        .and().formLogin()
        .and().
        csrf().disable();
    }

代码和路径变量来自我的登录微服务 如果我忘记了什么,请告诉我应该发布什么。

【问题讨论】:

    标签: java microservices


    【解决方案1】:

    你不把登录服务作为 api-gateway 的一部分吗?

    【讨论】:

    • 现在我做到了。但是现在微服务找不到我的用户。这怎么可能?
    猜你喜欢
    • 2019-01-01
    • 1970-01-01
    • 2020-09-25
    • 2019-04-06
    • 2018-07-17
    • 2018-05-04
    • 2018-01-06
    • 2020-05-02
    • 2019-07-28
    相关资源
    最近更新 更多