JHipster:通过网关路由时,REST服务呼叫返回“ 401未经授权”

问题描述 投票:3回答:1

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

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

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

我们使用oauth2作为认证类型。

已部署为仅休息资源。最简单的情况有两个调用:/ 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”

我可以按照以下的常见问题解答或清单进行尝试并进行故障排除吗?

[请让我知道我可以添加哪些信息以帮助您。

编辑:

SecurityConfiguration:

@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:Application.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 / **路径上使用allowAll()。这不是一个很好的选择,因为它使端点不受保护。

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

Jhipster OAuth 2.0 / OIDC Authentication Authorization header with bearer token

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

jhipster jhipster-registry
1个回答
0
投票

通过添加打开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
    .

出于安全考虑,请注意默认配置不包括转发敏感标头,因此请确保如果您对此进行更改,请确保您了解自己在做什么。

© www.soinside.com 2019 - 2024. All rights reserved.