我正在使用Facebook帐户套件Java和jwt令牌创建登录电话短信,看来工作正常,但我无法将长id用户添加到令牌承载中作为子项的声明中。
这是我的代码Facebook服务,我可以找到用户或创建用户,但是我不知道如何从findByAccountKit中找到用户长ID。
public FBToken authorizeToken(String appId, String appSecret, String code) throws FacebookException {
String authUrl = this.environment.getProperty(FacebookConstant.CONFIG_FB_GRAPH_AK_URL_ACCESS_TOKEN)
+ "?grant_type=authorization_code&code=" + code + "&access_token=AA|" + appId + "|" + appSecret;
log.info("authUrl : " + authUrl );
Object obj = this.restTemplate.getForObject(authUrl, Object.class);
@SuppressWarnings("unchecked")
Map<String, Object> info = (HashMap<String,Object>) obj;
String user_id = (String) info.get("id");
String user_access_token = (String) info.get("access_token");
String me_endpoint_uri = this.environment.getProperty(FacebookConstant.CONFIG_FB_GRAPH_AK_URL_ME) + "?access_token=" + user_access_token;
Object me = restTemplate.getForObject(me_endpoint_uri,Object.class);
//Set Phone User Object
@SuppressWarnings("unchecked")
Map<String, Object> map = (HashMap<String,Object>) me;
@SuppressWarnings("unchecked")
Map<String, Object> phone = (HashMap<String,Object>) map.get("phone");
String number = (String) phone.get("number");
String acccountKit = (String) info.get("id");
User userSMS = userRepository.findByAcccountKit(acccountKit);
if (userSMS == null) {
// Sign up user and login
User user = new User();
user.setAcccountKit(user_id);
user.setPhoneNumber(number);
user.setToken(user_access_token);
System.out.println(user);
User result = userRepository.save(user);
}
FBToken response= new FBToken();
try {
response = this.restTemplate.getForObject(authUrl, FBToken.class);
} catch (Exception e) {
// TODO: handle exception
log.error(e);
}
System.out.println(response);
log.info("Token : " + code + " | response : " + response);
return response;
}
public String getRegisteredMobileNumberToken(String code) throws FacebookException {
return null;
}
public User authorizeToken(String code, String mobile) throws FacebookException {
User login = new User();
FBToken fbToken = authorizeToken(this.environment.getProperty(FacebookConstant.CONFIG_FB_GRAPH_AK_APP_ID),
this.environment.getProperty(FacebookConstant.CONFIG_FB_GRAPH_AK_APP_SECRET), code);
if(fbToken.getAccess_token() == null) {
login.setUsername(mobile);
}
return login;
}
这是我登录用户和注册用户的端点
@Validated
@RestController
@RequestMapping(value = "/login/fb")
public class LoginFBController {
@Autowired
private JwtTokenHandler jwtTokenHandler;
@Autowired
private AccountKitService accountKitService;
@PostMapping("/ak")
public APIResponse loginViaFB(@RequestParam String mobile, @RequestParam String code) throws FacebookException {
return manipulateLoginResponse(this.accountKitService.authorizeToken(code, mobile));
}
private APIResponse manipulateLoginResponse(User user){
APIResponse apiResponse = new APIResponse();
if (user.getUsername() == null) {
apiResponse.setMessage("Authentication failed");
apiResponse.setStatus(false);
apiResponse.setStatusCode(HttpStatus.UNAUTHORIZED.value());
} else {
String token = jwtTokenHandler.generate(user.getUsername(), user.getAcccountKit());
apiResponse.setAccessToken(token);
apiResponse.setTokenType("Bearer");
apiResponse.setMessage("Authenticated");
apiResponse.setStatus(true);
apiResponse.setStatusCode(HttpStatus.OK.value());
}
return apiResponse;
}
}
这是我的令牌句柄生成和验证
public String generate(String username) {
Claims claims = Jwts.claims().setSubject(username);
Date now = new Date();
Date expiryDate = new Date(now.getTime() + jwtExpirationInMs);
return Jwts.builder()
.setIssuedAt(new Date())
.setExpiration(expiryDate)
.setClaims(claims)
.signWith(SignatureAlgorithm.HS512, jwtSecret)
.compact();
}
public JwtUserDetails validate (String token) {
JwtUserDetails jwtUserDetails = null;
try {
Claims body = Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody();
jwtUserDetails = new JwtUserDetails(body.getSubject(), token);
} catch (Exception e) {
e.printStackTrace();
}
return jwtUserDetails;
}
代码正在努力生成和验证令牌,但是我想在声明中添加长用户ID,这样很容易按ID查找用户。有人有主意吗?谢谢。