危化品全生命周期管理后端
kongzy
2024-08-22 0c73654f55844e34772732914af8cc1e247aab63
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.gkhy.hazmat.framework.web.service;
 
import cn.hutool.core.util.ObjectUtil;
import com.gkhy.hazmat.common.domain.entity.SysUser;
import com.gkhy.hazmat.common.domain.model.LoginUserDetails;
import com.gkhy.hazmat.common.enums.DeleteStatus;
import com.gkhy.hazmat.common.enums.UserStatus;
import com.gkhy.hazmat.common.enums.UserTypeEnum;
import com.gkhy.hazmat.common.exception.ApiException;
import com.gkhy.hazmat.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 用户验证处理
 */
@Slf4j
@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private SysUserService userService;
 
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user=userService.selectUserByUsername(username);
 
        if (ObjectUtil.isNull(user))
        {
            log.info("登录用户:{} 不存在.", username);
            throw new ApiException("登录用户不存在");
        }
        else if (DeleteStatus.DELETED.getCode().equals(user.getDelFlag()))
        {
            log.info("登录用户:{} 已被删除.", username);
            throw new ApiException("登录用户已被删除");
        }
        else if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
        {
            log.info("登录用户:{} 已被停用.", username);
            throw new ApiException("登录用户已被停用");
        }else if(!UserTypeEnum.SYSTEM_USER.getCode().equals(user.getUserType())&&user.getCompanyId()==null){
            log.info("登录用户:{} 公司信息缺失.", username);
            throw new ApiException("登录用户公司信息缺失");
        }
        List<GrantedAuthority> authorities=new ArrayList<>();
        if(UserTypeEnum.SYSTEM_USER.getCode().equals(user.getUserType())) {
            authorities.add(new SimpleGrantedAuthority("hazmat:manage:system"));
        }else if(UserTypeEnum.COMPANY_USER.getCode().equals(user.getUserType())){
            authorities.add(new SimpleGrantedAuthority("hazmat:manage:company"));
        }else if(UserTypeEnum.NORMAL_USER.getCode().equals(user.getUserType())){
            authorities.add(new SimpleGrantedAuthority("hazmat:manage:common"));
        }
        return new LoginUserDetails(user,authorities);
    }
}