1 回答
TA贡献1797条经验 获得超4个赞
您可以创建一个 API
id_token
从请求正文中的客户端获取。调用 facebook 的 API 来验证
id_token
并获取用户的详细信息。在数据库中保存或更新用户。
验证用户
创建并返回 JWT
这是一个示例代码 -
@PostMapping("/signin/facebook")
public ResponseEntity<?> signInWithFacebook(@Valid @RequestBody FBLoginRequest loginRequest) {
// Call facebook's API to validate id_token and get user's details
// Register new user (Note: Also add code to check if a user already exists with the given facebookId )
User user = new User(fbName, fbID,
emailId, null);
Role userRole = roleRepository.findByName(RoleName.ROLE_USER)
.orElseThrow(() -> new AppException("User Role not set."));
user.setRoles(Collections.singleton(userRole));
User result = userRepository.save(user);
// Authenticate User
UserPrincipal userPrincipal = UserPrincipal.create(result);
PreAuthenticatedAuthenticationToken authentication = new PreAuthenticatedAuthenticationToken(userPrincipal, null, userPrincipal.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
// Generate and return token
String jwt = tokenProvider.generateToken(authentication);
return ResponseEntity.ok(new JwtAuthenticationResponse(jwt));
}
我没有添加调用 facebook 的 API 的代码。但这应该很容易实现。
添加回答
举报