Search in sources :

Example 1 with HttpSingleSignOnService

use of small.danfer.sso.http.HttpSingleSignOnService 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", false, "", 0L, "", "", "DEFAULT", "", 999, false));
            // 添加默认权限
            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 (Exception 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) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) SingleSignOnException(small.danfer.sso.SingleSignOnException) 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)

Example 2 with HttpSingleSignOnService

use of small.danfer.sso.http.HttpSingleSignOnService in project pmph by BCSquad.

the class WeChatLoginController method load.

/**
 * <pre>
 * 功能描述:加载个人信息,此处添加了@OAuthRequired注解
 * 使用示范:
 *
 * @param request
 * @param model
 * @return
 * </pre>
 */
@RequestMapping(value = { "/login" })
public // @OAuthRequired
ModelAndView load(HttpServletRequest request, HttpServletResponse response) throws /*, Model model*/
Exception {
    // 判断是否从企业微信App登陆
    String userAgent = request.getHeader("user-agent").toLowerCase();
    Boolean isTrue = userAgent == null || userAgent.indexOf("micromessenger") == -1 ? false : true;
    PmphUser pmphUser = null;
    String username = null;
    String password = null;
    ModelAndView model = new ModelAndView();
    logger.info("login " + isTrue);
    if (isTrue) {
        HttpSession session = request.getSession();
        // userId 在session 中可以取到 微信--企业微信号 这个是pmph_user_wechat 表中的wechat_id
        String wechatUserId = (String) session.getAttribute("UserId");
        if (StringUtil.isEmpty(wechatUserId)) {
            // app 访问登录
            wechatUserId = request.getParameter("UserId");
        }
        String appType = request.getParameter("appType");
        // 微信--微信企业号直接访问app登录
        if (StringUtil.isEmpty(appType)) {
            /*微信 -- 待办 登录 首页*/
            if (StringUtil.isEmpty(wechatUserId)) {
                throw new CheckedServiceException(CheckedExceptionBusiness.USER_MANAGEMENT, CheckedExceptionResult.NULL_PARAM, "网络异常,请重新再试!");
            }
            model.addObject("UserId", wechatUserId);
            PmphUserWechat pmphUserWechat = pmphUserWechatService.getPmphUserWechatByWechatId(wechatUserId);
            if (ObjectUtil.isNull(pmphUserWechat)) {
                logger.info("wechatLoginController   " + ObjectUtil.isNull(pmphUserWechat));
                // 查找不到对应的社内用户 跳转登录页面
                model.addObject("isLogin", "0");
                // model.addObject("sessionPmphUser", "1");
                // model.addObject("pmphUserPermissionIds", "1");
                // commission 从微信 -- 企业微信号 代办
                model.addObject("isIndexOrCommission", ((!StringUtil.isEmpty((String) session.getAttribute("UserId")) && !StringUtil.isEmpty(request.getParameter("commission"))) ? "commission" : ""));
                // if ((!StringUtil.isEmpty((String) session.getAttribute("UserId")) && !StringUtil.isEmpty(request.getParameter("commission")))) {
                /*logger.info("http://medu.ipmph.com/wx/#/login?wechatUserId=" + wechatUserId+"&isIndexOrCommission="+((!StringUtil.isEmpty((String)session.getAttribute("UserId"))&&!StringUtil.isEmpty(request.getParameter("commission")))?"commission":""));
                    response.sendRedirect("http://medu.ipmph.com/wx/#/login?wechatUserId=" + wechatUserId+"&isIndexOrCommission=");*/
                // }
                model.setViewName("wechat");
                return model;
            } else {
                // 查找到对应的社内用户,跳转到首页
                pmphUser = pmphUserService.getPmphUserByUsername(pmphUserWechat.getUsername(), pmphUserWechat.getUserid());
                if (ObjectUtil.notNull(pmphUser)) {
                    username = new DesRun(null, pmphUserWechat.getUsername()).enpsw;
                    password = pmphUser.getPassword();
                    model.addObject(Const.PMPH_WECHAT_USER_TOKEN, new DesRun(password, username + password + wechatUserId + "<pmpheep>").enpsw);
                    model.addObject("username", username);
                    model.addObject("password", password);
                    if (StringUtil.isEmpty(request.getParameter("commission"))) {
                        // 跳转到首页
                        model.addObject("isLogin", "1");
                    } else {
                        // 跳转到代办页面
                        model.addObject("isLogin", "5");
                    }
                }
            }
        } else {
            /*微信 -- 申报等页面*/
            if (StringUtil.isEmpty(wechatUserId)) {
                throw new CheckedServiceException(CheckedExceptionBusiness.USER_MANAGEMENT, CheckedExceptionResult.NULL_PARAM, "网络异常,请重新再试!");
            }
            model.addObject("UserId", wechatUserId);
            PmphUserWechat pmphUserWechat = pmphUserWechatService.getPmphUserWechatByWechatId(wechatUserId);
            if (ObjectUtil.isNull(pmphUserWechat)) {
                pmphUser = pmphUserService.getPmphUserByOpenid(wechatUserId);
                if (ObjectUtil.notNull(pmphUser)) {
                    username = new DesRun(null, pmphUser.getUsername()).enpsw;
                    password = pmphUser.getPassword();
                    model.addObject(Const.PMPH_WECHAT_USER_TOKEN, new DesRun(password, username + password + wechatUserId + "<pmpheep>").enpsw);
                    model.addObject("username", username);
                    model.addObject("password", password);
                    model.addObject("isLogin", "3");
                }
            } else {
                pmphUser = pmphUserService.getPmphUserByUsername(pmphUserWechat.getUsername(), pmphUserWechat.getUserid());
                if (ObjectUtil.notNull(pmphUser)) {
                    username = new DesRun(null, pmphUserWechat.getUsername()).enpsw;
                    password = pmphUser.getPassword();
                    model.addObject(Const.PMPH_WECHAT_USER_TOKEN, new DesRun(password, username + password + wechatUserId + "<pmpheep>").enpsw);
                    model.addObject("username", username);
                    model.addObject("password", password);
                    model.addObject("isLogin", "4");
                }
            }
            /*跳转到某个具体的页面*/
            if ("1".equals(appType)) {
                // 教材审核
                String materialId = request.getParameter("materialId");
                String declarationId = request.getParameter("declarationId");
                model.addObject("materialId", materialId);
                model.addObject("declarationId", declarationId);
            } else if ("2".equals(appType)) {
            // 选题申报
            } else if ("3".equals(appType)) {
                // 图书纠错
                String bookName = request.getParameter("bookName");
                String type = request.getParameter("type");
                String id = request.getParameter("id");
                model.addObject("bookName", bookName);
                model.addObject("type", type);
                model.addObject("id", id);
            }
            model.addObject("appType", appType);
        }
    } else {
        // SSO 登陆
        model.addObject("isLogin", "2");
        HttpSingleSignOnService service = new HttpSingleSignOnService();
        try {
            LocalPrincipal principal = service.singleSignOn(request);
            String localPass = principal.getLocalPass();
            String userName = principal.getName();
            // String userName = "liub";
            assert userName != null;
            pmphUser = pmphUserService.getPmphUserByUsername(userName, null);
            // Map map = ssoHelper.getUserInfo(userName,"123456");
            // 预留
            Map map = new HashMap();
            if (ObjectUtil.isNull(pmphUser)) {
                // 为空就新建一个用户
                String pass = "888888";
                if (localPass != null) {
                    pass = localPass;
                }
                pmphUser = pmphUserService.add(new PmphUser(userName, pass, false, MapUtils.getString(map, "RealName", ""), 0L, MapUtils.getString(map, "Mobile", ""), MapUtils.getString(map, "Emial", ""), "DEFAULT", "", 999, false));
                // 添加默认权限
                pmphRoleService.addUserRole(pmphUser.getId(), 2L);
            }
            username = new DesRun(null, pmphUser.getUsername()).enpsw;
            password = pmphUser.getPassword();
            String wechatUserId = "sso";
            model.addObject("username", username);
            model.addObject("password", password);
            model.addObject("UserId", wechatUserId);
            model.addObject(Const.PMPH_WECHAT_USER_TOKEN, new DesRun(password, username + password + wechatUserId + "<pmpheep>").enpsw);
        } catch (Exception e) {
            logger.error("SSO登陆失败,异常信息'{}'", e.getMessage());
        }
    }
    /*--------------------------- 登录权限控制 session cookie ------------为了直接跳转到某个vue页面---------------------*/
    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());
    if (pmphRoles.isEmpty()) {
        // 添加默认权限
        pmphRoleService.addUserRole(pmphUser.getId(), 2L);
        pmphRoles = pmphRoleService.getPmphRoleByUserId(pmphUser.getId());
    }
    List<Long> roleIds = new ArrayList<Long>();
    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());
    request.getSession().setAttribute(Const.SESSION_PMPH_USER, pmphUser);
    // 验证成功在Session中保存用户Token信息
    request.getSession().setAttribute(Const.SEESION_PMPH_USER_TOKEN, new DesRun(password, username).enpsw);
    model.addObject(Const.USER_SEESION_ID, request.getSession().getId());
    model.addObject(Const.SESSION_PMPH_USER, JSON.toJSON(pmphUser));
    model.addObject(Const.SEESION_PMPH_USER_TOKEN, new DesRun(password, username).enpsw);
    model.addObject("pmphUserPermissionIds", pmphUserPermissionIds);
    /*---------------------------------------------------------------*/
    model.setViewName("wechat");
    return model;
}
Also used : PmphUser(com.bc.pmpheep.back.po.PmphUser) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) ModelAndView(org.springframework.web.servlet.ModelAndView) HttpSingleSignOnService(small.danfer.sso.http.HttpSingleSignOnService) ArrayList(java.util.ArrayList) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) PmphUserWechat(com.bc.pmpheep.back.po.PmphUserWechat) LocalPrincipal(small.danfer.sso.assertion.LocalPrincipal) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) IOException(java.io.IOException) PmphRole(com.bc.pmpheep.back.po.PmphRole) HashMap(java.util.HashMap) Map(java.util.Map) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

PmphRole (com.bc.pmpheep.back.po.PmphRole)2 PmphUser (com.bc.pmpheep.back.po.PmphUser)2 CheckedServiceException (com.bc.pmpheep.service.exception.CheckedServiceException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 HttpSingleSignOnService (small.danfer.sso.http.HttpSingleSignOnService)2 PmphUserWechat (com.bc.pmpheep.back.po.PmphUserWechat)1 DesRun (com.bc.pmpheep.back.util.DesRun)1 ResponseBean (com.bc.pmpheep.controller.bean.ResponseBean)1 IOException (java.io.IOException)1 Principal (java.security.Principal)1 Map (java.util.Map)1 HttpSession (javax.servlet.http.HttpSession)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1 SingleSignOnException (small.danfer.sso.SingleSignOnException)1 LocalPrincipal (small.danfer.sso.assertion.LocalPrincipal)1