【发布时间】:2021-08-26 17:43:13
【问题描述】:
有没有办法通过网关访问微服务 API 而无需身份验证?例如,如果我有一个需要从微服务 API 读取数据的公共登录页面。我启用了 CORS 并通过 Swagger 测试了 API,它在网关应用程序中运行良好;但是,如果我使用 CURL 调用 API,则会收到未经授权的错误。
这是我要执行的 CURL 命令:
curl -X 'GET' \
'http://localhost:8080/services/tajvoteservice/api/landing-page-by-organizations' \
-H 'accept: */*' \
-H 'X-XSRF-TOKEN: 5d3e3faf-3a3d-4905-bdea-f5ce305d3672'
这是我得到的错误:
{"type":"https://www.jhipster.tech/problem/problem-with-message","title":"Unauthorized","status":401,"detail":"Not Authenticated","path":"/services/tajvoteservice/api/landing-page-by-organizations","message":"error.http.401"}%
这是我的 SecurityConfiguration.java 配置方法:
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.csrf()
.disable()
.exceptionHandling()
.authenticationEntryPoint(problemSupport)
.accessDeniedHandler(problemSupport)
.and()
.headers()
.contentSecurityPolicy(jHipsterProperties.getSecurity().getContentSecurityPolicy())
.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'; fullscreen 'self'; payment 'none'")
.and()
.frameOptions()
.deny()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/api/auth-info").permitAll()
.antMatchers("/api/admin/**").hasAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/api/landing-page-by-organizations/**").permitAll()
.antMatchers("/api/**").authenticated()
.antMatchers("/management/health").permitAll()
.antMatchers("/management/health/**").permitAll()
.antMatchers("/management/info").permitAll()
.antMatchers("/management/prometheus").permitAll()
.antMatchers("/management/**").hasAuthority(AuthoritiesConstants.ADMIN)
.and()
.oauth2ResourceServer()
.jwt()
.jwtAuthenticationConverter(authenticationConverter())
.and()
.and()
.oauth2Client();
// @formatter:on
}
请指教。
【问题讨论】:
-
你检查过哪个应用触发了http 401:网关还是服务?
-
感谢您的提问。它来自网关。我通过关闭服务并运行 CURL 命令对此进行了测试;我仍然遇到同样的错误。
-
但是您添加到 SecurityConfiguration 的 permitAll() 来自微服务,对吧?您是否尝试在网关中做同样的事情?蚂蚁匹配器应该类似于“/services/tajvoteservice/api/landing-page-by-organizations”。另外,我不明白你为什么在关于授权的问题中提到 CORS。
-
我的 CORS 问题是:对于仅来自 www.travelodgefloridacity.com 上托管的客户端的“GET”请求,我如何限制对“saathratri.com/services/tajvoteservice/api/…”的访问? Marziou 先生概述的解决方案及其在此 StackOverflow 线程中显示的实现代码向任何连接到 Internet 的客户端打开资源“saathratri.com/services/tajvoteservice/api/…”。
标签: spring-boot jhipster jhipster-gateway