我正在尝试使用 Spring Boot、Eureka、Zuul 和 Spring OAuth 设置 Zuul 反向代理。具体来说,我正在尝试从 Zuul 后面的 OAuth 服务器获取 OAuth 持有者令牌。为此,我需要向重定向到我们的 OAuth 服务器的代理端点发出 POST 请求。此请求使用 client_credentials 授权类型,因此使用 BasicAuth 来获取不记名令牌。我已经验证我可以绕过 Zuul 来获取令牌。
我在获得预期结果时遇到了麻烦,这是一个支持 OAuth 的反向代理,但本身没有必需的安全性。我尝试了几种不同的配置变体,但找不到金票。
这是我的Maven:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mycompany.cloud</groupId>
<artifactId>mycompany-cloud</artifactId>
<version>0.0.2-SNAPSHOT</version>
</parent>
<artifactId>mycompany-cloud-zuul-proxy</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.3.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我最初创建的配置只是
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
@EnableOAuth2Sso
public class ZuulProxyApplication {
public static void main(final String[] args) {
SpringApplication.run(ZuulProxyApplication.class, args);
}
}
但默认情况下,这启用了基本身份验证安全性。我知道这一点是因为我会在发出任何 POST 请求时得到 CSRF 错误。设置security.enable-csrf=false
并没有禁用它(我发现这很奇怪)。设置security.basic.enabled=false
也没有禁用任何安全也奇怪。我终于注意到 JavaDoc 上@EnableOAuth2Sso
说如果没有WebSecurityConfigurerAdapter
提供,那么它将使用默认值。我尝试将其添加@EnableWebSecurity
到我应该添加的配置中,WebSecurityConfigurerAdapter
但我的 POST 请求仍然出现 CSRF 错误。也许默认它使用不知道SecurityProperties。所以我最终得到了这个配置:
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ZuulProxyApplication {
public static void main(final String[] args) {
SpringApplication.run(ZuulProxyApplication.class, args);
}
@Configuration
@EnableOAuth2Sso
@EnableWebSecurity
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
public void globalUserDetails(final AuthenticationManagerBuilder auth) throws Exception {
// add no users
auth.inMemoryAuthentication();
}
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
}
和以下属性:
spring:
application:
name: mycompany-cloud-zuul-proxy
index: 0
security:
oauth2:
client:
access-token-uri: http://mycompany-cloud-authorization-server/oauth/token
user-authorization-uri: http://mycompany-cloud-authorization-server/oauth/authorize
basic:
enabled: false
enable-csrf: false
sessions: stateless
server:
port: 9200
eureka:
client:
service-url:
defaultZone: http://localhost:9100/eureka/
这很成功,它禁用了 CSRF 配置,我能够向我的服务发出 POST 请求而不会收到 CSRF 错误。但是,现在我的 OAuth 服务器拒绝请求,因为 BasicAuth 标头不再出现在请求中。Zuul 似乎正在剥离标题。我是否误解了添加@EnableOAuth2Sso
注释会使应用程序知道 OAuth 并且它将允许访问已配置的 OAuth 服务器的方式,或者它是否仅适用于 Bearer 令牌?将您的 OAuth 服务器放在代理后面是否正常,或者这不是预期的事情?我猜我错过了一些我尚未从文档中理解的重要知识和/或配置。
这里的任何帮助将不胜感激。