Spring Cloud云架构 - SSO单点登录之OAuth2.0 根据token获取用户信息(4)
上一篇我根据框架中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);
- }
- }
我这里只是简单写了一些登出的代码,我们会在后面的文章中详细贴出所有代码供大家参考,而且会从创建数据库,到执行操作的每一个流程记录下来。
从现在开始,我这边会将近期研发的spring cloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,大家来一起探讨spring cloud架构的搭建过程及如何运用于企业项目。源码来源