我尝试了Spring Cloud Gateway / KeyCloak

我尝试了以下文章的内容。

 

尝试的事项

我只跳过了第七步并实现了其他部分,使用的是Kotlin语言。

image.png

Keycloak的配置

使用Docker启动Keycloak。

只需要一种选项来将以下命令的意思用中文表达出来:自动下载Docker镜像。

docker run -d --name keycloak -p 8888:8080 \
   -e KEYCLOAK_USER=spring \
   -e KEYCLOAK_PASSWORD=spring123 \
   jboss/keycloak

创建客户

image.png

获取客户端秘钥

Keycloak_Admin_Console.png

创建没有测试范围的Keycloak也是一样的。

下载Spring Boot

image.png

build.gradle.kts的配置

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
	id("org.springframework.boot") version "2.5.2"
	id("io.spring.dependency-management") version "1.0.11.RELEASE"
	kotlin("jvm") version "1.5.20"
	kotlin("plugin.spring") version "1.5.20"
}

group = "phoneappli.people"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
	mavenCentral()
}

dependencyManagement {
	imports {
		mavenBom("org.springframework.cloud:spring-cloud-dependencies:2020.0.3")
	}
}

dependencies {
	implementation("org.jetbrains.kotlin:kotlin-reflect")
	implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
	implementation("org.springframework.cloud:spring-cloud-starter-gateway")
	implementation("org.springframework.boot:spring-boot-starter-web")
	implementation("org.springframework.boot:spring-boot-starter-security")
	implementation("org.springframework.security:spring-security-test")
	implementation("org.springframework.security:spring-security-oauth2-resource-server")
	implementation("org.springframework.security:spring-security-oauth2-jose")
	implementation("org.springframework.security:spring-security-oauth2-client")

	testImplementation("org.springframework.boot:spring-boot-starter-test")

}

tasks.withType<KotlinCompile> {
	kotlinOptions {
		freeCompilerArgs = listOf("-Xjsr305=strict")
		jvmTarget = "1.8"
	}
}

tasks.withType<Test> {
	useJUnitPlatform()
}

应用程序的yml配置

server.port: 8090

spring:
  security:
    oauth2:
      client:
        provider:
          keycloak:
            token-uri: http://127.0.0.1:8888/auth/realms/master/protocol/openid-connect/token
            authorization-uri: http://127.0.0.1:8888/auth/realms/master/protocol/openid-connect/auth
            userinfo-uri: http://127.0.0.1:8888/auth/realms/master/protocol/openid-connect/userinfo
            user-name-attribute: preferred_username
        registration:
          keycloak-with-test-scope:
            provider: keycloak
            client-id: spring-with-test-scope
            client-secret: f7fab54d-b766-4fc3-b3a2-fbcaf826e816
            authorization-grant-type: authorization_code
            redirect-uri: "{baseUrl}/login/oauth2/code/keycloak"
  application:
    name: callme
    security:
      oauth2:
        resourceserver:
          jwt:
            issuer-uri: http://127.0.0.1:8888/auth/realms/master
  cloud:
    gateway:
      default-filters: TokenRelay
      routes:
        - id: callme-service
          uri: http://127.0.0.1:5000 #callmeサービスのURL
          predicates:
            - Path=/callme/**
          filters:
            - RemoveRequestHeader=Cookie #callmeサービスへ転送する時にCookieを削除
  main:
    web-application-type: reactive
    allow-bean-definition-overriding: true

logging:
  level:
    org.springframework.security: DEBUG

Spring Security的配置

import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
class SecurityConfig: WebSecurityConfigurerAdapter() {
    override fun configure(http: HttpSecurity) {
        http.authorizeHttpRequests {
            authorize -> authorize.anyRequest().authenticated()
        }.oauth2ResourceServer().jwt()
    }
}

确认动作

使用Python Flask实现了一个只返回字符串的Callme服务。

Please_sign_in.png
Sign_in_to_Keycloak.png
127_0_0_1_8090_callme_ping.png

在转发给callme服务的请求中,cookie已被删除,并且以下内容已附加到header中。

Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJaSlZqWmlHUzgybmVETVlpVUZaRW1QQjBJM0ItOXJSTmhzQWJMWnhnbmtjIn0.eyJleHAiOjE2MjU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