heheng
9 天以前 bd605ac1e79b73db3b501f323d6f4a10c0f27dd5
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.gkhy.exam.common.utils;
 
import com.gkhy.exam.common.domain.model.LoginUserDetails;
import com.gkhy.exam.common.exception.ApiException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 
public class SecurityUtils {
 
    /**
     * 是否为管理员
     *
     * @param userId 用户ID
     * @return 结果
     */
    public static boolean isAdmin(Long userId)
    {
        return userId != null && 1L == userId;
    }
 
    public static boolean adminUser(){
        if(getLoginUser() == null){
            return false;
        }
        if(getLoginUser().getUser().getId() == 1L){
            return true;
        }
        if(getLoginUser().getUser().getUserType() == 0){
            return true;
        }
       return false;
    }
 
    /**
     * 判断密码是否相同
     * @param rawPasword   真实密码
     * @param encodePassword  加密后密码
     * @return
     */
    public static boolean matchesPassword(String rawPasword,String encodePassword){
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        return passwordEncoder.matches(rawPasword,encodePassword);
    }
 
   /*
    **
     * 用户ID
     **/
    public static Long getUserId()
    {
        try
        {
            return getLoginUser().getUser().getId();
        }
        catch (Exception e)
        {
            throw new ApiException("获取用户ID异常");
        }
    }
 
 
 
    /*
     **
     * 用户ID
     **/
    public static Long getCompanyId()
    {
        try
        {
            return getLoginUser().getUser().getCompanyId();
        }
        catch (Exception e)
        {
            throw new ApiException("获取用户公司异常");
        }
    }
 
 
 
    /**
     * 获取用户账户
     **/
    public static String getUsername()
    {
        try
        {
            return getLoginUser().getUser().getUsername();
        }
        catch (Exception e)
        {
            throw new ApiException("获取用户账户异常");
        }
    }
 
    /**
     * 获取用户
     **/
    public static LoginUserDetails getLoginUser()
    {
        try
        {
            return (LoginUserDetails) getAuthentication().getPrincipal();
        }
        catch (Exception e)
        {
            throw new ApiException("获取用户信息异常");
        }
    }
 
 
    /**
     * 获取用户
     **/
    public static LoginUserDetails getLoginUserWithoutError()
    {
        try
        {
            return (LoginUserDetails) getAuthentication().getPrincipal();
        }
        catch (Exception e)
        {
            return null;
        }
    }
 
 
 
    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication()
    {
        return SecurityContextHolder.getContext().getAuthentication();
    }
 
    /**
     * 生成BCryptPasswordEncoder密码
     *
     * @param password 密码
     * @return 加密字符串
     */
    public static String encryptPassword(String password)
    {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(password);
    }
 
 
}