zj
2025-10-17 bbc4713b23778aebc1eb3d46cb04d539179d883d
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package com.yami.trading.admin.controller.googleAuth;
 
import com.yami.trading.admin.controller.googleAuth.model.AdminGoogleAuthModel;
import com.yami.trading.admin.controller.googleAuth.model.AdminGoogleAuthUnBindModel;
import com.yami.trading.admin.controller.googleAuth.model.SuperGoogleAuthBindModel;
import com.yami.trading.admin.controller.googleAuth.model.SuperGoogleAuthUnBindModel;
import com.yami.trading.bean.model.Log;
import com.yami.trading.bean.syspara.domain.Syspara;
import com.yami.trading.common.constants.Constants;
import com.yami.trading.common.domain.Result;
import com.yami.trading.common.exception.BusinessException;
import com.yami.trading.common.exception.YamiShopBindException;
import com.yami.trading.common.util.GoogleAuthenticator;
import com.yami.trading.common.util.IPHelper;
import com.yami.trading.common.util.StringUtils;
import com.yami.trading.security.common.util.SecurityUtils;
import com.yami.trading.service.syspara.SysparaService;
import com.yami.trading.service.system.LogService;
import com.yami.trading.sys.model.SysUser;
import com.yami.trading.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
@Api(tags = "root 谷歌验证码")
@RestController
@CrossOrigin
@Slf4j
@RequestMapping(value = "adminGoogleAuthAction")
public class RootGoogleAuthController {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysparaService sysparaService;
    @Autowired
    LogService logService;
 
    /**
     * 系统配置-admin谷歌验证器
     */
    @ApiOperation("获取admin谷歌验证器绑定状态")
    @GetMapping(value = "getAdminGoogleAuth")
    public Result getAdminGoogleAuth(HttpServletRequest request) {
        if (!"root".equals(SecurityUtils.getSysUser().getUsername())) {
            throw new BusinessException("权限不足");
        }
        SysUser secUser = sysUserService.getByUserName("admin");
        boolean google_auth_bind = secUser.isGoogleAuthBind();
        Map<String, Object> map = new HashMap<>();
        map.put("googleAuthBind", google_auth_bind);
        return Result.succeed(map);
    }
 
    /**
     * admin谷歌验证器-绑定
     */
    @ApiOperation("admin谷歌验证器-绑定")
    @PostMapping(value = "adminGoogleAuthBind")
    public Result adminGoogleAuthBind(@RequestBody @Valid AdminGoogleAuthModel model) {
        if (!"root".equals(SecurityUtils.getSysUser().getUsername())) {
            throw new BusinessException("权限不足");
        }
        Syspara superSecret = sysparaService.find("super_google_auth_secret");
        if (superSecret == null || StringUtils.isEmptyString(superSecret.getCode())) {
            throw new BusinessException("超级验证器尚未设置");
        }
        long t = System.currentTimeMillis();
        GoogleAuthenticator ga = new GoogleAuthenticator();
        ga.setWindowSize(5); // should give 5 * 30 seconds of grace...
        boolean checkSuperCode = ga.check_code(superSecret.getSvalue(), Long.valueOf(model.getSuperGoogleAuthCode()), t);
        if (!checkSuperCode) {
            throw new YamiShopBindException("超级管理员谷歌验证码错误");
        }
        boolean checkCode = ga.check_code(model.getGoogleAuthSecret(), Long.valueOf(model.getGoogleAuthCode()), t);
        if (!checkCode) {
            throw new YamiShopBindException("谷歌验证码错误");
        }
        SysUser secUser = sysUserService.getByUserName("admin");
        secUser.setGoogleAuthBind(true);
        secUser.setGoogleAuthSecret(model.getGoogleAuthSecret());
        sysUserService.updateById(secUser);
        saveLog(secUser, SecurityUtils.getSysUser().getUsername(), "ip:" + IPHelper.getIpAddr() + "admin谷歌验证器绑定");
        return Result.succeed();
    }
 
    public void saveLog(SysUser secUser, String operator, String context) {
        Log log = new Log();
        log.setCategory(Constants.LOG_CATEGORY_OPERATION);
        log.setOperator(operator);
        log.setUsername(secUser.getUsername());
        log.setUserId(secUser.getUserId() + "");
        log.setLog(context);
        log.setCreateTime(new Date());
        logService.save(log);
    }
 
    /**
     * admin谷歌验证器-解绑
     */
 
    @ApiOperation(" admin谷歌验证器-解绑")
    @PostMapping(value = "adminGoogleAuthUnBind")
    public Result adminGoogleAuthUnBind(@RequestBody @Valid AdminGoogleAuthUnBindModel model) {
        if (!"root".equals(SecurityUtils.getSysUser().getUsername())) {
            throw new BusinessException("权限不足");
        }
        Syspara superSecret = this.sysparaService.find("super_google_auth_secret");
        if (superSecret == null || StringUtils.isEmptyString(superSecret.getSvalue())) {
            throw new BusinessException("超级验证器尚未设置");
        }
        long t = System.currentTimeMillis();
        GoogleAuthenticator ga = new GoogleAuthenticator();
        ga.setWindowSize(5); // should give 5 * 30 seconds of grace...
        boolean checkCode = ga.check_code(superSecret.getSvalue(), Long.valueOf(model.getSuperGoogleAuthCode()), t);
        if (!checkCode) {
            throw new YamiShopBindException("超级管理员谷歌验证码错误");
        }
        SysUser secUser = sysUserService.getByUserName("admin");
        secUser.setGoogleAuthBind(false);
        secUser.setGoogleAuthSecret("");
        sysUserService.updateById(secUser);
        saveLog(secUser, SecurityUtils.getSysUser().getUsername(), "ip:" + IPHelper.getIpAddr() + "admin谷歌验证器绑定");
        return Result.succeed();
    }
 
    /**
     * 系统配置-超级谷歌验证码
     */
    @GetMapping(value = "getUpdateSuperGoogleAuth")
    @ApiOperation("获取系统配置-超级谷歌验证码绑定状态")
    public Result toUpdateSuperGoogleAuth() {
        if (!"root".equals(SecurityUtils.getSysUser().getUsername())) {
            throw new BusinessException("权限不足");
        }
        Syspara superSecret = this.sysparaService.find("super_google_auth_secret");
        boolean google_auth_bind = superSecret != null && !StringUtils.isEmptyString(superSecret.getSvalue());
        Map<String, Object> map = new HashMap<>();
        map.put("googleAuthBind", google_auth_bind);
        return Result.succeed(map);
    }
 
    /**
     * 系统配置-超级谷歌验证器-绑定
     */
    @PostMapping(value = "superGoogleAuthBind")
    @ApiOperation("系统配置-超级谷歌验证器-绑定")
    public Result superGoogleAuthBind(@RequestBody @Valid SuperGoogleAuthBindModel request) {
            if (!"root".equals(SecurityUtils.getSysUser().getUsername())) {
                throw new BusinessException("权限不足");
            }
            String  google_auth_secret= request.getSuperGoogleAuthSecret();
            if (StringUtils.isEmptyString(google_auth_secret)) {
                throw new BusinessException("密匙不能为空");
            }
            String super_google_auth_code =request.getSuperGoogleAuthCode();
            if (StringUtils.isEmptyString(super_google_auth_code)) {
                throw new BusinessException("超级谷歌验证码不能为空");
            }
            Syspara superSecret = this.sysparaService.find("super_google_auth_secret");
            if (superSecret != null && !StringUtils.isEmptyString(superSecret.getSvalue())) {
                throw new BusinessException("用户已绑定");
            }
            long t = System.currentTimeMillis();
            GoogleAuthenticator ga = new GoogleAuthenticator();
            ga.setWindowSize(5); // should give 5 * 30 seconds of grace...
            boolean checkCode = ga.check_code(google_auth_secret, Long.valueOf(request.getSuperGoogleAuthCode()), t);
            if (!checkCode) {
                throw new YamiShopBindException("超级管理员谷歌验证码错误");
            }
            superSecret.setSvalue(google_auth_secret);
            this.sysparaService.updateById(superSecret);
            SysUser secUser = sysUserService.getByUserName(SecurityUtils.getSysUser().getUsername());
            saveLog(secUser, SecurityUtils.getSysUser().getUsername(), "ip:" + IPHelper.getIpAddr() + "谷歌超级验证器绑定");
        return Result.succeed();
    }
 
    /**
     * 超级谷歌验证器-解绑
     */
    @PostMapping(value = "superGoogleAuthUnBind")
    @ApiOperation("超级谷歌验证器-解绑")
    public Result superGoogleAuthUnBind(@RequestBody @Valid SuperGoogleAuthUnBindModel request) {
        String message = "";
        String error = "";
        if (!"root".equals(SecurityUtils.getSysUser().getUsername())) {
            throw new BusinessException("权限不足");
        }
        Syspara superSecret = this.sysparaService.find("super_google_auth_secret");
        if (superSecret == null || StringUtils.isEmptyString(superSecret.getSvalue())) {
            throw new BusinessException("用户未绑定,无需解绑");
        }
        String secert = superSecret.getSvalue();
        String super_google_auth_code = request.getSuperGoogleAuthCode();
        if (StringUtils.isNullOrEmpty(super_google_auth_code)) {
            throw new BusinessException("超级谷歌验证码不能为空");
        }
        long t = System.currentTimeMillis();
        GoogleAuthenticator ga = new GoogleAuthenticator();
        ga.setWindowSize(5); // should give 5 * 30 seconds of grace...
        boolean checkCode = ga.check_code(superSecret.getSvalue(),
                Long.valueOf(request.getSuperGoogleAuthCode()), t);
        if (!checkCode) {
            throw new YamiShopBindException("超级管理员谷歌验证码错误");
        }
        superSecret.setSvalue("");
        sysparaService.updateById(superSecret);
        SysUser secUser = sysUserService.getByUserName(SecurityUtils.getSysUser().getUsername());
        saveLog(secUser, SecurityUtils.getSysUser().getUsername(), "ip:" + IPHelper.getIpAddr() + "谷歌超级验证器解绑");
        return Result.succeed();
    }
}