use of small.danfer.sso.SingleSignOnException in project pmph by BCSquad.
the class PmphLoginController method ssoLogin.
/**
* <pre>
* 功能描述:SSO登陆
* 使用示范:
*
* @param request
* @return
* </pre>
*/
@ResponseBody
@RequestMapping(value = "/sso", method = RequestMethod.GET)
public ResponseBean ssoLogin(HttpServletRequest request, HttpServletResponse response) {
String sessionId = CookiesUtil.getSessionId(request);
PmphUser pmUser = SessionUtil.getPmphUserBySessionId(sessionId);
if (ObjectUtil.isNull(pmUser)) {
throw new CheckedServiceException(CheckedExceptionBusiness.MESSAGE, CheckedExceptionResult.NULL_PARAM, "用户为空");
}
Map<String, Object> resultMap = new HashMap<String, Object>();
HttpSingleSignOnService service = new HttpSingleSignOnService();
// String url = service.getSingleSignOnURL();
try {
Principal principal = service.singleSignOn(request);
String userName = principal.getName();
PmphUser pmphUser = pmphUserService.login(userName, null);
if (ObjectUtil.isNull(pmphUser)) {
// 为空就新建一个用户
pmphUser = pmphUserService.add(new PmphUser(userName, "888888", userName, "DEFAULT"));
// 添加默认权限
pmphRoleService.addUserRole(pmphUser.getId(), 2L);
}
pmphUser.setLoginType(Const.LOGIN_TYPE_PMPH);
if (!RouteUtil.DEFAULT_USER_AVATAR.equals(pmphUser.getAvatar())) {
pmphUser.setAvatar(RouteUtil.userAvatar(pmphUser.getAvatar()));
}
// 根据用户Id查询对应角色(是否为管理员)
List<PmphRole> pmphRoles = pmphRoleService.getPmphRoleByUserId(pmphUser.getId());
List<Long> roleIds = new ArrayList<Long>(pmphRoles.size());
for (PmphRole pmphRole : pmphRoles) {
roleIds.add(pmphRole.getId());
if (ObjectUtil.notNull(pmphRole)) {
if (Const.LOGIN_USER_IS_ADMIN.equals(pmphRole.getRoleName()) || Const.LOGIN_USER_IS_ADMINS.equals(pmphRole.getRoleName()) || Const.LOGIN_SYS_USER_IS_ADMIN.equals(pmphRole.getRoleName())) {
pmphUser.setIsAdmin(true);
} else {
pmphUser.setIsAdmin(false);
}
}
if (Const.TRUE == pmphUser.getIsAdmin()) {
break;
}
}
// 根据用户Id查询对应权限Id
List<Long> pmphUserPermissionIds = pmphUserService.getPmphUserPermissionByUserId(pmphUser.getId());
// 验证成功在Session中保存用户信息
request.getSession().setAttribute(Const.SESSION_PMPH_USER, pmphUser);
// 验证成功在Session中保存用户Token信息
request.getSession().setAttribute(Const.SEESION_PMPH_USER_TOKEN, new DesRun(userName, userName).enpsw);
// pmphUserSessionId
resultMap.put(Const.USER_SEESION_ID, request.getSession().getId());
resultMap.put(Const.SESSION_PMPH_USER, pmphUser);
resultMap.put(Const.SEESION_PMPH_USER_TOKEN, new DesRun(userName, userName).enpsw);
resultMap.put("pmphUserPermissionIds", pmphUserPermissionIds);
return new ResponseBean(resultMap);
} catch (SingleSignOnException e) {
return new ResponseBean(e);
}
}
Aggregations