<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> </dependency> <dependency> <groupId>com.webauthn4j</groupId> <artifactId>webauthn4j-core</artifactId> <version>${webauthn4j-core-version}</version> </dependency>
@Bean SecurityFilterChain filterChain(HttpSecurity http) { http .formLogin(withDefaults()) .webAuthn((webAuthn) -> webAuthn .rpName("Your Application Name") .rpId("your-domain.com") .allowedOrigins("https://your-domain.com") ); return http.build(); } @Bean UserDetailsService userDetailsService() { UserDetails userDetails = User.withDefaultPasswordEncoder() .username("user") .password("password") .roles("USER") .build(); return new InMemoryUserDetailsManager(userDetails); }
POST /webauthn/register/options X-CSRF-TOKEN: <your-csrf-token>
// 在前端使用 navigator.credentials.create() 创建凭证 const credential = await navigator.credentials.create({ publicKey: publicKeyCredentialCreationOptions });
POST /webauthn/register { "publicKey": { "credential": { // 凭证详细信息 }, "label": "my-passkey" } }
POST /webauthn/authenticate/options X-CSRF-TOKEN: <your-csrf-token>
POST /login/webauthn { // 凭证验证信息 }