【问题标题】:JHipster: Rest service calls return "401 Unauthorized" when routing through the gatewayJHipster:通过网关路由时,Rest 服务调用返回“401 Unauthorized”
【发布时间】:2020-06-11 01:51:12
【问题描述】:

我是微服务和 JHipster 的新手,所以请耐心等待,并在必要时帮助我。

我认为是配置问题,但我似乎找不到。以下是一些细节:

我们正在使用 Keycloak 运行 JHipster 网关。环境是 Docker 组合,据我所知,我们已经完成了 JHipster Docker 文档中规定的必要工作。

我们使用 oauth2 作为身份验证类型。

Deployed 是仅 Rest 的资源。最简单的情况有两个调用: /api/hello -> 应该返回“我说你好” /free/hello -> 应该返回“我有空!”

/api/hello 调用应该是安全的,而 /free/hello 调用显然不是。

当我通过网关(即http://gatewayip:port/helloapp/free/hello)点击 /free/hello 服务时,我得到预期的响应“我有空!”

所以我预计网关已启动并正在运行并路由流量。

对于安全服务,我使用邮递员首先获取 JWT 令牌。
当我直接点击服务时,即 serverip:appPort/api/hello 我得到了我期望的响应

这对我来说表明服务正在运行,spring security 可以使用我的 JWT 令牌。

现在,当我尝试通过网关路由到安全服务时,问题就开始了。我通过邮递员使用相同的令牌。 http://gatewayip:port/helloapp/api/hello

这现在给了我回应: 输入“https://www.jhipster.tech/problem/problem-with-message” 标题“未经授权” 状态 401 详细信息“访问此资源需要完全身份验证” 路径“/api/hello” 消息“error.http.401”

是否有常见问题解答或清单可供我尝试排除故障?

请让我知道我可以添加哪些信息来提供帮助。

编辑:

安全配置:

@EnableWebSecurity
@Import(SecurityProblemSupport.class)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Value("${spring.security.oauth2.client.provider.oidc.issuer-uri}")
private String issuerUri;

private final JHipsterProperties jHipsterProperties;
private final JwtAuthorityExtractor jwtAuthorityExtractor;
private final SecurityProblemSupport problemSupport;

public SecurityConfiguration(JwtAuthorityExtractor jwtAuthorityExtractor, JHipsterProperties jHipsterProperties, SecurityProblemSupport problemSupport) {
    this.problemSupport = problemSupport;
    this.jwtAuthorityExtractor = jwtAuthorityExtractor;
    this.jHipsterProperties = jHipsterProperties;
}

@Override
public void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http
        .csrf()
        .disable()
        .exceptionHandling()
            .authenticationEntryPoint(problemSupport)
            .accessDeniedHandler(problemSupport)
    .and()
        .headers()
        .contentSecurityPolicy("default-src 'self'; frame-src 'self' data:; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://storage.googleapis.com; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:")
    .and()
        .referrerPolicy(ReferrerPolicyHeaderWriter.ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN)
    .and()
        .featurePolicy("geolocation 'none'; midi 'none'; sync-xhr 'none'; microphone 'none'; camera 'none'; magnetometer 'none'; gyroscope 'none'; speaker 'none'; fullscreen 'self'; payment 'none'")
    .and()
        .frameOptions()
        .deny()
    .and()
        .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    .and()
        .authorizeRequests()
        .antMatchers("/api/auth-info").permitAll()
        .antMatchers("/api/**").authenticated()//hasAuthority(AuthoritiesConstants.USER)//permitAll()
        .antMatchers("/management/health").permitAll()
        .antMatchers("/management/info").permitAll()
        .antMatchers("/management/prometheus").permitAll()
        .antMatchers("/management/**").hasAuthority(AuthoritiesConstants.ADMIN)
    .and()
        .oauth2ResourceServer()
            .jwt()
            .jwtAuthenticationConverter(jwtAuthorityExtractor)
            .and()
        .and()
            .oauth2Client();
    // @formatter:on
}

编辑 2: 应用程序.yml

security:
oauth2:
    client:
        access-token-uri: http://xxx.xxx.xxx.xxx:30080/auth/realms/test/protocol/openid-connect/token
        user-authorization-uri: http://xxx.xxx.xxx.xxx:30080/auth/realms/test/protocol/openid-connect/auth
        client-id: web_app
        client-secret: web_app
        scope: openid profile email

    resource:
        user-info-uri: http://xxx.xxx.xxx.xxx:30080/auth/realms/test/protocol/openid-connect/userinfo         

server:
 port: 40404

编辑 3: 这个问题和我的类似:

JHipster - How to add route to external microservices in application.yml

没有给出答案,发帖人得到的解决方案是只在 /api/** 路径上使用 permitAll()。这不是一个很好的选择,因为它会使端点不安全。

另一个类似的问题在这里:

Jhipster OAuth 2.0 / OIDC Authentication Authorization header with bearer token

这收到了一些使用@EnableResourceServer 的答案。这是一篇较旧的帖子,我的印象是我正在运行的 Jhipster 应用程序的新版本很好地迎合了这种情况 - 我说错了吗?

【问题讨论】:

    标签: jhipster jhipster-registry


    【解决方案1】:

    在 SpringSecurity 中开启登录后添加

    @Override
        public void configure(WebSecurity web){
            web.debug(true);
        }
    

    在 SecurityConfiguration.java 类中,我们可以看到网关没有转发安全标头。

    这将我们带到了这个页面:https://github.com/spring-cloud/spring-cloud-netflix/issues/3126

    这就解释了原因。

    当更改配置中的zuul参数时(gateway.yml/jhipster-registry.yml):

        zuul: # those values must be configured depending on the application specific needs
        .
        .
            ignore-security-headers: false
            ignored-headers: cookie,set-cookie
            sensitiveHeaders: Cookie,Set-Cookie
        .
    

    请注意,出于安全原因,默认配置不包括敏感标头的转发,因此请确保您了解更改此设置时所做的操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 2017-01-25
      • 1970-01-01
      • 2021-05-14
      • 2019-06-17
      • 2012-11-01
      • 2011-01-03
      相关资源
      最近更新 更多