Search in sources :

Example 1 with SingleSignOnException

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);
    }
}
Also used : PmphUser(com.bc.pmpheep.back.po.PmphUser) HashMap(java.util.HashMap) HttpSingleSignOnService(small.danfer.sso.http.HttpSingleSignOnService) ArrayList(java.util.ArrayList) SingleSignOnException(small.danfer.sso.SingleSignOnException) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) PmphRole(com.bc.pmpheep.back.po.PmphRole) DesRun(com.bc.pmpheep.back.util.DesRun) ResponseBean(com.bc.pmpheep.controller.bean.ResponseBean) Principal(java.security.Principal) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

PmphRole (com.bc.pmpheep.back.po.PmphRole)1 PmphUser (com.bc.pmpheep.back.po.PmphUser)1 DesRun (com.bc.pmpheep.back.util.DesRun)1 ResponseBean (com.bc.pmpheep.controller.bean.ResponseBean)1 CheckedServiceException (com.bc.pmpheep.service.exception.CheckedServiceException)1 Principal (java.security.Principal)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 SingleSignOnException (small.danfer.sso.SingleSignOnException)1 HttpSingleSignOnService (small.danfer.sso.http.HttpSingleSignOnService)1