【问题标题】:zuul proxy always giving 401 unauthorizedzuul代理总是给401未经授权
【发布时间】:2023-04-02 16:02:01
【问题描述】:

我正在为路由使用 zuul 代理,并为此添加了 JWT 身份验证。例如,我已指定要跳过授权的 API (/auth),但我无法调用相同的 API,因为我也为允许的 URL 获得 401。

下面是代码sn-p。

实现 WebSecurityConfigurerAdapter 的类

    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .exceptionHandling().authenticationEntryPoint(new JwtAuthenticationEntryPoint())
                .and()
                .addFilterAfter(new JwtTokenAuthenticationFilter(jwtConfig), UsernamePasswordAuthenticationFilter.class)
                .authorizeRequests()
                .antMatchers(HttpMethod.POST, "/auth/**").permitAll()
                .antMatchers("/ping").permitAll()
                .antMatchers("/login/**").permitAll()
                .antMatchers("/signup/**").permitAll()
                .anyRequest().authenticated();

    }

我的 application.properties 文件如下所示

server.port=8762
spring.application.name=zuul-server
eureka.client.service-url.default-zone=http://localhost:8761/eureka/

# A prefix that can added to beginning of all requests.
zuul.prefix=/api

# Disable accessing services using service name (i.e. gallery-service).
# They should be only accessed through the path defined below.
zuul.ignored-services=*

# Map paths to services
zuul.routes.user-service.path=/users/**
zuul.routes.user-service.service-id=user-service
zuul.routes.user-service.sensitive-headers=Cookie,Set-Cookie

# Map path to auth service
zuul.routes.auth-service.path=/auth/**
zuul.routes.auth-service.service-id=auth-service
zuul.routes.auth-service.strip-prefix=false
# Exclude authorization from sensitive headers
zuul.routes.auth-service.sensitive-headers=Cookie,Set-Cookie

但我无法点击 /ping 或 /login 或 /auth API 都给出 401。

有人可以帮我解决同样的问题吗?

提前致谢!!!

【问题讨论】:

  • 我想你在配置文件中添加了这个zuul.prefix=/api,它与你的端点不匹配

标签: java spring-boot microservices netflix-zuul


【解决方案1】:

试试这个

   .antMatchers(HttpMethod.POST, "/api/auth/**").permitAll()

【讨论】:

    猜你喜欢
    • 2018-04-08
    • 1970-01-01
    • 2016-10-28
    • 2014-07-03
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    相关资源
    最近更新 更多