春云Zuul不传递访问令牌
问题描述:
我想使用Spring Cloud Zuul作为api /身份验证网关。我已成功实现了zuul后面的服务的持票人令牌授权,并且我成功将Zuul转发给我的表单登录并路由回到我的应用程序,但我无法让Zuul将不记名令牌传递给服务。春云Zuul不传递访问令牌
我Zuul配置如下:
@EnableEurekaClient
@EnableZuulProxy
@SpringBootApplication
@RestController
public class Application { ... }
我的服务配置如下:
@Profile("oauth")
@Configuration
@EnableResourceServer
@EnableWebSecurity
public static class InternalApiGatewayConfig extends ResourceServerConfigurerAdapter {
当我的角度应用程序试图通过zuul访问我的服务,我得到
{"error":"unauthorized","error_description":"Full authentication is required to access this resource"}
我已经设法通过将下面的代码放在ZuulFilter中解决了这个问题,但是它看起来不正确:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails)authentication.getDetails();
String tokenValue = details.getTokenValue();
ctx.addZuulRequestHeader("Authorization", "bearer " + tokenValue);
我的理解是Zuul应该自动发送持票人标记值。我错过了什么?
答
所以我想出了我自己的问题的答案,它很简单。我的项目导入spring-security-oauth2
。我只需要在spring-cloud-security
上添加依赖关系。这样,我就不必实施ZuulFilter了。