首页 > 解决方案 > 我需要了解如何将 API 网关与身份验证微服务和其他微服务连接

问题描述

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();
    }

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

标签: javamicroservices

解决方案


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


推荐阅读