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

The following examples show how to use org.jeecg.common.system.util.JwtUtil#getUserNameByToken() . 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: SysDepartController.java    From jeecg-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * 添加新数据 添加用户新建的部门对象数据,并保存到数据库
 * 
 * @param sysDepart
 * @return
 */
//@RequiresRoles({"admin"})
@RequestMapping(value = "/add", method = RequestMethod.POST)
@CacheEvict(value= {CacheConstant.SYS_DEPARTS_CACHE,CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries=true)
public Result<SysDepart> add(@RequestBody SysDepart sysDepart, HttpServletRequest request) {
	Result<SysDepart> result = new Result<SysDepart>();
	String username = JwtUtil.getUserNameByToken(request);
	try {
		sysDepart.setCreateBy(username);
		sysDepartService.saveDepartData(sysDepart, username);
		//清除部门树内存
		// FindsDepartsChildrenUtil.clearSysDepartTreeList();
		// FindsDepartsChildrenUtil.clearDepartIdModel();
		result.success("添加成功!");
	} catch (Exception e) {
		log.error(e.getMessage(),e);
		result.error500("操作失败");
	}
	return result;
}
 
Example 2
Source File: SysDepartController.java    From jeecg-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * 编辑数据 编辑部门的部分数据,并保存到数据库
 * 
 * @param sysDepart
 * @return
 */
//@RequiresRoles({"admin"})
@RequestMapping(value = "/edit", method = RequestMethod.PUT)
@CacheEvict(value= {CacheConstant.SYS_DEPARTS_CACHE,CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries=true)
public Result<SysDepart> edit(@RequestBody SysDepart sysDepart, HttpServletRequest request) {
	String username = JwtUtil.getUserNameByToken(request);
	sysDepart.setUpdateBy(username);
	Result<SysDepart> result = new Result<SysDepart>();
	SysDepart sysDepartEntity = sysDepartService.getById(sysDepart.getId());
	if (sysDepartEntity == null) {
		result.error500("未找到对应实体");
	} else {
		boolean ok = sysDepartService.updateDepartDataById(sysDepart, username);
		// TODO 返回false说明什么?
		if (ok) {
			//清除部门树内存
			//FindsDepartsChildrenUtil.clearSysDepartTreeList();
			//FindsDepartsChildrenUtil.clearDepartIdModel();
			result.success("修改成功!");
		}
	}
	return result;
}
 
Example 3
Source File: SysDepartController.java    From jeecg-boot-with-activiti with MIT License 6 votes vote down vote up
/**
 * 添加新数据 添加用户新建的部门对象数据,并保存到数据库
 * 
 * @param sysDepart
 * @return
 */
@RequestMapping(value = "/add", method = RequestMethod.POST)
@CacheEvict(value= {CacheConstant.SYS_DEPARTS_CACHE,CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries=true)
public Result<SysDepart> add(@RequestBody SysDepart sysDepart, HttpServletRequest request) {
	Result<SysDepart> result = new Result<SysDepart>();
	String username = JwtUtil.getUserNameByToken(request);
	try {
		sysDepart.setCreateBy(username);
		sysDepartService.saveDepartData(sysDepart, username);
		//清除部门树内存
		// FindsDepartsChildrenUtil.clearSysDepartTreeList();
		// FindsDepartsChildrenUtil.clearDepartIdModel();
		result.success("添加成功!");
	} catch (Exception e) {
		log.error(e.getMessage(),e);
		result.error500("操作失败");
	}
	return result;
}
 
Example 4
Source File: SysDepartController.java    From jeecg-boot-with-activiti with MIT License 6 votes vote down vote up
/**
 * 编辑数据 编辑部门的部分数据,并保存到数据库
 * 
 * @param sysDepart
 * @return
 */
@RequestMapping(value = "/edit", method = RequestMethod.PUT)
@CacheEvict(value= {CacheConstant.SYS_DEPARTS_CACHE,CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries=true)
public Result<SysDepart> edit(@RequestBody SysDepart sysDepart, HttpServletRequest request) {
	String username = JwtUtil.getUserNameByToken(request);
	sysDepart.setUpdateBy(username);
	Result<SysDepart> result = new Result<SysDepart>();
	SysDepart sysDepartEntity = sysDepartService.getById(sysDepart.getId());
	if (sysDepartEntity == null) {
		result.error500("未找到对应实体");
	} else {
		boolean ok = sysDepartService.updateDepartDataById(sysDepart, username);
		// TODO 返回false说明什么?
		if (ok) {
			//清除部门树内存
			//FindsDepartsChildrenUtil.clearSysDepartTreeList();
			//FindsDepartsChildrenUtil.clearDepartIdModel();
			result.success("修改成功!");
		}
	}
	return result;
}
 
Example 5
Source File: SysDepartController.java    From teaching with Apache License 2.0 6 votes vote down vote up
/**
 * 添加新数据 添加用户新建的部门对象数据,并保存到数据库
 * 
 * @param sysDepart
 * @return
 */
@RequestMapping(value = "/add", method = RequestMethod.POST)
@CacheEvict(value= {CacheConstant.SYS_DEPARTS_CACHE,CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries=true)
public Result<SysDepart> add(@RequestBody SysDepart sysDepart, HttpServletRequest request) {
	Result<SysDepart> result = new Result<SysDepart>();
	String username = JwtUtil.getUserNameByToken(request);
	try {
		sysDepart.setCreateBy(username);
		sysDepartService.saveDepartData(sysDepart, username);
		//清除部门树内存
		// FindsDepartsChildrenUtil.clearSysDepartTreeList();
		// FindsDepartsChildrenUtil.clearDepartIdModel();
		result.success("添加成功!");
	} catch (Exception e) {
		log.error(e.getMessage(),e);
		result.error500("操作失败");
	}
	return result;
}
 
Example 6
Source File: SysDepartController.java    From teaching with Apache License 2.0 6 votes vote down vote up
/**
 * 编辑数据 编辑部门的部分数据,并保存到数据库
 * 
 * @param sysDepart
 * @return
 */
@RequestMapping(value = "/edit", method = RequestMethod.PUT)
@CacheEvict(value= {CacheConstant.SYS_DEPARTS_CACHE,CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries=true)
public Result<SysDepart> edit(@RequestBody SysDepart sysDepart, HttpServletRequest request) {
	String username = JwtUtil.getUserNameByToken(request);
	sysDepart.setUpdateBy(username);
	Result<SysDepart> result = new Result<SysDepart>();
	SysDepart sysDepartEntity = sysDepartService.getById(sysDepart.getId());
	if (sysDepartEntity == null) {
		result.error500("未找到对应实体");
	} else {
		boolean ok = sysDepartService.updateDepartDataById(sysDepart, username);
		// TODO 返回false说明什么?
		if (ok) {
			//清除部门树内存
			//FindsDepartsChildrenUtil.clearSysDepartTreeList();
			//FindsDepartsChildrenUtil.clearDepartIdModel();
			result.success("修改成功!");
		}
	}
	return result;
}
 
Example 7
Source File: SysDepartController.java    From jeecg-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 添加新数据 添加用户新建的部门对象数据,并保存到数据库
 * 
 * @param sysDepart
 * @return
 */
//@RequiresRoles({"admin"})
@RequestMapping(value = "/add", method = RequestMethod.POST)
@CacheEvict(value= {CacheConstant.SYS_DEPARTS_CACHE,CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries=true)
public Result<SysDepart> add(@RequestBody SysDepart sysDepart, HttpServletRequest request) {
	Result<SysDepart> result = new Result<SysDepart>();
	String username = JwtUtil.getUserNameByToken(request);
	try {
		sysDepart.setCreateBy(username);
		sysDepartService.saveDepartData(sysDepart, username);
		//清除部门树内存
		// FindsDepartsChildrenUtil.clearSysDepartTreeList();
		// FindsDepartsChildrenUtil.clearDepartIdModel();
		result.success("添加成功!");
	} catch (Exception e) {
		log.error(e.getMessage(),e);
		result.error500("操作失败");
	}
	return result;
}
 
Example 8
Source File: SysDepartController.java    From jeecg-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 编辑数据 编辑部门的部分数据,并保存到数据库
 * 
 * @param sysDepart
 * @return
 */
//@RequiresRoles({"admin"})
@RequestMapping(value = "/edit", method = RequestMethod.PUT)
@CacheEvict(value= {CacheConstant.SYS_DEPARTS_CACHE,CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries=true)
public Result<SysDepart> edit(@RequestBody SysDepart sysDepart, HttpServletRequest request) {
	String username = JwtUtil.getUserNameByToken(request);
	sysDepart.setUpdateBy(username);
	Result<SysDepart> result = new Result<SysDepart>();
	SysDepart sysDepartEntity = sysDepartService.getById(sysDepart.getId());
	if (sysDepartEntity == null) {
		result.error500("未找到对应实体");
	} else {
		boolean ok = sysDepartService.updateDepartDataById(sysDepart, username);
		// TODO 返回false说明什么?
		if (ok) {
			//清除部门树内存
			//FindsDepartsChildrenUtil.clearSysDepartTreeList();
			//FindsDepartsChildrenUtil.clearDepartIdModel();
			result.success("修改成功!");
		}
	}
	return result;
}
 
Example 9
Source File: PermissionDataAspect.java    From jeecg-cloud with Apache License 2.0 5 votes vote down vote up
private void authDataHandler(HttpServletRequest request, String component) {
	List<SysPermission> currentSyspermission = null;
	if(oConvertUtils.isNotEmpty(component)) {
		//1.通过注解属性pageComponent 获取菜单
		currentSyspermission = sysUserRemoteApi.queryComponentPermission(component);
	}else {
		String requestMethod = request.getMethod();
		String requestPath = request.getRequestURI().substring(request.getContextPath().length());
		requestPath = filterUrl(requestPath);
		log.info("拦截请求 >> "+requestPath+";请求类型 >> "+requestMethod);
		currentSyspermission = sysUserRemoteApi.queryRequestPermission(requestMethod, requestPath);
	}
	//3.通过用户名+菜单ID 找到权限配置信息 放到request中去
	if(currentSyspermission!=null && currentSyspermission.size()>0) {
		String username = JwtUtil.getUserNameByToken(request);
		List<SysPermissionDataRuleModel> dataRules = new ArrayList<SysPermissionDataRuleModel>();
		for (SysPermission sysPermission : currentSyspermission) {
			// update-begin--Author:scott Date:20191119 for:数据权限规则编码不规范,项目存在相同包名和类名 #722
			List<SysPermissionDataRule> temp = sysUserRemoteApi.queryUserDataRule(username, sysPermission.getId());
			if(temp!=null && temp.size()>0) {
				//dataRules.addAll(temp);
				dataRules = oConvertUtils.entityListToModelList(temp,SysPermissionDataRuleModel.class);
			}
			// update-end--Author:scott Date:20191119 for:数据权限规则编码不规范,项目存在相同包名和类名 #722
		}
		if(dataRules!=null && dataRules.size()>0) {
			JeecgDataAutorUtils.installDataSearchConditon(request, dataRules);
			SysUserCacheInfo userinfo = sysUserRemoteApi.getCacheUser(username);
			JeecgDataAutorUtils.installUserInfo(request, userinfo);
		}
	}
}
 
Example 10
Source File: SysUserController.java    From jeecg-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 根据TOKEN获取用户的部分信息(返回的数据是可供表单设计器使用的数据)
 * 
 * @return
 */
@GetMapping("/getUserSectionInfoByToken")
public Result<?> getUserSectionInfoByToken(HttpServletRequest request, @RequestParam(name = "token", required = false) String token) {
	try {
		String username = null;
		// 如果没有传递token,就从header中获取token并获取用户信息
		if (oConvertUtils.isEmpty(token)) {
			 username = JwtUtil.getUserNameByToken(request);
		} else {
			 username = JwtUtil.getUsername(token);				
		}

		log.info(" ------ 通过令牌获取部分用户信息,当前用户: " + username);

		// 根据用户名查询用户信息
		SysUser sysUser = sysUserService.getUserByName(username);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sysUserId", sysUser.getId());
		map.put("sysUserCode", sysUser.getUsername()); // 当前登录用户登录账号
		map.put("sysUserName", sysUser.getRealname()); // 当前登录用户真实名称
		map.put("sysOrgCode", sysUser.getOrgCode()); // 当前登录用户部门编号

		log.info(" ------ 通过令牌获取部分用户信息,已获取的用户信息: " + map);

		return Result.ok(map);
	} catch (Exception e) {
		log.error(e.getMessage(), e);
		return Result.error(500, "查询失败:" + e.getMessage());
	}
}
 
Example 11
Source File: SysUserController.java    From jeecg-boot-with-activiti with MIT License 5 votes vote down vote up
/**
 * 根据TOKEN获取用户的部分信息(返回的数据是可供表单设计器使用的数据)
 * 
 * @return
 */
@GetMapping("/getUserSectionInfoByToken")
public Result<?> getUserSectionInfoByToken(HttpServletRequest request, @RequestParam(name = "token", required = false) String token) {
	try {
		String username = null;
		// 如果没有传递token,就从header中获取token并获取用户信息
		if (oConvertUtils.isEmpty(token)) {
			 username = JwtUtil.getUserNameByToken(request);
		} else {
			 username = JwtUtil.getUsername(token);				
		}

		log.info(" ------ 通过令牌获取部分用户信息,当前用户: " + username);

		// 根据用户名查询用户信息
		SysUser sysUser = sysUserService.getUserByName(username);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sysUserId", sysUser.getId());
		map.put("sysUserCode", sysUser.getUsername()); // 当前登录用户登录账号
		map.put("sysUserName", sysUser.getRealname()); // 当前登录用户真实名称
		map.put("sysOrgCode", sysUser.getOrgCode()); // 当前登录用户部门编号

		log.info(" ------ 通过令牌获取部分用户信息,已获取的用户信息: " + map);

		return Result.ok(map);
	} catch (Exception e) {
		log.error(e.getMessage(), e);
		return Result.error(500, "查询失败:" + e.getMessage());
	}
}
 
Example 12
Source File: SysUserController.java    From teaching with Apache License 2.0 5 votes vote down vote up
/**
 * 根据TOKEN获取用户的部分信息(返回的数据是可供表单设计器使用的数据)
 * 
 * @return
 */
@GetMapping("/getUserSectionInfoByToken")
public Result<?> getUserSectionInfoByToken(HttpServletRequest request, @RequestParam(name = "token", required = false) String token) {
	try {
		String username = null;
		// 如果没有传递token,就从header中获取token并获取用户信息
		if (oConvertUtils.isEmpty(token)) {
			 username = JwtUtil.getUserNameByToken(request);
		} else {
			 username = JwtUtil.getUsername(token);				
		}

		log.info(" ------ 通过令牌获取部分用户信息,当前用户: " + username);

		// 根据用户名查询用户信息
		SysUser sysUser = sysUserService.getUserByName(username);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sysUserId", sysUser.getId());
		map.put("sysUserCode", sysUser.getUsername()); // 当前登录用户登录账号
		map.put("sysUserName", sysUser.getRealname()); // 当前登录用户真实名称
		map.put("sysOrgCode", sysUser.getOrgCode()); // 当前登录用户部门编号

		log.info(" ------ 通过令牌获取部分用户信息,已获取的用户信息: " + map);

		return Result.ok(map);
	} catch (Exception e) {
		log.error(e.getMessage(), e);
		return Result.error(500, "查询失败:" + e.getMessage());
	}
}
 
Example 13
Source File: SysUserController.java    From jeecg-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 根据TOKEN获取用户的部分信息(返回的数据是可供表单设计器使用的数据)
 * 
 * @return
 */
@GetMapping("/getUserSectionInfoByToken")
public Result<?> getUserSectionInfoByToken(HttpServletRequest request, @RequestParam(name = "token", required = false) String token) {
	try {
		String username = null;
		// 如果没有传递token,就从header中获取token并获取用户信息
		if (oConvertUtils.isEmpty(token)) {
			 username = JwtUtil.getUserNameByToken(request);
		} else {
			 username = JwtUtil.getUsername(token);				
		}

		log.info(" ------ 通过令牌获取部分用户信息,当前用户: " + username);

		// 根据用户名查询用户信息
		SysUser sysUser = sysUserService.getUserByName(username);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sysUserId", sysUser.getId());
		map.put("sysUserCode", sysUser.getUsername()); // 当前登录用户登录账号
		map.put("sysUserName", sysUser.getRealname()); // 当前登录用户真实名称
		map.put("sysOrgCode", sysUser.getOrgCode()); // 当前登录用户部门编号

		log.info(" ------ 通过令牌获取部分用户信息,已获取的用户信息: " + map);

		return Result.ok(map);
	} catch (Exception e) {
		log.error(e.getMessage(), e);
		return Result.error(500, "查询失败:" + e.getMessage());
	}
}
 
Example 14
Source File: OnlineInterceptor.java    From jeecg-boot with Apache License 2.0 4 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    // 请求的方法是否有注解
    boolean anno = handler.getClass().isAssignableFrom(HandlerMethod.class);
    if (anno) {
        OnlineAuth onlineAuth = ((HandlerMethod) handler).getMethodAnnotation(OnlineAuth.class);
        if (onlineAuth != null) {
            String requestPath = request.getRequestURI().substring(request.getContextPath().length());
            requestPath = filterUrl(requestPath);
            //1.通过前端请求地址得到code
            String authKey = onlineAuth.value();
            String code = requestPath.substring(requestPath.lastIndexOf(authKey) + authKey.length());
            log.info("拦截请求(" + request.getMethod() + "):" + requestPath + ",");
            if ("form".equals(authKey) && "DELETE".equals(request.getMethod())) {
                code = code.substring(0, code.lastIndexOf("/"));
            }
            List<String> possibleUrl = new ArrayList<>();
            //获取可能的表单地址
            for (String pre : ONLINE_TEST_PRE) {
                possibleUrl.add(pre + code);
            }
            //查询菜单
            LambdaQueryWrapper<SysPermission> query = new LambdaQueryWrapper<SysPermission>();
            query.eq(SysPermission::getDelFlag, 0);
            query.in(SysPermission::getUrl, possibleUrl);
            List<SysPermission> permissionList = sysPermissionService.list(query);

            String username = JwtUtil.getUserNameByToken(request);
            if (permissionList == null || permissionList.size() == 0) {
                //没有配置菜单 找online表单菜单地址
                boolean hasPermission = sysPermissionService.hasPermission(username, ONLINE_FORM);
                if (!hasPermission) {
                    backError(response, authKey);
                    return false;
                }
            } else {
                //找到菜单了
                boolean has = false;
                for (SysPermission p : permissionList) {
                    has = has || sysPermissionService.hasPermission(username, p);
                }
                if (!has) {
                    backError(response, authKey);
                    return false;
                }
            }
        }
    }
    return true;
}