Java Code Examples for org.jeecg.common.system.util.JwtUtil#sign()

The following examples show how to use org.jeecg.common.system.util.JwtUtil#sign() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShiroRealm.java    From jeecg-cloud with Apache License 2.0 6 votes vote down vote up
/**
	 * JWTToken刷新生命周期 (实现: 用户在线操作不掉线功能)
	 * 1、登录成功后将用户的JWT生成的Token作为k、v存储到cache缓存里面(这时候k、v值一样),缓存有效期设置为Jwt有效时间的2倍
	 * 2、当该用户再次请求时,通过JWTFilter层层校验之后会进入到doGetAuthenticationInfo进行身份验证
	 * 3、当该用户这次请求jwt生成的token值已经超时,但该token对应cache中的k还是存在,则表示该用户一直在操作只是JWT的token失效了,程序会给token对应的k映射的v值重新生成JWTToken并覆盖v值,该缓存生命周期重新计算
	 * 4、当该用户这次请求jwt在生成的token值已经超时,并在cache中不存在对应的k,则表示该用户账户空闲超时,返回用户信息已失效,请重新登录。
	 * 注意: 前端请求Header中设置Authorization保持不变,校验有效性以缓存中的token为准。
     *       用户过期时间 = Jwt有效时间 * 2。
	 *
	 * @param userName
	 * @param passWord
	 * @return
	 */
	public boolean jwtTokenRefresh(String token, String userName, String passWord) {
		String cacheToken = String.valueOf(redisUtil.get(CommonConstant.PREFIX_USER_TOKEN + token));
		if (oConvertUtils.isNotEmpty(cacheToken)) {
			// 校验token有效性
			if (!JwtUtil.verify(cacheToken, userName, passWord)) {
				String newAuthorization = JwtUtil.sign(userName, passWord);
				// 设置超时时间
				redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, newAuthorization);
				redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME *2 / 1000);
                log.info("——————————用户在线操作,更新token保证不掉线—————————jwtTokenRefresh——————— "+ token);
			}
            //update-begin--Author:scott  Date:20191005  for:解决每次请求,都重写redis中 token缓存问题
//			else {
//				// 设置超时时间
//				redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, cacheToken);
//				redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 1000);
//			}
            //update-end--Author:scott  Date:20191005   for:解决每次请求,都重写redis中 token缓存问题
			return true;
		}
		return false;
	}
 
Example 2
Source File: TokenUtils.java    From jeecg-boot with Apache License 2.0 6 votes vote down vote up
/**
     * 刷新token(保证用户在线操作不掉线)
     * @param token
     * @param userName
     * @param passWord
     * @param redisUtil
     * @return
     */
    private static boolean jwtTokenRefresh(String token, String userName, String passWord, RedisUtil redisUtil) {
        String cacheToken = String.valueOf(redisUtil.get(CommonConstant.PREFIX_USER_TOKEN + token));
        if (oConvertUtils.isNotEmpty(cacheToken)) {
            // 校验token有效性
            if (!JwtUtil.verify(cacheToken, userName, passWord)) {
                String newAuthorization = JwtUtil.sign(userName, passWord);
                // 设置Toekn缓存有效时间
                redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, newAuthorization);
                redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
            }
            //update-begin--Author:scott  Date:20191005  for:解决每次请求,都重写redis中 token缓存问题
//            else {
//                redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, cacheToken);
//                // 设置超时时间
//                redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 1000);
//            }
            //update-end--Author:scott  Date:20191005  for:解决每次请求,都重写redis中 token缓存问题
            return true;
        }
        return false;
    }
 
Example 3
Source File: TokenUtils.java    From jeecg-cloud with Apache License 2.0 6 votes vote down vote up
/**
     * 刷新token(保证用户在线操作不掉线)
     * @param token
     * @param userName
     * @param passWord
     * @param redisUtil
     * @return
     */
    private static boolean jwtTokenRefresh(String token, String userName, String passWord, RedisUtil redisUtil) {
        String cacheToken = String.valueOf(redisUtil.get(CommonConstant.PREFIX_USER_TOKEN + token));
        if (oConvertUtils.isNotEmpty(cacheToken)) {
            // 校验token有效性
            if (!JwtUtil.verify(cacheToken, userName, passWord)) {
                String newAuthorization = JwtUtil.sign(userName, passWord);
                // 设置Toekn缓存有效时间
                redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, newAuthorization);
                redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
            }
            //update-begin--Author:scott  Date:20191005  for:解决每次请求,都重写redis中 token缓存问题
//            else {
//                redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, cacheToken);
//                // 设置超时时间
//                redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 1000);
//            }
            //update-end--Author:scott  Date:20191005  for:解决每次请求,都重写redis中 token缓存问题
            return true;
        }
        return false;
    }
 
Example 4
Source File: TokenUtils.java    From jeecg-boot-with-activiti with MIT License 6 votes vote down vote up
/**
     * 刷新token(保证用户在线操作不掉线)
     * @param token
     * @param userName
     * @param passWord
     * @param redisUtil
     * @return
     */
    private static boolean jwtTokenRefresh(String token, String userName, String passWord, RedisUtil redisUtil) {
        String cacheToken = String.valueOf(redisUtil.get(CommonConstant.PREFIX_USER_TOKEN + token));
        if (oConvertUtils.isNotEmpty(cacheToken)) {
            // 校验token有效性
            if (!JwtUtil.verify(cacheToken, userName, passWord)) {
                String newAuthorization = JwtUtil.sign(userName, passWord);
                // 设置Toekn缓存有效时间
                redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, newAuthorization);
                redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
            }
            //update-begin--Author:scott  Date:20191005  for:解决每次请求,都重写redis中 token缓存问题
//            else {
//                redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, cacheToken);
//                // 设置超时时间
//                redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 1000);
//            }
            //update-end--Author:scott  Date:20191005  for:解决每次请求,都重写redis中 token缓存问题
            return true;
        }
        return false;
    }
 
Example 5
Source File: ShiroRealm.java    From jeecg-boot with Apache License 2.0 6 votes vote down vote up
/**
	 * JWTToken刷新生命周期 (实现: 用户在线操作不掉线功能)
	 * 1、登录成功后将用户的JWT生成的Token作为k、v存储到cache缓存里面(这时候k、v值一样),缓存有效期设置为Jwt有效时间的2倍
	 * 2、当该用户再次请求时,通过JWTFilter层层校验之后会进入到doGetAuthenticationInfo进行身份验证
	 * 3、当该用户这次请求jwt生成的token值已经超时,但该token对应cache中的k还是存在,则表示该用户一直在操作只是JWT的token失效了,程序会给token对应的k映射的v值重新生成JWTToken并覆盖v值,该缓存生命周期重新计算
	 * 4、当该用户这次请求jwt在生成的token值已经超时,并在cache中不存在对应的k,则表示该用户账户空闲超时,返回用户信息已失效,请重新登录。
	 * 注意: 前端请求Header中设置Authorization保持不变,校验有效性以缓存中的token为准。
     *       用户过期时间 = Jwt有效时间 * 2。
	 *
	 * @param userName
	 * @param passWord
	 * @return
	 */
	public boolean jwtTokenRefresh(String token, String userName, String passWord) {
		String cacheToken = String.valueOf(redisUtil.get(CommonConstant.PREFIX_USER_TOKEN + token));
		if (oConvertUtils.isNotEmpty(cacheToken)) {
			// 校验token有效性
			if (!JwtUtil.verify(cacheToken, userName, passWord)) {
				String newAuthorization = JwtUtil.sign(userName, passWord);
				// 设置超时时间
				redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, newAuthorization);
				redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME *2 / 1000);
                log.info("——————————用户在线操作,更新token保证不掉线—————————jwtTokenRefresh——————— "+ token);
			}
            //update-begin--Author:scott  Date:20191005  for:解决每次请求,都重写redis中 token缓存问题
//			else {
//				// 设置超时时间
//				redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, cacheToken);
//				redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 1000);
//			}
            //update-end--Author:scott  Date:20191005   for:解决每次请求,都重写redis中 token缓存问题
			return true;
		}
		return false;
	}
 
Example 6
Source File: TokenUtils.java    From teaching with Apache License 2.0 6 votes vote down vote up
/**
     * 刷新token(保证用户在线操作不掉线)
     * @param token
     * @param userName
     * @param passWord
     * @param redisUtil
     * @return
     */
    private static boolean jwtTokenRefresh(String token, String userName, String passWord, RedisUtil redisUtil) {
        String cacheToken = String.valueOf(redisUtil.get(CommonConstant.PREFIX_USER_TOKEN + token));
        if (oConvertUtils.isNotEmpty(cacheToken)) {
            // 校验token有效性
            if (!JwtUtil.verify(cacheToken, userName, passWord)) {
                String newAuthorization = JwtUtil.sign(userName, passWord);
                // 设置Toekn缓存有效时间
                redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, newAuthorization);
                redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
            }
            //update-begin--Author:scott  Date:20191005  for:解决每次请求,都重写redis中 token缓存问题
//            else {
//                redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, cacheToken);
//                // 设置超时时间
//                redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 1000);
//            }
            //update-end--Author:scott  Date:20191005  for:解决每次请求,都重写redis中 token缓存问题
            return true;
        }
        return false;
    }
 
Example 7
Source File: ShiroRealm.java    From teaching with Apache License 2.0 6 votes vote down vote up
/**
	 * JWTToken刷新生命周期 (实现: 用户在线操作不掉线功能)
	 * 1、登录成功后将用户的JWT生成的Token作为k、v存储到cache缓存里面(这时候k、v值一样),缓存有效期设置为Jwt有效时间的2倍
	 * 2、当该用户再次请求时,通过JWTFilter层层校验之后会进入到doGetAuthenticationInfo进行身份验证
	 * 3、当该用户这次请求jwt生成的token值已经超时,但该token对应cache中的k还是存在,则表示该用户一直在操作只是JWT的token失效了,程序会给token对应的k映射的v值重新生成JWTToken并覆盖v值,该缓存生命周期重新计算
	 * 4、当该用户这次请求jwt在生成的token值已经超时,并在cache中不存在对应的k,则表示该用户账户空闲超时,返回用户信息已失效,请重新登录。
	 * 注意: 前端请求Header中设置Authorization保持不变,校验有效性以缓存中的token为准。
     *       用户过期时间 = Jwt有效时间 * 2。
	 *
	 * @param userName
	 * @param passWord
	 * @return
	 */
	public boolean jwtTokenRefresh(String token, String userName, String passWord) {
		String cacheToken = String.valueOf(redisUtil.get(CommonConstant.PREFIX_USER_TOKEN + token));
		if (oConvertUtils.isNotEmpty(cacheToken)) {
			// 校验token有效性
			if (!JwtUtil.verify(cacheToken, userName, passWord)) {
				String newAuthorization = JwtUtil.sign(userName, passWord);
				// 设置超时时间
				redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, newAuthorization);
				redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME *2 / 1000);
                log.info("——————————用户在线操作,更新token保证不掉线—————————jwtTokenRefresh——————— "+ token);
			}
            //update-begin--Author:scott  Date:20191005  for:解决每次请求,都重写redis中 token缓存问题
//			else {
//				// 设置超时时间
//				redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, cacheToken);
//				redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 1000);
//			}
            //update-end--Author:scott  Date:20191005   for:解决每次请求,都重写redis中 token缓存问题
			return true;
		}
		return false;
	}
 
Example 8
Source File: LoginController.java    From jeecg-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 用户信息
 *
 * @param sysUser
 * @param result
 * @return
 */
private Result<JSONObject> userInfo(SysUser sysUser, Result<JSONObject> result) {
	String syspassword = sysUser.getPassword();
	String username = sysUser.getUsername();
	// 生成token
	String token = JwtUtil.sign(username, syspassword);
       // 设置token缓存有效时间
	redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
	redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);

	// 获取用户部门信息
	JSONObject obj = new JSONObject();
	List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
	obj.put("departs", departs);
	if (departs == null || departs.size() == 0) {
		obj.put("multi_depart", 0);
	} else if (departs.size() == 1) {
		sysUserService.updateUserDepart(username, departs.get(0).getOrgCode());
		obj.put("multi_depart", 1);
	} else {
		obj.put("multi_depart", 2);
	}
	obj.put("token", token);
	obj.put("userInfo", sysUser);
	obj.put("sysAllDictItems", sysDictService.queryAllDictItems());
	result.setResult(obj);
	result.success("登录成功");
	return result;
}
 
Example 9
Source File: LoginController.java    From jeecg-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 用户信息
 *
 * @param sysUser
 * @param result
 * @return
 */
private Result<JSONObject> userInfo(SysUser sysUser, Result<JSONObject> result) {
	String syspassword = sysUser.getPassword();
	String username = sysUser.getUsername();
	// 生成token
	String token = JwtUtil.sign(username, syspassword);
       // 设置token缓存有效时间
	redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);

	//------------------------------------------------------------------------------------------
	LoginUser vo = new LoginUser();
	BeanUtils.copyProperties(sysUser,vo);
	vo.setPassword(SecureUtil.md5(sysUser.getPassword()));
	redisUtil.set(CacheConstant.SYS_USERS_CACHE_JWT +":" +token, vo);
	//------------------------------------------------------------------------------------------
	redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);

	// 获取用户部门信息
	JSONObject obj = new JSONObject();
	List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
	obj.put("departs", departs);
	if (departs == null || departs.size() == 0) {
		obj.put("multi_depart", 0);
	} else if (departs.size() == 1) {
		sysUserService.updateUserDepart(username, departs.get(0).getOrgCode());
		obj.put("multi_depart", 1);
	} else {
		obj.put("multi_depart", 2);
	}
	obj.put("token", token);
	obj.put("userInfo", sysUser);
	obj.put("sysAllDictItems", sysDictService.queryAllDictItems());
	result.setResult(obj);
	result.success("登录成功");
	return result;
}
 
Example 10
Source File: TestWebController.java    From jeecg-boot with Apache License 2.0 5 votes vote down vote up
@PostMapping("/login")
public ResponseBean login(@RequestParam("username") String username,
                          @RequestParam("password") String password) {
	SysUser user = userService.getUserByName(username);
	if(user==null) {
		return new ResponseBean(200, "用户不存在!", JwtUtil.sign(username, user.getPassword()));
	}
	String passwordEncode = PasswordUtil.encrypt(username, password, user.getSalt());
    if (passwordEncode.equals(user.getPassword())) {
        return new ResponseBean(200, "Login success", JwtUtil.sign(username, user.getPassword()));
    } else {
        throw new UnauthorizedException();
    }
}
 
Example 11
Source File: LoginController.java    From jeecg-boot-with-activiti with MIT License 5 votes vote down vote up
/**
 * 用户信息
 *
 * @param sysUser
 * @param result
 * @return
 */
private Result<JSONObject> userInfo(SysUser sysUser, Result<JSONObject> result) {
	String syspassword = sysUser.getPassword();
	String username = sysUser.getUsername();
	// 生成token
	String token = JwtUtil.sign(username, syspassword);
       // 设置token缓存有效时间
	redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
	redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);

	// 获取用户部门信息
	JSONObject obj = new JSONObject();
	List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
	obj.put("departs", departs);
	if (departs == null || departs.size() == 0) {
		obj.put("multi_depart", 0);
	} else if (departs.size() == 1) {
		sysUserService.updateUserDepart(username, departs.get(0).getOrgCode());
		obj.put("multi_depart", 1);
	} else {
		obj.put("multi_depart", 2);
	}
	obj.put("token", token);
	obj.put("userInfo", sysUser);
	result.setResult(obj);
	result.success("登录成功");
	return result;
}
 
Example 12
Source File: LoginController.java    From teaching with Apache License 2.0 5 votes vote down vote up
/**
 * 用户信息
 *
 * @param sysUser
 * @param result
 * @return
 */
private Result<JSONObject> userInfo(SysUser sysUser, Result<JSONObject> result) {
	String syspassword = sysUser.getPassword();
	String username = sysUser.getUsername();
	// 生成token
	String token = JwtUtil.sign(username, syspassword);
       // 设置token缓存有效时间
	redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
	redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);

	// 获取用户部门信息
	JSONObject obj = new JSONObject();
	List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
	obj.put("departs", departs);
	if (departs == null || departs.size() == 0) {
		obj.put("multi_depart", 0);
	} else if (departs.size() == 1) {
		sysUserService.updateUserDepart(username, departs.get(0).getOrgCode());
		obj.put("multi_depart", 1);
	} else {
		obj.put("multi_depart", 2);
	}
	obj.put("token", token);
	obj.put("userInfo", sysUser);
	result.setResult(obj);
	result.success("登录成功");
	return result;
}
 
Example 13
Source File: TestWebController.java    From teaching with Apache License 2.0 5 votes vote down vote up
@PostMapping("/login")
public ResponseBean login(@RequestParam("username") String username,
                          @RequestParam("password") String password) {
	SysUser user = userService.getUserByName(username);
	if(user==null) {
		return new ResponseBean(200, "用户不存在!", JwtUtil.sign(username, user.getPassword()));
	}
	String passwordEncode = PasswordUtil.encrypt(username, password, user.getSalt());
    if (passwordEncode.equals(user.getPassword())) {
        return new ResponseBean(200, "Login success", JwtUtil.sign(username, user.getPassword()));
    } else {
        throw new UnauthorizedException();
    }
}
 
Example 14
Source File: CasClientController.java    From jeecg-boot with Apache License 2.0 4 votes vote down vote up
@GetMapping("/validateLogin")
public Object validateLogin(@RequestParam(name="ticket") String ticket,
							@RequestParam(name="service") String service,
							HttpServletRequest request,
							HttpServletResponse response) throws Exception {
	Result<JSONObject> result = new Result<JSONObject>();
	log.info("Rest api login.");
	try {
		String validateUrl = prefixUrl+"/p3/serviceValidate";
		String res = CASServiceUtil.getSTValidate(validateUrl, ticket, service);
		log.info("res."+res);
		final String error = XmlUtils.getTextForElement(res, "authenticationFailure");
		if(StringUtils.isNotEmpty(error)) {
			throw new Exception(error);
		}
		final String principal = XmlUtils.getTextForElement(res, "user");
		if (StringUtils.isEmpty(principal)) {
            throw new Exception("No principal was found in the response from the CAS server.");
        }
		log.info("-------token----username---"+principal);
	    //1. 校验用户是否有效
  		SysUser sysUser = sysUserService.getUserByName(principal);
  		result = sysUserService.checkUserIsEffective(sysUser);
  		if(!result.isSuccess()) {
  			return result;
  		}
 		String token = JwtUtil.sign(sysUser.getUsername(), sysUser.getPassword());
 		// 设置超时时间
 		redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
 		redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
  		//获取用户部门信息
		JSONObject obj = new JSONObject();
		List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
		obj.put("departs", departs);
		if (departs == null || departs.size() == 0) {
			obj.put("multi_depart", 0);
		} else if (departs.size() == 1) {
			sysUserService.updateUserDepart(principal, departs.get(0).getOrgCode());
			obj.put("multi_depart", 1);
		} else {
			obj.put("multi_depart", 2);
		}
		obj.put("token", token);
		obj.put("userInfo", sysUser);
		result.setResult(obj);
		result.success("登录成功");
  		
	} catch (Exception e) {
		//e.printStackTrace();
		result.error500(e.getMessage());
	}
	return new HttpEntity<>(result);
}
 
Example 15
Source File: LoginController.java    From teaching with Apache License 2.0 4 votes vote down vote up
/**
 * app登录
 * @param sysLoginModel
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/mLogin", method = RequestMethod.POST)
public Result<JSONObject> mLogin(@RequestBody SysLoginModel sysLoginModel) throws Exception {
	Result<JSONObject> result = new Result<JSONObject>();
	String username = sysLoginModel.getUsername();
	String password = sysLoginModel.getPassword();
	
	//1. 校验用户是否有效
	SysUser sysUser = sysUserService.getUserByName(username);
	result = sysUserService.checkUserIsEffective(sysUser);
	if(!result.isSuccess()) {
		return result;
	}
	
	//2. 校验用户名或密码是否正确
	String userpassword = PasswordUtil.encrypt(username, password, sysUser.getSalt());
	String syspassword = sysUser.getPassword();
	if (!syspassword.equals(userpassword)) {
		result.error500("用户名或密码错误");
		return result;
	}
	
	String orgCode = sysUser.getOrgCode();
	if(oConvertUtils.isEmpty(orgCode)) {
		//如果当前用户无选择部门 查看部门关联信息
		List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
		if (departs == null || departs.size() == 0) {
			result.error500("用户暂未归属部门,不可登录!");
			return result;
		}
		orgCode = departs.get(0).getOrgCode();
		sysUser.setOrgCode(orgCode);
		this.sysUserService.updateUserDepart(username, orgCode);
	}
	JSONObject obj = new JSONObject();
	//用户登录信息
	obj.put("userInfo", sysUser);
	
	// 生成token
	String token = JwtUtil.sign(username, syspassword);
	// 设置超时时间
	redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
	redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
	//token 信息
	obj.put("token", token);
	result.setResult(obj);
	result.setSuccess(true);
	result.setCode(200);
	sysBaseAPI.addLog("用户名: " + username + ",登录成功[移动端]!", CommonConstant.LOG_TYPE_1, null);
	return result;
}
 
Example 16
Source File: CasClientController.java    From teaching with Apache License 2.0 4 votes vote down vote up
@GetMapping("/validateLogin")
public Object validateLogin(@RequestParam(name="ticket") String ticket,
							@RequestParam(name="service") String service,
							HttpServletRequest request,
							HttpServletResponse response) throws Exception {
	Result<JSONObject> result = new Result<JSONObject>();
	log.info("Rest api login.");
	try {
		String validateUrl = prefixUrl+"/p3/serviceValidate";
		String res = CASServiceUtil.getSTValidate(validateUrl, ticket, service);
		log.info("res."+res);
		final String error = XmlUtils.getTextForElement(res, "authenticationFailure");
		if(StringUtils.isNotEmpty(error)) {
			throw new Exception(error);
		}
		final String principal = XmlUtils.getTextForElement(res, "user");
		if (StringUtils.isEmpty(principal)) {
            throw new Exception("No principal was found in the response from the CAS server.");
        }
		log.info("-------token----username---"+principal);
	    //1. 校验用户是否有效
  		SysUser sysUser = sysUserService.getUserByName(principal);
  		result = sysUserService.checkUserIsEffective(sysUser);
  		if(!result.isSuccess()) {
  			return result;
  		}
 		String token = JwtUtil.sign(sysUser.getUsername(), sysUser.getPassword());
 		// 设置超时时间
 		redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
 		redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
  		//获取用户部门信息
		JSONObject obj = new JSONObject();
		List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
		obj.put("departs", departs);
		if (departs == null || departs.size() == 0) {
			obj.put("multi_depart", 0);
		} else if (departs.size() == 1) {
			sysUserService.updateUserDepart(principal, departs.get(0).getOrgCode());
			obj.put("multi_depart", 1);
		} else {
			obj.put("multi_depart", 2);
		}
		obj.put("token", token);
		obj.put("userInfo", sysUser);
		result.setResult(obj);
		result.success("登录成功");
  		
	} catch (Exception e) {
		//e.printStackTrace();
		result.error500(e.getMessage());
	}
	return new HttpEntity<>(result);
}
 
Example 17
Source File: LoginController.java    From jeecg-boot-with-activiti with MIT License 4 votes vote down vote up
/**
 * app登录
 * @param sysLoginModel
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/mLogin", method = RequestMethod.POST)
public Result<JSONObject> mLogin(@RequestBody SysLoginModel sysLoginModel) throws Exception {
	Result<JSONObject> result = new Result<JSONObject>();
	String username = sysLoginModel.getUsername();
	String password = sysLoginModel.getPassword();
	
	//1. 校验用户是否有效
	SysUser sysUser = sysUserService.getUserByName(username);
	result = sysUserService.checkUserIsEffective(sysUser);
	if(!result.isSuccess()) {
		return result;
	}
	
	//2. 校验用户名或密码是否正确
	String userpassword = PasswordUtil.encrypt(username, password, sysUser.getSalt());
	String syspassword = sysUser.getPassword();
	if (!syspassword.equals(userpassword)) {
		result.error500("用户名或密码错误");
		return result;
	}
	
	String orgCode = sysUser.getOrgCode();
	if(oConvertUtils.isEmpty(orgCode)) {
		//如果当前用户无选择部门 查看部门关联信息
		List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
		if (departs == null || departs.size() == 0) {
			result.error500("用户暂未归属部门,不可登录!");
			return result;
		}
		orgCode = departs.get(0).getOrgCode();
		sysUser.setOrgCode(orgCode);
		this.sysUserService.updateUserDepart(username, orgCode);
	}
	JSONObject obj = new JSONObject();
	//用户登录信息
	obj.put("userInfo", sysUser);
	
	// 生成token
	String token = JwtUtil.sign(username, syspassword);
	// 设置超时时间
	redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
	redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
	//token 信息
	obj.put("token", token);
	result.setResult(obj);
	result.setSuccess(true);
	result.setCode(200);
	sysBaseAPI.addLog("用户名: " + username + ",登录成功[移动端]!", CommonConstant.LOG_TYPE_1, null);
	return result;
}
 
Example 18
Source File: CasClientController.java    From jeecg-boot-with-activiti with MIT License 4 votes vote down vote up
@GetMapping("/validateLogin")
public Object validateLogin(@RequestParam(name="ticket") String ticket,
							@RequestParam(name="service") String service,
							HttpServletRequest request,
							HttpServletResponse response) throws Exception {
	Result<JSONObject> result = new Result<JSONObject>();
	log.info("Rest api login.");
	try {
		String validateUrl = prefixUrl+"/p3/serviceValidate";
		String res = CASServiceUtil.getSTValidate(validateUrl, ticket, service);
		log.info("res."+res);
		final String error = XmlUtils.getTextForElement(res, "authenticationFailure");
		if(StringUtils.isNotEmpty(error)) {
			throw new Exception(error);
		}
		final String principal = XmlUtils.getTextForElement(res, "user");
		if (StringUtils.isEmpty(principal)) {
            throw new Exception("No principal was found in the response from the CAS server.");
        }
		log.info("-------token----username---"+principal);
	    //1. 校验用户是否有效
  		SysUser sysUser = sysUserService.getUserByName(principal);
  		result = sysUserService.checkUserIsEffective(sysUser);
  		if(!result.isSuccess()) {
  			return result;
  		}
 		String token = JwtUtil.sign(sysUser.getUsername(), sysUser.getPassword());
 		// 设置超时时间
 		redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
 		redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
  		//获取用户部门信息
		JSONObject obj = new JSONObject();
		List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
		obj.put("departs", departs);
		if (departs == null || departs.size() == 0) {
			obj.put("multi_depart", 0);
		} else if (departs.size() == 1) {
			sysUserService.updateUserDepart(principal, departs.get(0).getOrgCode());
			obj.put("multi_depart", 1);
		} else {
			obj.put("multi_depart", 2);
		}
		obj.put("token", token);
		obj.put("userInfo", sysUser);
		result.setResult(obj);
		result.success("登录成功");
  		
	} catch (Exception e) {
		//e.printStackTrace();
		result.error500(e.getMessage());
	}
	return new HttpEntity<>(result);
}
 
Example 19
Source File: LoginController.java    From jeecg-cloud with Apache License 2.0 4 votes vote down vote up
/**
 * app登录
 * @param sysLoginModel
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/mLogin", method = RequestMethod.POST)
public Result<JSONObject> mLogin(@RequestBody SysLoginModel sysLoginModel) throws Exception {
	Result<JSONObject> result = new Result<JSONObject>();
	String username = sysLoginModel.getUsername();
	String password = sysLoginModel.getPassword();
	
	//1. 校验用户是否有效
	SysUser sysUser = sysUserService.getUserByName(username);
	result = sysUserService.checkUserIsEffective(sysUser);
	if(!result.isSuccess()) {
		return result;
	}
	
	//2. 校验用户名或密码是否正确
	String userpassword = PasswordUtil.encrypt(username, password, sysUser.getSalt());
	String syspassword = sysUser.getPassword();
	if (!syspassword.equals(userpassword)) {
		result.error500("用户名或密码错误");
		return result;
	}
	
	String orgCode = sysUser.getOrgCode();
	if(oConvertUtils.isEmpty(orgCode)) {
		//如果当前用户无选择部门 查看部门关联信息
		List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
		if (departs == null || departs.size() == 0) {
			result.error500("用户暂未归属部门,不可登录!");
			return result;
		}
		orgCode = departs.get(0).getOrgCode();
		sysUser.setOrgCode(orgCode);
		this.sysUserService.updateUserDepart(username, orgCode);
	}
	JSONObject obj = new JSONObject();
	//用户登录信息
	obj.put("userInfo", sysUser);
	
	// 生成token
	String token = JwtUtil.sign(username, syspassword);
	// 设置超时时间
	redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
	redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
	//token 信息
	obj.put("token", token);
	result.setResult(obj);
	result.setSuccess(true);
	result.setCode(200);
	sysBaseAPI.addLog("用户名: " + username + ",登录成功[移动端]!", CommonConstant.LOG_TYPE_1, null);
	return result;
}
 
Example 20
Source File: CasClientController.java    From jeecg-cloud with Apache License 2.0 4 votes vote down vote up
@GetMapping("/validateLogin")
public Object validateLogin(@RequestParam(name="ticket") String ticket,
							@RequestParam(name="service") String service,
							HttpServletRequest request,
							HttpServletResponse response) throws Exception {
	Result<JSONObject> result = new Result<JSONObject>();
	log.info("Rest api login.");
	try {
		String validateUrl = prefixUrl+"/p3/serviceValidate";
		String res = CASServiceUtil.getSTValidate(validateUrl, ticket, service);
		log.info("res."+res);
		final String error = XmlUtils.getTextForElement(res, "authenticationFailure");
		if(StringUtils.isNotEmpty(error)) {
			throw new Exception(error);
		}
		final String principal = XmlUtils.getTextForElement(res, "user");
		if (StringUtils.isEmpty(principal)) {
            throw new Exception("No principal was found in the response from the CAS server.");
        }
		log.info("-------token----username---"+principal);
	    //1. 校验用户是否有效
  		SysUser sysUser = sysUserService.getUserByName(principal);
  		result = sysUserService.checkUserIsEffective(sysUser);
  		if(!result.isSuccess()) {
  			return result;
  		}
 		String token = JwtUtil.sign(sysUser.getUsername(), sysUser.getPassword());
 		// 设置超时时间
 		redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
 		redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
  		//获取用户部门信息
		JSONObject obj = new JSONObject();
		List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
		obj.put("departs", departs);
		if (departs == null || departs.size() == 0) {
			obj.put("multi_depart", 0);
		} else if (departs.size() == 1) {
			sysUserService.updateUserDepart(principal, departs.get(0).getOrgCode());
			obj.put("multi_depart", 1);
		} else {
			obj.put("multi_depart", 2);
		}
		obj.put("token", token);
		obj.put("userInfo", sysUser);
		result.setResult(obj);
		result.success("登录成功");
  		
	} catch (Exception e) {
		//e.printStackTrace();
		result.error500(e.getMessage());
	}
	return new HttpEntity<>(result);
}