feat: 完成用户登录
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
技术栈采用Spring Cloud Gateway+Spring Security OAuth2+JWT
|
||||
|
||||
服务器 47.180.174.163 root / Yang716938
|
||||
服务器 47.108.174.163 root / Yang716938
|
||||
|
||||
nacos 47.108.174.163:8848 nacos / nacos
|
||||
|
||||
|
||||
@@ -76,8 +76,16 @@ public class PathConstant {
|
||||
* 获取树
|
||||
*/
|
||||
final static public String TREE = "tree";
|
||||
/**
|
||||
* 当前用户
|
||||
*/
|
||||
final static public String CURRENT_USER = "current/user";
|
||||
/**
|
||||
* 当前用户权限
|
||||
*/
|
||||
final static public String USER_PREM = "user/prem";
|
||||
final static public String CURRENT_PERM = "current/prem";
|
||||
/**
|
||||
* 当前用户菜单
|
||||
*/
|
||||
final static public String CURRENT_MENU = "current/menu";
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.aos.auth.controller;
|
||||
|
||||
import cn.hutool.core.lang.tree.Tree;
|
||||
import com.aos.auth.constant.PathConstant;
|
||||
import com.aos.auth.domain.req.user.LoginUseReq;
|
||||
import com.aos.auth.domain.vo.login.CurrentUserInfoVo;
|
||||
import com.aos.auth.domain.vo.user.UserVo;
|
||||
import com.aos.auth.service.AosAuthLoginService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
@@ -11,6 +12,9 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(PathConstant.BASE_API)
|
||||
@Api(tags = "用户登录接口")
|
||||
@@ -26,13 +30,29 @@ public class LoginController {
|
||||
return token;
|
||||
}
|
||||
|
||||
@GetMapping(PathConstant.USER_PREM)
|
||||
@ApiOperation("获取当前用户权限")
|
||||
public CurrentUserInfoVo userPrem() {
|
||||
CurrentUserInfoVo resourceVoList = aosAuthLoginService.currentUserInfo();
|
||||
@GetMapping(PathConstant.CURRENT_USER)
|
||||
@ApiOperation("获取当前用户权限信息")
|
||||
public UserVo currentUser() {
|
||||
UserVo resourceVoList = aosAuthLoginService.currentUserInfo();
|
||||
return resourceVoList;
|
||||
}
|
||||
|
||||
@GetMapping(PathConstant.CURRENT_PERM)
|
||||
@ApiOperation("获取当前用户所有权限码")
|
||||
public Set<String> perm() {
|
||||
Set<String> permSet = aosAuthLoginService.userPrem();
|
||||
return permSet;
|
||||
}
|
||||
|
||||
|
||||
@GetMapping(PathConstant.CURRENT_MENU)
|
||||
@ApiOperation("获取当前用户所有菜单树")
|
||||
public List<Tree<Long>> menu() {
|
||||
List<Tree<Long>> menuList = aosAuthLoginService.userMenu();
|
||||
return menuList;
|
||||
}
|
||||
|
||||
|
||||
@GetMapping(PathConstant.LOGOUT)
|
||||
@ApiOperation("退出登录")
|
||||
public String logout() {
|
||||
|
||||
@@ -44,7 +44,7 @@ public class UserController {
|
||||
aosAuthUsersService.updateUser(req);
|
||||
}
|
||||
|
||||
@PutMapping(PathConstant.DELETE)
|
||||
@DeleteMapping(PathConstant.DELETE)
|
||||
@ApiOperation("删除用户")
|
||||
public void update(@RequestBody List<Long> ids) {
|
||||
aosAuthUsersService.deleteUser(ids);
|
||||
|
||||
@@ -32,8 +32,8 @@ public class AosAuthUsers extends BaseEntity implements Serializable {
|
||||
/**
|
||||
* 用户名称
|
||||
*/
|
||||
@TableField(value = "name")
|
||||
private String name;
|
||||
@TableField(value = "user_name")
|
||||
private String userName;
|
||||
|
||||
/**
|
||||
* 用户密码
|
||||
|
||||
@@ -30,7 +30,7 @@ public class UserReq {
|
||||
* 用户名称
|
||||
*/
|
||||
@ApiModelProperty(value = "用户名称")
|
||||
private String name;
|
||||
private String userName;
|
||||
|
||||
/**
|
||||
* 用户密码
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.aos.auth.domain.vo.login;
|
||||
|
||||
import com.aos.auth.domain.vo.resource.ResourceVo;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 当前用户信息
|
||||
*/
|
||||
@@ -36,9 +33,4 @@ public class CurrentUserInfoVo {
|
||||
*/
|
||||
@ApiModelProperty(value = "手机号")
|
||||
private String phone;
|
||||
/**
|
||||
* 权限
|
||||
*/
|
||||
@ApiModelProperty(value = "权限列表")
|
||||
List<ResourceVo> resourceVoList;
|
||||
}
|
||||
|
||||
@@ -16,25 +16,25 @@ public class UserVo {
|
||||
/**
|
||||
* 登录id
|
||||
*/
|
||||
@ApiModelProperty(value = "account")
|
||||
@ApiModelProperty(value = "登录id")
|
||||
private String account;
|
||||
|
||||
/**
|
||||
* 用户名称
|
||||
*/
|
||||
@ApiModelProperty(value = "name")
|
||||
private String name;
|
||||
@ApiModelProperty(value = "用户名称")
|
||||
private String userName;
|
||||
|
||||
/**
|
||||
* 用户邮箱
|
||||
*/
|
||||
@ApiModelProperty(value = "email")
|
||||
@ApiModelProperty(value = "用户邮箱")
|
||||
private String email;
|
||||
|
||||
/**
|
||||
* 手机号
|
||||
*/
|
||||
@ApiModelProperty(value = "phone")
|
||||
@ApiModelProperty(value = "手机号")
|
||||
private String phone;
|
||||
|
||||
/**
|
||||
|
||||
+61
-7
@@ -1,9 +1,13 @@
|
||||
package com.aos.auth.service.impl.security;
|
||||
|
||||
import cn.hutool.core.lang.tree.Tree;
|
||||
import cn.hutool.core.lang.tree.TreeNode;
|
||||
import cn.hutool.core.lang.tree.TreeUtil;
|
||||
import com.aos.auth.domain.req.user.LoginUseReq;
|
||||
import com.aos.auth.domain.security.SecurityUser;
|
||||
import com.aos.auth.domain.vo.login.CurrentUserInfoVo;
|
||||
import com.aos.auth.domain.vo.resource.ResourceVo;
|
||||
import com.aos.auth.domain.vo.user.UserVo;
|
||||
import com.aos.auth.enums.ResourceTypeEnum;
|
||||
import com.aos.auth.service.AosAuthLoginService;
|
||||
import com.aos.auth.service.AosAuthResourcesService;
|
||||
import com.aos.common.copy.AosBeanUtils;
|
||||
@@ -18,7 +22,8 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author: wangyl
|
||||
@@ -32,6 +37,7 @@ public class AosAuthLoginServiceImpl implements AosAuthLoginService {
|
||||
final AuthenticationManager authenticationManager;
|
||||
final AosAuthResourcesService aosAuthResourcesService;
|
||||
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
@@ -55,8 +61,16 @@ public class AosAuthLoginServiceImpl implements AosAuthLoginService {
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户可以访问的资源
|
||||
*
|
||||
* @param
|
||||
* @return java.util.List<com.aos.auth.domain.vo.resource.ResourceVo>
|
||||
* @Date 2022/11/20
|
||||
* @Author wangyl
|
||||
*/
|
||||
@Override
|
||||
public List<ResourceVo> userPrem() {
|
||||
public List<ResourceVo> userResource() {
|
||||
SecurityUserInfo context = CurrentUserContextHolder.getContext();
|
||||
List<String> authorities = context.getAuthorities();
|
||||
List<ResourceVo> resourceVoList = aosAuthResourcesService.getResourceByPrem(authorities);
|
||||
@@ -71,11 +85,51 @@ public class AosAuthLoginServiceImpl implements AosAuthLoginService {
|
||||
* @Author wangyl
|
||||
*/
|
||||
@Override
|
||||
public CurrentUserInfoVo currentUserInfo() {
|
||||
public UserVo currentUserInfo() {
|
||||
SecurityUserInfo context = CurrentUserContextHolder.getContext();
|
||||
CurrentUserInfoVo currentUserInfoVo = AosBeanUtils.copyProperties(context, CurrentUserInfoVo::new);
|
||||
List<ResourceVo> resourceVoList = this.userPrem();
|
||||
currentUserInfoVo.setResourceVoList(resourceVoList);
|
||||
UserVo currentUserInfoVo = AosBeanUtils.copyProperties(context, UserVo::new);
|
||||
return currentUserInfoVo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户权限码
|
||||
*
|
||||
* @return java.util.Set<java.lang.String>
|
||||
* @Date 2022/11/20
|
||||
* @Author wangyl
|
||||
*/
|
||||
@Override
|
||||
public Set<String> userPrem() {
|
||||
List<ResourceVo> resourceVos = this.userResource();
|
||||
Set<String> userPremSet = resourceVos.stream().map(ResourceVo::getResourcePerms).collect(Collectors.toSet());
|
||||
return userPremSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户菜单
|
||||
*
|
||||
* @return java.util.List<cn.hutool.core.lang.tree.Tree < java.lang.Long>>
|
||||
* @Date 2022/11/20
|
||||
* @Author wangyl
|
||||
*/
|
||||
@Override
|
||||
public List<Tree<Long>> userMenu() {
|
||||
List<ResourceVo> resourceVos = this.userResource();
|
||||
resourceVos = resourceVos.stream().filter(o -> {
|
||||
Integer resourceType = o.getResourceType();
|
||||
return Objects.equals(resourceType, ResourceTypeEnum.CATALOGUE.getValue()) || Objects.equals(resourceType, ResourceTypeEnum.Menu.getValue());
|
||||
}).collect(Collectors.toList());
|
||||
List<TreeNode<Long>> collect = resourceVos.stream().map(o -> {
|
||||
TreeNode<Long> longTree = new TreeNode<>();
|
||||
longTree.setId(o.getId());
|
||||
longTree.setParentId(o.getParentId());
|
||||
longTree.setName(o.getResourceName());
|
||||
Map<String, Object> extraMap = new HashMap<>();
|
||||
extraMap.put("path", o.getResourceRoute());
|
||||
longTree.setExtra(extraMap);
|
||||
return longTree;
|
||||
}).collect(Collectors.toList());
|
||||
List<Tree<Long>> build = TreeUtil.build(collect, -1L);
|
||||
return build;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -42,7 +42,7 @@ public class AosUserDetailServiceImpl implements UserDetailsService {
|
||||
throw new UsernameNotFoundException("");
|
||||
}
|
||||
List<GrantedAuthority> userAuthorities = this.getUserAuthorities(userByAccount.getId(), userByAccount.getState());
|
||||
return new SecurityUser(userByAccount.getId(), userByAccount.getName(), userByAccount.getPassword(), userByAccount.getEmail(), userByAccount.getPhone(), userByAccount.getState(), userAuthorities);
|
||||
return new SecurityUser(userByAccount.getId(), userByAccount.getUserName(), userByAccount.getPassword(), userByAccount.getEmail(), userByAccount.getPhone(), userByAccount.getState(), userAuthorities);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.aos.auth.service;
|
||||
|
||||
import cn.hutool.core.lang.tree.Tree;
|
||||
import com.aos.auth.domain.req.user.LoginUseReq;
|
||||
import com.aos.auth.domain.vo.login.CurrentUserInfoVo;
|
||||
import com.aos.auth.domain.vo.resource.ResourceVo;
|
||||
import com.aos.auth.domain.vo.user.UserVo;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author: wangyl
|
||||
@@ -23,14 +25,14 @@ public interface AosAuthLoginService {
|
||||
String login(LoginUseReq loginUseReq);
|
||||
|
||||
/**
|
||||
* 获取用户权限
|
||||
* 获取用户资源
|
||||
*
|
||||
* @param
|
||||
* @return java.util.List<com.aos.auth.domain.vo.resource.ResourceVo>
|
||||
* @Date 2022/11/18
|
||||
* @Author wangyl
|
||||
*/
|
||||
List<ResourceVo> userPrem();
|
||||
List<ResourceVo> userResource();
|
||||
|
||||
/**
|
||||
* 获取当前用户信息
|
||||
@@ -40,5 +42,26 @@ public interface AosAuthLoginService {
|
||||
* @Date 2022/11/18
|
||||
* @Author wangyl
|
||||
*/
|
||||
CurrentUserInfoVo currentUserInfo();
|
||||
UserVo currentUserInfo();
|
||||
|
||||
/**
|
||||
* 获取用户权限码
|
||||
*
|
||||
* @param
|
||||
* @return java.util.Set<java.lang.String>
|
||||
* @Date 2022/11/20
|
||||
* @Author wangyl
|
||||
*/
|
||||
Set<String> userPrem();
|
||||
|
||||
/**
|
||||
* 获取当前用户菜单
|
||||
*
|
||||
* @param
|
||||
* @return java.util.List<cn.hutool.core.lang.tree.Tree < java.lang.Long>>
|
||||
* @Date 2022/11/20
|
||||
* @Author wangyl
|
||||
*/
|
||||
List<Tree<Long>> userMenu();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user