|
@@ -0,0 +1,141 @@
|
|
|
|
+package com.lantone.security.facade;
|
|
|
|
+
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.diagbot.enums.IsDeleteEnum;
|
|
|
|
+import com.diagbot.exception.CommonErrorCode;
|
|
|
|
+import com.diagbot.exception.CommonException;
|
|
|
|
+import com.diagbot.util.BeanUtil;
|
|
|
|
+import com.diagbot.util.EntityUtil;
|
|
|
|
+import com.diagbot.util.ListUtil;
|
|
|
|
+import com.diagbot.util.StringUtil;
|
|
|
|
+import com.lantone.dblayermbg.entity.Organize;
|
|
|
|
+import com.lantone.dblayermbg.entity.Role;
|
|
|
|
+import com.lantone.dblayermbg.entity.User;
|
|
|
|
+import com.lantone.dblayermbg.entity.UserRole;
|
|
|
|
+import com.lantone.dblayermbg.facade.UserRoleFacade;
|
|
|
|
+import com.lantone.dblayermbg.service.impl.UserServiceImpl;
|
|
|
|
+import com.lantone.security.client.AuthServiceClient;
|
|
|
|
+import com.lantone.security.dto.JWT;
|
|
|
|
+import com.lantone.security.dto.JwtDTO;
|
|
|
|
+import com.lantone.security.dto.JwtStore;
|
|
|
|
+import com.lantone.security.dto.MenuInfoDTO;
|
|
|
|
+import com.lantone.security.dto.OrganizeDTO;
|
|
|
|
+import com.lantone.security.enums.StatusEnum;
|
|
|
|
+import com.lantone.security.exception.ServiceErrorCode;
|
|
|
|
+import com.lantone.security.util.SysUserUtils;
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.security.crypto.factory.PasswordEncoderFactories;
|
|
|
|
+import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.util.DigestUtils;
|
|
|
|
+
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.HashSet;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Set;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * <p>
|
|
|
|
+ * 系统用户表 服务实现类Facade
|
|
|
|
+ * </p>
|
|
|
|
+ */
|
|
|
|
+@Component
|
|
|
|
+public class UserLoginFacade extends UserServiceImpl {
|
|
|
|
+ @Autowired
|
|
|
|
+ private TokenFacade tokenFacade;
|
|
|
|
+ @Autowired
|
|
|
|
+ private AuthServiceClient authServiceClient;
|
|
|
|
+ @Autowired
|
|
|
|
+ private UserRoleFacade userRoleFacade;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取jwt
|
|
|
|
+ *
|
|
|
|
+ * @param username 用户名
|
|
|
|
+ * @param password 密码
|
|
|
|
+ * @return jwt
|
|
|
|
+ */
|
|
|
|
+ public JwtDTO getJwt(String username, String password) {
|
|
|
|
+ JwtDTO data = new JwtDTO();
|
|
|
|
+ if (StringUtil.isBlank(username)) {
|
|
|
|
+ throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
|
|
|
|
+ "请输入用户名");
|
|
|
|
+ }
|
|
|
|
+ if (StringUtil.isBlank(password)) {
|
|
|
|
+ throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
|
|
|
|
+ "请输入密码");
|
|
|
|
+ }
|
|
|
|
+ //使用MD5对密码进行加密
|
|
|
|
+ String MD5Password = DigestUtils.md5DigestAsHex(password.getBytes());
|
|
|
|
+ QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ userQueryWrapper.eq("username", username)
|
|
|
|
+ .eq("status", StatusEnum.Enable.getKey())
|
|
|
|
+ .eq("is_deleted", IsDeleteEnum.N.getKey());
|
|
|
|
+ User user = this.getOne(userQueryWrapper, false);
|
|
|
|
+ if (null == user) {
|
|
|
|
+ throw new CommonException(ServiceErrorCode.USER_NOT_FOUND);
|
|
|
|
+ }
|
|
|
|
+ PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
|
|
|
+ if (!passwordEncoder.matches(MD5Password, user.getPassword())) {
|
|
|
|
+ throw new CommonException(ServiceErrorCode.USER_PASSWORD_ERROR);
|
|
|
|
+ }
|
|
|
|
+ JWT jwt = authServiceClient.getToken("Basic dWFhLXNlcnZpY2U6MTIzNDU2",
|
|
|
|
+ "password", username, MD5Password);
|
|
|
|
+ if (null == jwt) {
|
|
|
|
+ throw new CommonException(ServiceErrorCode.GET_TOKEN_FAIL);
|
|
|
|
+ }
|
|
|
|
+ data.setAccessToken(jwt.getAccess_token());
|
|
|
|
+ data.setRefreshToken(jwt.getRefresh_token());
|
|
|
|
+ //获取用户组织信息
|
|
|
|
+ Long id = user.getId();
|
|
|
|
+ List<Organize> organizes = this.baseMapper.getUserOrganizeByUserID(id);
|
|
|
|
+ if (organizes != null && !organizes.isEmpty()) {
|
|
|
|
+ List<OrganizeDTO> organizeDTOS = new ArrayList<>();
|
|
|
|
+ //获取用户当前组织下的角色信息
|
|
|
|
+ for (Organize organize : organizes) {
|
|
|
|
+ OrganizeDTO organizeDTO = new OrganizeDTO();
|
|
|
|
+ BeanUtils.copyProperties(organize,organizeDTO);
|
|
|
|
+ if(StringUtil.isNotBlank(organize.getOrganizeId())){
|
|
|
|
+ List<Role> roles= this.baseMapper.getUserRoleByUserID(id,organize.getOrganizeId());
|
|
|
|
+ organizeDTO.setRoles(roles);
|
|
|
|
+ }
|
|
|
|
+ organizeDTOS.add(organizeDTO);
|
|
|
|
+ }
|
|
|
|
+ data.setOrganizes(organizeDTOS);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //token存入redis
|
|
|
|
+ JwtStore jwtStore = new JwtStore();
|
|
|
|
+ jwtStore.setAccessToken(jwt.getAccess_token());
|
|
|
|
+ jwtStore.setRefreshToken(jwt.getRefresh_token());
|
|
|
|
+ tokenFacade.createToken(jwtStore);
|
|
|
|
+ return data;
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * @Author songxl
|
|
|
|
+ * @Description 获取用户显示的菜单
|
|
|
|
+ * @Date 2021/7/19
|
|
|
|
+ * @Param []
|
|
|
|
+ * @Return com.lantone.security.dto.MenuInfoDTO
|
|
|
|
+ * @MethodName getUserOrgMenu
|
|
|
|
+ */
|
|
|
|
+ public MenuInfoDTO getUserOrgMenu() {
|
|
|
|
+ MenuInfoDTO menuInfoDTO = new MenuInfoDTO();
|
|
|
|
+ Set<String> roleSet = new HashSet<>();
|
|
|
|
+ Long userId = Long.parseLong(SysUserUtils.getCurrentPrincipleID());
|
|
|
|
+ //获取当前登录用户角色
|
|
|
|
+ List<UserRole> userRoles = userRoleFacade.list(new QueryWrapper<UserRole>()
|
|
|
|
+ .eq("is_deleted", IsDeleteEnum.N.getKey())
|
|
|
|
+ .eq("status", StatusEnum.Enable.getKey())
|
|
|
|
+ .eq("id", userId));
|
|
|
|
+ //1.超级管理员
|
|
|
|
+ if(!ListUtil.isEmpty(userRoles)&&userRoles.contains(0)){
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ //添加菜单信息
|
|
|
|
+ return menuInfoDTO;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|