4

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 構成が無効になり、CSRF エラーを受け取ることなくサービスに POST 要求を行うことができました。ただし、BasicAuth ヘッダーが要求に含まれなくなったため、OAuth サーバーは要求を拒否しています。Zuul がヘッダーを削除しているようです。アノテーションを追加する@EnableOAuth2Ssoとアプリケーションが OAuth を認識するようになり、構成された OAuth サーバーにアクセスする手段が許可されるか、それとも単純に Bearer トークンに適用されると誤解していますか? OAuth サーバーをプロキシの背後に配置するのは正常ですか、それとも予期しないことですか? ドキュメントからまだ理解していない重要な知識や構成が欠けていると思います。

ここで何か助けていただければ幸いです。

4

1 に答える 1