【发布时间】:2018-02-16 06:19:19
【问题描述】:
我正在尝试使用 Spring Boot 1.5.6.RELEASE 和 Spring Cloud Dalston.SR3 来实现微服务架构后端,这将被移动/网络端点使用。
API 网关应用程序
@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 - --request POST -u acme:acmesecret "http://localhost:8899/userauth/oauth/token?grant_type=password&username=<...>&password=<...>"
{ “ACCESS_TOKEN”:“eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDQ3ODg4NzgsInVzZXJfbmFtZSI6IjcyMTk2MTk2NDEiLCJhdXRob3JpdGllcyI6WyJST0xFX1BBVElFTlQiXSwianRpIjoiZThhMzBjNmQtZjA2MS00MWEzLWEyZGItYTZiN2ZjYTI5ODk1IiwiY2xpZW50X2lkIjoiYWNtZSIsInNjb3BlIjpbIm9wZW5pZCJdfQ.AhF_kqfsRYM1t1HVT ........ P>
我可以使用访问令牌向授权服务器或其他资源请求数据
curl -D - --request GET -H "授权:承载 eyJhbGciOiJSUzI1...." http://localhost:8899/userauth/me
{"authorities":[{"authority":"ROLE_P.........}
curl -D - --request GET -H "授权:承载 eyJhbGciOiJSUzI1NiIsInR5......." http://localhost:8081/user-service/
[{"firstName":"Anil".....}]
但是,对于通过 API 网关路由的相同请求,它会在网关本身失败并被过滤为 AnonymousAuthenticationToken。
curl -D - --request GET -H "授权:承载 eyJhbGciOiJSUzI1...." http://localhost:8765/user-service/
HTTP/1.1 302 设置 Cookie: XSRF-TOKEN=b5a1c34e-e83c-47ea-86a6-13a237c027d4;路径=/ 位置: http://localhost:8765/login
我假设使用@EnableZuulProxy 和@EnableOAuth2Sso,Zuul 会小心地将不记名令牌转发给下游服务,但事实并非如此。我已经有一个使用 HTTP 会话和浏览器重定向来让 API 网关传递令牌的工作示例 - https://github.com/anilallewar/microservices-basics-spring-boot
但我很难让它与无状态会话一起工作,Zuul API 网关端可能缺少什么指针?
【问题讨论】:
-
你有这个工作吗?我被困在同一个问题上。你能解释一下吗?
标签: spring-boot spring-security-oauth2 netflix-zuul spring-cloud-netflix