上一篇我根据框架中OAuth2.0的使用总结,画了SSO单点登录之OAuth2.0 登出流程,今天我们看一下根据用户token获取yoghurt信息的流程:
/**
* 根据token获取用户信息
* @param accessToken
* @return
* @throws Exception
*/
@RequestMapping(value = "/user/token/{accesstoken}", method = RequestMethod.GET)
public ResponseVO getUserByToken(@PathVariable(value = "accessToken", required = true) String accessToken,@RequestHeader(value = "userId", required = true) Long userId) throws Exception {
if(StringUtils.isEmpty(accessToken)){
return UserResponseCode.buildEnumResponseVO(UserResponseCode.RESPONSE_CODE_REQ_CANNOT_EMPTY, null);
}
OauthAccessToken oauthAccessToken = userMgrService.getOauthAccessToken(accessToken);
if(null == oauthAccessToken){
return UserResponseCode.buildEnumResponseVO(UserResponseCode.RESPONSE_CODE_OAUTH_ACCESSTOKEN_EMPTY, null);
}
String userName = oauthAccessToken.getUserName();
if (StringUtils.isEmpty(userName)) {
return UserResponseCode.buildEnumResponseVO(UserResponseCode.RESPONSE_CODE_OAUTH_ACCESSTOKEN_EMPTY, null);
}
return this.getUser(userName);
}
@RequestMapping(path = "/user/get/{userName}", method = RequestMethod.GET)
public ResponseVO getUser(@PathVariable(value = "userName") String userName) {
Map<String, Object> returnData = null;
try {
User user = userMgrService.getUserByName(userName);
if (null != user) {
returnData = new HashMap<String, Object>();
returnData.put("user", user);
return UserResponseCode.buildEnumResponseVO(UserResponseCode.RESPONSE_CODE_SUCCESS, returnData);
}
return UserResponseCode.buildEnumResponseVO(UserResponseCode.RESPONSE_CODE_SYSTEM_ERROR, null);
} catch (Exception e) {
return UserResponseCode.buildEnumResponseVO(UserResponseCode.RESPONSE_CODE_SYSTEM_ERROR, null);
}
}
我这里只是简单写了一些登出的代码,我们会在后面的文章中详细贴出所有代码供大家参考,而且会从创建数据库,到执行操作的每一个流程记录下来。
原文地址:http://blog.51cto.com/13795270/2136129
时间: 2024-10-06 18:10:10