Spring boot Spring Cloud Zuul API网关不';t为无状态会话转发JWT令牌
我正在尝试使用移动/web端点使用的Spring Boot 1.5.6.RELEASE和Spring Cloud Dalston.SR3来实现微服务体系结构后端 API网关应用程序Spring boot Spring Cloud Zuul API网关不';t为无状态会话转发JWT令牌,spring-boot,spring-security-oauth2,netflix-zuul,spring-cloud-netflix,Spring Boot,Spring Security Oauth2,Netflix Zuul,Spring Cloud Netflix,我正在尝试使用移动/web端点使用的Spring Boot 1.5.6.RELEASE和Spring Cloud Dalston.SR3来实现微服务体系结构后端 API网关应用程序 @SpringBootApplicatio @EnableEurekaClient @EnableZuulProxy public class GatewayApplication { public static void main(String[] args) { SpringAppli
@SpringBootApplicatio
@EnableEurekaClient
@EnableZuulProxy
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
API安全性
@Configuration
@EnableWebSecurity
@Order(ManagementServerProperties.ACCESS_OVERRIDE_ORDER)
@EnableOAuth2Sso
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/sign-up", "/login")
.permitAll()
.anyRequest()
.authenticated()
.and()
.csrf()
.ignoringAntMatchers("/sign-up", "/login")
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
// @formatter:on
}
}
Gradle安全相关依赖项
// Spring OAuth2 security
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.security.oauth:spring-security-oauth2")
compile("org.springframework.cloud:spring-cloud-starter-oauth2")
compile("org.springframework.security:spring-security-jwt")
祖尔航线
zuul:
ignoredServices: '*'
routes:
user-service:
path: /user-service/**
stripPrefix: false
serviceId: user-webservice
sensitiveHeaders:
task-service:
path: /task-service/**
stripPrefix: false
serviceId: task-webservice
sensitiveHeaders:
user:
path: /userauth/**
stripPrefix: false
serviceId: auth-server
sensitiveHeaders:
我能够从授权服务器获取访问令牌(无状态会话-无JSESSIONID cookie)
curl-D---请求POST-u acme:acmescret
“&密码=”
{“访问令牌”:“eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.Eyjlehaioj1mdq3odg4nzgsinvzzxjfbmftzsi6ijcymtk2ndelcjhdxrob3jpdglllcyi6wyjjst0xf1bbvelftlqixswianrpijjoizhhhzmzmzmqtzja2ms0mmzz0mwzzzzzzzzmzmzzmzzzzzmzzzg5odk1iiy2xpzzzzzzzzzzzzzzzzzzzzzzyzzzzzzzzzzzzzzzzzzzzzz
我可以使用访问令牌从授权服务器或其他资源请求数据
curl-D---请求GET-H“授权:承载
EYJHBGCOIJSUZI1……”
{“权威”:[{“权威”:“角色”\u p……}
curl-D---请求GET-H“授权:承载
EYJHBGCOIJSUZI1NIISINR5……”
[{“名字”:“Anil”…}]
但是,对于通过API网关路由的相同请求,它在网关本身失败,并被过滤为AnonymousAuthenticationToken
curl-D---请求GET-H“授权:承载
EYJHBGCOIJSUZI1……”
HTTP/1.1 302集Cookie:
XSRF-TOKEN=b5a1c34e-e83c-47ea-86a6-13a237c027d4;路径=/位置:
我假设有了@EnableZuulProxy
和@EnableOAuth2Sso
,Zuul将负责将承载令牌转发到下游服务,但这并没有发生。我已经有了一个工作示例,它使用HTTP会话和浏览器重定向让API网关传递令牌-
但我正在努力使它能够处理无状态会话,Zuul API网关端可能缺少的任何指针?Zuul默认情况下将授权头视为敏感头,不会将其传递给下游请求。若要覆盖这一点,您可以在Zuul配置或全局配置中修改
sensitiveHeaders
ly(适用于所有路线):
或针对特定路线:
zuul:
ignoredServices: '*'
routes:
user-service:
path: /user-service/**
stripPrefix: false
serviceId: user-webservice
# exclude Authorization from sensitive headers
sensitiveHeaders: Cookie,Set-Cookie
要了解有关问题的更多信息,请检查以下问题:
我假设使用@EnableZuulProxy和@EnableOAuth2Sso,Zuul会注意将承载令牌转发到下游服务,但事实并非如此
我也这么想,但我的(痛苦)根据经验,@enableAuth2sso使用SSO保护所有端点,甚至阻止使用承载令牌的请求进入下游服务。我必须更改网关以禁用通向资源的路由上的身份验证,以便使用承载令牌的请求能够通过
尝试将/user service/**
和/task service/**
添加到您的permitAll()
匹配器:
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/sign-up", "/login", "/task-service/**", "/user-service/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.csrf()
.ignoringAntMatchers("/sign-up", "/login")
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
// @formatter:on
}
你有这个工作吗?我被困在同一个问题上。你能解释一下吗?敏感标题已经设置为空(敏感标题:)如果你看原始问题。那肯定不是问题。谢谢Alex。这救了我一天。是的,我还注意到我必须用@EnableOAuth2Sso将请求授权给每个微服务。在spring boot 2.0中我仍然可以注意到这一点。
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/sign-up", "/login", "/task-service/**", "/user-service/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.csrf()
.ignoringAntMatchers("/sign-up", "/login")
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
// @formatter:on
}