use of com.publiccms.view.pojo.oauth.OauthUser in project PublicCMS-preview by sanluan.
the class OauthController method callback.
/**
* @param channel
* @param state
* @param code
* @param request
* @param session
* @param response
* @param model
* @return view name
*/
@RequestMapping(value = "callback/{channel}")
public String callback(@PathVariable("channel") String channel, String state, String code, HttpServletRequest request, HttpSession session, HttpServletResponse response, ModelMap model) {
Oauth oauthComponent = oauthChannelMap.get(channel);
SysSite site = getSite(request);
Cookie stateCookie = RequestUtils.getCookie(request.getCookies(), STATE_COOKIE_NAME);
if (null != oauthComponent && oauthComponent.enabled(site.getId()) && null != stateCookie && null != state && state.equals(stateCookie.getValue())) {
try {
OauthAccess oauthAccess = oauthComponent.getOpenId(site.getId(), code);
if (null != oauthAccess && null != oauthAccess.getOpenId()) {
Cookie cookie = RequestUtils.getCookie(request.getCookies(), RETURN_URL);
String returnUrl = site.getDynamicPath();
if (null != cookie && null != cookie.getValue()) {
returnUrl = cookie.getValue();
}
SysUserToken entity = sysUserTokenService.getEntity(oauthAccess.getOpenId());
if (null != entity) {
if (entity.getChannel().equals(channel)) {
setUserToSession(session, sysUserService.getEntity(entity.getUserId()));
return REDIRECT + returnUrl;
}
} else {
SysUser user = getUserFromSession(session);
if (null == user) {
OauthUser oauthUser = oauthComponent.getUserInfo(site.getId(), oauthAccess);
Map<String, String> config = configComponent.getConfigData(site.getId(), AbstractOauth.CONFIG_CODE);
if (null != oauthUser && CommonUtils.notEmpty(config) && CommonUtils.notEmpty(config.get(LoginConfigComponent.CONFIG_REGISTER_URL))) {
model.addAttribute("nickname", oauthUser.getNickname());
model.addAttribute("openId", oauthUser.getOpenId());
model.addAttribute("avatar", oauthUser.getAvatar());
model.addAttribute("gender", oauthUser.getGender());
model.addAttribute("channel", channel);
model.addAttribute("returnUrl", returnUrl);
return REDIRECT + config.get(LoginConfigComponent.CONFIG_REGISTER_URL);
}
} else {
String authToken = new StringBuilder(channel).append(DOT).append(site.getId()).append(DOT).append(oauthAccess.getOpenId()).toString();
entity = new SysUserToken(authToken, site.getId(), user.getId(), channel, CommonUtils.getDate(), RequestUtils.getIpAddress(request));
sysUserTokenService.save(entity);
setUserToSession(session, user);
return REDIRECT + returnUrl;
}
}
}
} catch (IOException e) {
log.error(e);
}
}
return REDIRECT + site.getDynamicPath();
}
use of com.publiccms.view.pojo.oauth.OauthUser in project PublicCMS-preview by sanluan.
the class QQOauthComponent method getUserInfo.
/*
* http://wiki.connect.qq.com/get_user_info
*/
@Override
public OauthUser getUserInfo(short siteId, OauthAccess oauthAccess) throws ClientProtocolException, IOException {
OauthConfig config = getConfig(siteId);
if (null != oauthAccess && null != config) {
StringBuilder sb = new StringBuilder("https://graph.qq.com/user/get_user_info?access_token=");
sb.append(oauthAccess.getAccessToken()).append("&oauth_consumer_key=").append(config.getAppKey()).append("&openid=").append(oauthAccess.getOpenId()).append("&format=format");
String html = get(sb.toString());
if (CommonUtils.notEmpty(html)) {
Map<String, Object> map = objectMapper.readValue(html, new TypeReference<Map<String, Object>>() {
});
if (0 == (Integer) map.get("ret")) {
return new OauthUser(oauthAccess.getOpenId(), (String) map.get("nickname"), (String) map.get("figureurl_qq_2"), "男".equals(map.get("gender")) ? "m" : "f");
}
}
}
return null;
}
Aggregations