use of com.moxi.mogublog.commons.entity.Admin in project mogu_blog_v2 by moxi624.
the class AuthorityVerifyAspect method doAround.
@Around(value = "pointcut(authorityVerify)")
public Object doAround(ProceedingJoinPoint joinPoint, AuthorityVerify authorityVerify) throws Throwable {
ServletRequestAttributes attribute = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attribute.getRequest();
// 获取请求路径
String url = request.getRequestURI();
// 解析出请求者的ID和用户名
String adminUid = request.getAttribute(SysConf.ADMIN_UID).toString();
// 管理员能够访问的路径
String visitUrlStr = redisUtil.get(RedisConf.ADMIN_VISIT_MENU + RedisConf.SEGMENTATION + adminUid);
LinkedTreeMap<String, String> visitMap = new LinkedTreeMap<>();
if (StringUtils.isNotEmpty(visitUrlStr)) {
// 从Redis中获取
visitMap = (LinkedTreeMap<String, String>) JsonUtils.jsonToMap(visitUrlStr, String.class);
} else {
// 查询数据库获取
Admin admin = adminService.getById(adminUid);
String roleUid = admin.getRoleUid();
Role role = roleService.getById(roleUid);
String caetgoryMenuUids = role.getCategoryMenuUids();
String[] uids = caetgoryMenuUids.replace("[", "").replace("]", "").replace("\"", "").split(",");
List<String> categoryMenuUids = new ArrayList<>(Arrays.asList(uids));
// 这里只需要查询访问的按钮
QueryWrapper<CategoryMenu> queryWrapper = new QueryWrapper<>();
queryWrapper.in(SQLConf.UID, categoryMenuUids);
queryWrapper.eq(SQLConf.MENU_TYPE, EMenuType.BUTTON);
queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
List<CategoryMenu> buttonList = categoryMenuService.list(queryWrapper);
for (CategoryMenu item : buttonList) {
if (StringUtils.isNotEmpty(item.getUrl())) {
visitMap.put(item.getUrl(), item.getUrl());
}
}
// 将访问URL存储到Redis中
redisUtil.setEx(RedisConf.ADMIN_VISIT_MENU + SysConf.REDIS_SEGMENTATION + adminUid, JsonUtils.objectToJson(visitMap), 1, TimeUnit.HOURS);
}
// 判断该角色是否能够访问该接口
if (visitMap.get(url) != null) {
log.info("用户拥有操作权限,访问的路径: {},拥有的权限接口:{}", url, visitMap.get(url));
// 执行业务
return joinPoint.proceed();
} else {
log.info("用户不具有操作权限,访问的路径: {}", url);
return ResultUtil.result(ECode.NO_OPERATION_AUTHORITY, MessageConf.RESTAPI_NO_PRIVILEGE);
}
}
use of com.moxi.mogublog.commons.entity.Admin in project mogu_blog_v2 by moxi624.
the class LoginRestApi method getMenu.
@ApiOperation(value = "获取当前用户的菜单", notes = "获取当前用户的菜单", response = String.class)
@GetMapping(value = "/getMenu")
public String getMenu(HttpServletRequest request) {
Collection<CategoryMenu> categoryMenuList = new ArrayList<>();
Admin admin = adminService.getById(request.getAttribute(SysConf.ADMIN_UID).toString());
List<String> roleUid = new ArrayList<>();
roleUid.add(admin.getRoleUid());
Collection<Role> roleList = roleService.listByIds(roleUid);
List<String> categoryMenuUids = new ArrayList<>();
roleList.forEach(item -> {
String caetgoryMenuUids = item.getCategoryMenuUids();
String[] uids = caetgoryMenuUids.replace("[", "").replace("]", "").replace("\"", "").split(",");
categoryMenuUids.addAll(Arrays.asList(uids));
});
categoryMenuList = categoryMenuService.listByIds(categoryMenuUids);
// 从三级级分类中查询出 二级分类
List<CategoryMenu> buttonList = new ArrayList<>();
Set<String> secondMenuUidList = new HashSet<>();
categoryMenuList.forEach(item -> {
// 查询二级分类
if (item.getMenuType() == EMenuType.MENU && item.getMenuLevel() == SysConf.TWO) {
secondMenuUidList.add(item.getUid());
}
// 从三级分类中,得到二级分类
if (item.getMenuType() == EMenuType.BUTTON && StringUtils.isNotEmpty(item.getParentUid())) {
// 找出二级菜单
secondMenuUidList.add(item.getParentUid());
// 找出全部按钮
buttonList.add(item);
}
});
Collection<CategoryMenu> childCategoryMenuList = new ArrayList<>();
Collection<CategoryMenu> parentCategoryMenuList = new ArrayList<>();
List<String> parentCategoryMenuUids = new ArrayList<>();
if (secondMenuUidList.size() > 0) {
childCategoryMenuList = categoryMenuService.listByIds(secondMenuUidList);
}
childCategoryMenuList.forEach(item -> {
// 选出所有的二级分类
if (item.getMenuLevel() == SysConf.TWO) {
if (StringUtils.isNotEmpty(item.getParentUid())) {
parentCategoryMenuUids.add(item.getParentUid());
}
}
});
if (parentCategoryMenuUids.size() > 0) {
parentCategoryMenuList = categoryMenuService.listByIds(parentCategoryMenuUids);
}
List<CategoryMenu> list = new ArrayList<>(parentCategoryMenuList);
// 对parent进行排序
Map<String, Object> map = new HashMap<>(Constants.NUM_THREE);
Collections.sort(list);
map.put(SysConf.PARENT_LIST, list);
map.put(SysConf.SON_LIST, childCategoryMenuList);
map.put(SysConf.BUTTON_LIST, buttonList);
return ResultUtil.result(SysConf.SUCCESS, map);
}
use of com.moxi.mogublog.commons.entity.Admin in project mogu_blog_v2 by moxi624.
the class LoginRestApi method login.
@ApiOperation(value = "用户登录", notes = "用户登录")
@PostMapping("/login")
public String login(HttpServletRequest request, @ApiParam(name = "username", value = "用户名或邮箱或手机号") @RequestParam(name = "username", required = false) String username, @ApiParam(name = "password", value = "密码") @RequestParam(name = "password", required = false) String password, @ApiParam(name = "isRememberMe", value = "是否记住账号密码") @RequestParam(name = "isRememberMe", required = false, defaultValue = "false") Boolean isRememberMe) {
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
return ResultUtil.result(SysConf.ERROR, "账号或密码不能为空");
}
String ip = IpUtils.getIpAddr(request);
String limitCount = redisUtil.get(RedisConf.LOGIN_LIMIT + RedisConf.SEGMENTATION + ip);
if (StringUtils.isNotEmpty(limitCount)) {
Integer tempLimitCount = Integer.valueOf(limitCount);
if (tempLimitCount >= Constants.NUM_FIVE) {
return ResultUtil.result(SysConf.ERROR, "密码输错次数过多,已被锁定30分钟");
}
}
Boolean isEmail = CheckUtils.checkEmail(username);
Boolean isMobile = CheckUtils.checkMobileNumber(username);
QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
if (isEmail) {
queryWrapper.eq(SQLConf.EMAIL, username);
} else if (isMobile) {
queryWrapper.eq(SQLConf.MOBILE, username);
} else {
queryWrapper.eq(SQLConf.USER_NAME, username);
}
queryWrapper.last(SysConf.LIMIT_ONE);
queryWrapper.eq(SysConf.STATUS, EStatus.ENABLE);
Admin admin = adminService.getOne(queryWrapper);
if (admin == null) {
// 设置错误登录次数
log.error("该管理员不存在");
return ResultUtil.result(SysConf.ERROR, String.format(MessageConf.LOGIN_ERROR, setLoginCommit(request)));
}
// 对密码进行加盐加密验证,采用SHA-256 + 随机盐【动态加盐】 + 密钥对密码进行加密
PasswordEncoder encoder = new BCryptPasswordEncoder();
boolean isPassword = encoder.matches(password, admin.getPassWord());
if (!isPassword) {
// 密码错误,返回提示
log.error("管理员密码错误");
return ResultUtil.result(SysConf.ERROR, String.format(MessageConf.LOGIN_ERROR, setLoginCommit(request)));
}
List<String> roleUids = new ArrayList<>();
roleUids.add(admin.getRoleUid());
List<Role> roles = (List<Role>) roleService.listByIds(roleUids);
if (roles.size() <= 0) {
return ResultUtil.result(SysConf.ERROR, MessageConf.NO_ROLE);
}
String roleNames = null;
for (Role role : roles) {
roleNames += (role.getRoleName() + Constants.SYMBOL_COMMA);
}
String roleName = roleNames.substring(0, roleNames.length() - 2);
long expiration = isRememberMe ? isRememberMeExpiresSecond : audience.getExpiresSecond();
String jwtToken = jwtTokenUtil.createJWT(admin.getUserName(), admin.getUid(), roleName, audience.getClientId(), audience.getName(), expiration * 1000, audience.getBase64Secret());
String token = tokenHead + jwtToken;
Map<String, Object> result = new HashMap<>(Constants.NUM_ONE);
result.put(SysConf.TOKEN, token);
// 进行登录相关操作
Integer count = admin.getLoginCount() + 1;
admin.setLoginCount(count);
admin.setLastLoginIp(IpUtils.getIpAddr(request));
admin.setLastLoginTime(new Date());
admin.updateById();
// 设置token到validCode,用于记录登录用户
admin.setValidCode(token);
// 设置tokenUid,【主要用于换取token令牌,防止token直接暴露到在线用户管理中】
admin.setTokenUid(StringUtils.getUUID());
admin.setRole(roles.get(0));
// 添加在线用户到Redis中【设置过期时间】
adminService.addOnlineAdmin(admin, expiration);
return ResultUtil.result(SysConf.SUCCESS, result);
}
use of com.moxi.mogublog.commons.entity.Admin in project mogu_blog_v2 by moxi624.
the class LoginRestApi method info.
@ApiOperation(value = "用户信息", notes = "用户信息", response = String.class)
@GetMapping(value = "/info")
public String info(HttpServletRequest request, @ApiParam(name = "token", value = "token令牌", required = false) @RequestParam(name = "token", required = false) String token) {
Map<String, Object> map = new HashMap<>(Constants.NUM_THREE);
if (request.getAttribute(SysConf.ADMIN_UID) == null) {
return ResultUtil.result(SysConf.ERROR, "token用户过期");
}
Admin admin = adminService.getById(request.getAttribute(SysConf.ADMIN_UID).toString());
map.put(SysConf.TOKEN, token);
// 获取图片
if (StringUtils.isNotEmpty(admin.getAvatar())) {
String pictureList = this.pictureFeignClient.getPicture(admin.getAvatar(), SysConf.FILE_SEGMENTATION);
List<String> list = webUtil.getPicture(pictureList);
if (list.size() > 0) {
map.put(SysConf.AVATAR, list.get(0));
} else {
map.put(SysConf.AVATAR, "https://gitee.com/moxi159753/wx_picture/raw/master/picture/favicon.png");
}
}
List<String> roleUid = new ArrayList<>();
roleUid.add(admin.getRoleUid());
Collection<Role> roleList = roleService.listByIds(roleUid);
map.put(SysConf.ROLES, roleList);
return ResultUtil.result(SysConf.SUCCESS, map);
}
use of com.moxi.mogublog.commons.entity.Admin in project mogu_blog_v2 by moxi624.
the class SecurityUserDetailsServiceImpl method loadUserByUsername.
/**
* @param username 浏览器输入的用户名【需要保证用户名的唯一性】
* @return
* @throws UsernameNotFoundException
*/
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(SQLConf.USER_NAME, username);
queryWrapper.last(SysConf.LIMIT_ONE);
Admin admin = adminService.getOne(queryWrapper);
if (admin == null) {
throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));
} else {
// 查询出角色信息封装到admin中
List<String> roleNames = new ArrayList<>();
Role role = roleService.getById(admin.getRoleUid());
roleNames.add(role.getRoleName());
admin.setRoleNames(roleNames);
return SecurityUserFactory.create(admin);
}
}
Aggregations