Java BFF
可选 vertex-sdk-web 模块为 Spring Servlet 应用提供 Hosted 与自定义 server_session。
已有 core Bearer 接入保持独立。先按公共流程确定回调和存储。
增加 Web 模块
复用 core 的固定源码 checkout 与限定 group 的 Maven 本地仓库:
bash gradlew :vertex-sdk-core:publishToMavenLocal :vertex-sdk-web:publishToMavenLocal
dependencies {
implementation("com.chaosguide.iam:vertex-sdk-web:0.1.0")
}
这是源码构建的本地候选坐标,不代表已经发布 Maven Central。模块使用 Java 17 classfile, 基线为 Spring Boot 3.4.4 / Security 6.4,不继承 IAM 后端的 Java 25 运行要求。
配置 Spring
import java.net.URI;
import java.util.Set;
import com.chaosguide.iam.vertex.web.CookieWebSessionCarrier;
import com.chaosguide.iam.vertex.web.EnableWebBff;
import com.chaosguide.iam.vertex.web.MemoryWebSessionStore;
import com.chaosguide.iam.vertex.web.WebBff;
import com.chaosguide.iam.vertex.web.WebBffConfig;
import com.chaosguide.iam.vertex.web.WebSessionCarrier;
import com.chaosguide.iam.vertex.web.WebSessionStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebBff
public class VertexBffConfiguration {
@Bean
WebBffConfig bffConfig(Environment env) {
URI origin = URI.create("https://app.example.com");
return new WebBffConfig(
URI.create(env.getRequiredProperty("IAM_ENDPOINT")),
env.getRequiredProperty("IAM_RESOURCE_AUDIENCE"),
env.getRequiredProperty("IAM_WEB_CLIENT_ID"),
env.getRequiredProperty("IAM_WEB_CLIENT_SECRET"),
origin.resolve("/auth/callback"), origin, "/auth",
Set.of("/signed-in", "/signed-out"), "/signed-in",
Set.of("openid", "profile"), false, 1);
}
@Bean(destroyMethod = "close")
WebSessionStore bffStore() { return new MemoryWebSessionStore(); }
@Bean
WebSessionCarrier bffCarrier(WebBffConfig config) {
return new CookieWebSessionCarrier(config);
}
@Bean
SecurityFilterChain security(HttpSecurity http, WebBff bff) throws Exception {
bff.configure(http, request -> request.getRequestURI().startsWith("/api/"));
http.authorizeHttpRequests(rules -> rules
.requestMatchers("/api/**").authenticated().anyRequest().permitAll())
.requestCache(cache -> cache.disable())
.formLogin(form -> form.disable()).httpBasic(basic -> basic.disable())
.logout(logout -> logout.disable());
return http.build();
}
}
按公共配置在后端提供四项环境变量。
仅添加依赖不会开放路由或创建安全链。@EnableWebBff 提供组件,宿主将 WebBff.configure 挂入自己的安全链。
该链必须匹配 /auth/** 与受保护业务路径。示例用同一条链覆盖所有路径,对 /api/** 要求认证;
继续添加实际业务接口和权限,例如使用 @RequirePerm 与已有 IamContext。
适配器对自己处理的写请求校验精确 Origin 与 X-CSRF-Token。其他路径继续沿用应用 CSRF 规则。
同一条链同时承接 Bearer 请求时,需要显式配置对应资源服务器过滤器;无效 Authorization 不能回退成 Cookie 身份。
浏览器与存储
搭配公共 React 示例,支持 Hosted 与现有 Embedded/Dialog/Custom; 登记的协议回调由 Java 消费。
MemoryWebSessionStore 是同一 JVM 内共用的单例,有容量限制、不持久化,声明多个进程会被拒绝。
自定义 provider 实现 WebSessionStore,保证原子版本、所有权和轮换语义。
CookieWebSessionCarrier 使用独立随机 Cookie;HttpSessionWebSessionCarrier 则在原生 Servlet Session
中只放 SDK handle,保留无关字段。原生 Cookie 策略需要显式配置。
Spring Session 与分布式自动保存存储需要另外验证 provider 与桥接行为。
参考独立 Maven 应用 与生成的 Web 模块 API。