<dependency>
<groupId>com.bitwarden</groupId>
<artifactId>passwordless</artifactId>
<version>1.0.5</version>
</dependency>
创建 PasswordlessClient
实例:
import com.bitwarden.passwordless.*;
import java.io.*;
public class PasswordlessJavaSdkExample implements Closeable {
private final PasswordlessClient client;
public PasswordlessClientExample() {
PasswordlessOptions options = PasswordlessOptions.builder()
.apiSecret("your_api_secret")
.build();
client = PasswordlessClientBuilder.create(options)
.build();
}
@Override
public void close() throws IOException {
client.close();
}
}
import com.bitwarden.passwordless.*;
import com.bitwarden.passwordless.error.*;
import com.bitwarden.passwordless.model.*;
import java.io.*;
import java.util.*;
public class PasswordlessJavaSdkExample {
private final PasswordlessClient client;
// 构造函数
public String getRegisterToken(String alias) throws PasswordlessApiException, IOException {
// 从会话中获取现有用户 ID 或创建新用户
String userId = UUID.randomUUID().toString();
// 提供给 Api 的选项
RegisterToken registerToken = RegisterToken.builder()
// 您的用户 id
.userId(userId)
// 例如用户电子邮件地址,将显示在浏览器 UI 中
.username(alias)
// 可选:将此用户 ID 链接到别名(例如电子邮件地址)
.aliases(Arrays.asList(alias))
.build();
RegisteredToken response = client.registerToken(registerToken);
// 返回此令牌
return response.getToken();
}
}
import com.bitwarden.passwordless.*;
import com.bitwarden.passwordless.error.*;
import com.bitwarden.passwordless.model.*;
import java.io.*;
public class PasswordlessJavaSdkExample {
private final PasswordlessClient client;
// Constructor
public VerifiedUser verifySignInToken(String token) throws PasswordlessApiException, IOException {
VerifySignIn signInVerify = VerifySignIn.builder()
.token(token)
.build();
// 用户登录、设置 cookie 等
return client.signIn(signInVerify);
}
}