1
zj
2025-02-11 497b9bc9d0ef6f877325e74b02a7133de772ddae
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
package project.web.admin.googleauth;
 
import kernel.exception.BusinessException;
import kernel.util.JsonUtils;
import kernel.util.StringUtils;
import kernel.web.PageActionSupport;
import project.Constants;
import project.log.LogService;
import project.syspara.Syspara;
import project.syspara.SysparaService;
import project.user.googleauth.GoogleAuthService;
import security.SecUser;
import security.internal.SecUserService;
import util.GoogleAuthenticator;
 
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
 
@RestController
public class AdminGoogleAuthController extends PageActionSupport {
 
    private static final Logger logger = LoggerFactory.getLogger(AdminGoogleAuthController.class);
    
    @Autowired
    private SysparaService sysparaService;
    
    @Autowired
    private SecUserService secUserService;
    
    @Autowired
    protected LogService logService;
    
    @Autowired
    private GoogleAuthService googleAuthService;
    
    private final String action = "normal/adminGoogleAuthAction!";
    
    /**
     * 点击登录名里面的
     * 谷歌验证器
     */
    @RequestMapping(value = action + "toUpdateLoginGoogleAuth.action") 
    public ModelAndView toUpdateLoginGoogleAuth(HttpServletRequest request) {
        
        String message = "";
        String error = "";
        if("admin".equals(this.getUsername_login())) {
            this.error = "请联系管理员操作";
            try {
                this.getResponse().sendRedirect("/admin/normal/indexAction!view.action");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        SecUser secUser = secUserService.findUserByLoginName(this.getUsername_login());
        Boolean google_auth_bind = secUser.isGoogle_auth_bind();
        ModelAndView model = new ModelAndView();
        model.addObject("username", secUser.getUsername());
        model.addObject("message", message);
        model.addObject("error", error);
        model.addObject("google_auth_bind", google_auth_bind);
        model.setViewName("google_auth_login");
        return model;
    }
    
    /**
     * 点击登录名里面的
     * 谷歌验证器 - 生成密钥
     */
    @RequestMapping(value = action + "getLoginSecret.action")
    public String getLoginSecret() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            if("admin".equals(this.getUsername_login())) {
                throw new BusinessException("请联系管理员操作");
            }
            SecUser secUser = secUserService.findUserByLoginName(this.getUsername_login());
            // 未绑定
            if (!secUser.isGoogle_auth_bind()) {
                String secretKey = GoogleAuthenticator.generateSecretKey();
                resultMap.put("google_auth_secret", secretKey);
                resultMap.put("google_auth_url", googleAuthService.getGoogleAuthUrl(secUser.getUsername(), secretKey));
            }else {
                throw new BusinessException("已绑定谷歌验证器");
            }
            resultMap.put("google_auth_bind", secUser.isGoogle_auth_bind());
            resultMap.put("code", 200);
        } catch (BusinessException e) {
            resultMap.put("code", 500);
            resultMap.put("message", e.getMessage());
        } catch (Throwable t) {
            logger.error(" error ", t);
            resultMap.put("code", 500);
            resultMap.put("message", "程序错误");
        }
        return JsonUtils.getJsonString(resultMap);
    }
    
    /**
     * 点击登录名里面的
     * 谷歌验证器 - 绑定
     */
    @RequestMapping(value = action + "googleLoginAuthBind.action")
    public ModelAndView googleLoginAuthBind(HttpServletRequest request) {
        String message = "";
        String error = "";
        try {
            
            String google_auth_secret = request.getParameter("google_auth_secret");
            String google_auth_code = request.getParameter("google_auth_code");
            
            //admin只能通过超级签操作
            if("admin".equals(this.getUsername_login())) {
                throw new BusinessException("请联系管理员操作");
            }
            boolean checkCode = googleAuthService.saveGoogleAuthBind(this.getUsername_login(), 
                    google_auth_secret, google_auth_code);
            if(!checkCode) {
                throw new BusinessException("验证码错误,或请刷新二维码重新进行扫描");
            }
            SecUser secUser = this.secUserService.findUserByLoginName(this.getUsername_login());
            saveLog(secUser,this.getUsername_login(),"ip:"+this.getIp()+"谷歌验证器绑定");
            message="绑定成功";
        } catch (BusinessException e) {
            error = e.getMessage();
        } catch (Exception e) {
            logger.error("error ", e);
            error = "程序错误";
        }
        
        ModelAndView model = new ModelAndView();
        model.addObject("message", message);
        model.addObject("error", error);
        model.setViewName("redirect:/" + action + "toUpdateLoginGoogleAuth.action");
        return model;
    }
    
    
    /**
     * 系统配置-超级谷歌验证码
     */
    @RequestMapping(value = action + "toUpdateSuperGoogleAuth.action")
    public ModelAndView toUpdateSuperGoogleAuth(HttpServletRequest request) {
        if(!"root".equals(this.getUsername_login())) {
            throw new BusinessException("权限不足");
        }
        
        String message = request.getParameter("message");
        String error = request.getParameter("error");
        Syspara superSecret = this.sysparaService.find("super_google_auth_secret");
        boolean google_auth_bind = superSecret != null && !StringUtils.isEmptyString(superSecret.getValue());
        
        ModelAndView model = new ModelAndView();
        model.addObject("message", message);
        model.addObject("error", error);
        model.addObject("google_auth_bind", google_auth_bind);
        model.setViewName("google_auth_super");
        return model;
    }
    
    
    /**
     * 超级谷歌验证码-生成密钥
     */
    @RequestMapping(value = action + "getSuperSecret.action")
    public String getSuperSecret() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            if(!"root".equals(this.getUsername_login())) {
                throw new BusinessException("权限不足");
            }
            Syspara superSecret = this.sysparaService.find("super_google_auth_secret");
            if(superSecret==null||StringUtils.isEmptyString(superSecret.getValue())) {
                // 未绑定则
                String secretKey = GoogleAuthenticator.generateSecretKey();
                resultMap.put("google_auth_secret", secretKey);
                resultMap.put("google_auth_url", googleAuthService.getGoogleAuthUrl("super", secretKey));
            }else {
                throw new BusinessException("已绑定谷歌验证器");
            }
            resultMap.put("code", 200);
        } catch (BusinessException e) {
            resultMap.put("code", 500);
            resultMap.put("message", e.getMessage());
        } catch (Throwable t) {
            logger.error(" error ", t);
            resultMap.put("code", 500);
            resultMap.put("message", "程序错误");
        }
        return JsonUtils.getJsonString(resultMap);
    }
    
    /**
     * 系统配置-超级谷歌验证器-绑定
     */
    @RequestMapping(value = action + "superGoogleAuthBind.action")
    public ModelAndView superGoogleAuthBind(HttpServletRequest request) {
        String message = "";
        String error = "";
        try {
            if(!"root".equals(this.getUsername_login())) {
                throw new BusinessException("权限不足");
            }
            
            String google_auth_secret = request.getParameter("google_auth_secret");
            if (StringUtils.isEmptyString(google_auth_secret)) {
                throw new BusinessException("密匙不能为空");
            }
            
            String super_google_auth_code = request.getParameter("super_google_auth_code");
            if (StringUtils.isEmptyString(super_google_auth_code)) {
                throw new BusinessException("超级谷歌验证码不能为空");
            }
            
            String super_google_auth_secret = request.getParameter("super_google_auth_secret");
            Syspara superSecret = this.sysparaService.find("super_google_auth_secret");
            if(superSecret!=null&&!StringUtils.isEmptyString(superSecret.getValue())) {
                throw new BusinessException("用户已绑定");
            }
            boolean checkCode = googleAuthService.checkCode(google_auth_secret, super_google_auth_code);
            if(!checkCode) {
                throw new BusinessException("验证码错误,或请刷新二维码重新进行扫描");
            }
            superSecret.setValue(google_auth_secret);
            this.sysparaService.update(superSecret);
            SecUser secUser = this.secUserService.findUserByLoginName(this.getUsername_login());
            saveLog(secUser,this.getUsername_login(),"ip:"+this.getIp()+"谷歌超级验证器绑定");
            secUser.setGoogle_auth_secret(google_auth_secret);
            secUserService.update(secUser);
            super_google_auth_code=null;
            message="绑定成功";
        } catch (BusinessException e) {
            error = e.getMessage();
        } catch (Exception e) {
            logger.error("error ", e);
            error = "程序错误";
        }
        
        ModelAndView model = new ModelAndView();
        model.addObject("message", message);
        model.addObject("error", error);
        model.setViewName("redirect:/" + action + "toUpdateSuperGoogleAuth.action");
        return model;
    }
    
    /**
     * 超级谷歌验证器-解绑
     */
    @RequestMapping(value = action + "superGoogleAuthUnBind.action")
    public ModelAndView superGoogleAuthUnBind(HttpServletRequest request) {
        String message = "";
        String error = "";
        try {
            if(!"root".equals(this.getUsername_login())) {
                throw new BusinessException("权限不足");
            }
            
            Syspara superSecret = this.sysparaService.find("super_google_auth_secret");
            if(superSecret==null||StringUtils.isEmptyString(superSecret.getValue())) {
                throw new BusinessException("用户未绑定,无需解绑");
            }
            String secert = superSecret.getValue();
            String super_google_auth_code = request.getParameter("super_google_auth_code");
            if (StringUtils.isNullOrEmpty(super_google_auth_code)) {
                throw new BusinessException("超级谷歌验证码不能为空");
            }
            boolean checkCode = this.googleAuthService.checkCode(secert, super_google_auth_code);
            if(!checkCode) {
                throw new BusinessException("验证码错误,或请刷新二维码重新进行扫描");
            }
            superSecret.setValue("");
            sysparaService.update(superSecret);
            
            SecUser secUser = this.secUserService.findUserByLoginName(this.getUsername_login());
            saveLog(secUser,this.getUsername_login(),"ip:"+this.getIp()+"谷歌超级验证器解绑");
            message="解绑成功";
        } catch (BusinessException e) {
            error = e.getMessage();
        } catch (Exception e) {
            logger.error("error ", e);
            error = "程序错误";
        }
        
        ModelAndView model = new ModelAndView();
        model.addObject("message", message);
        model.addObject("error", error);
        model.setViewName("redirect:/" + action + "toUpdateSuperGoogleAuth.action");
        return model;
    }
    
    
    /**
     * 系统配置-admin谷歌验证器
     */
    @RequestMapping(value = action + "toUpdateAdminGoogleAuth.action")
    public ModelAndView toUpdateAdminGoogleAuth(HttpServletRequest request) {
        String message = request.getParameter("message");
        String error = request.getParameter("error");
        String google_auth_secret = request.getParameter("google_auth_secret");
        String google_auth_url = request.getParameter("google_auth_url");
                
        if(!"root".equals(this.getUsername_login())) {
            throw new BusinessException("权限不足");
        }
        
        SecUser secUser = secUserService.findUserByLoginName("admin");
        boolean google_auth_bind = secUser.isGoogle_auth_bind();
        
        ModelAndView model = new ModelAndView();
        model.addObject("message", message);
        model.addObject("error", error);
        model.addObject("google_auth_secret", google_auth_secret);
        model.addObject("google_auth_url", google_auth_url);
        model.addObject("google_auth_bind", google_auth_bind + "");
        model.setViewName("google_auth_admin");
        return model;
    }
        
    /**
     * admin谷歌验证器-绑定
     */
    @RequestMapping(value = action + "adminGoogleAuthBind.action")
    public ModelAndView adminGoogleAuthBind(HttpServletRequest request) {
        String super_google_auth_code = request.getParameter("super_google_auth_code");
        String google_auth_code = request.getParameter("google_auth_code");
        String google_auth_secret = request.getParameter("google_auth_secret");
        String google_auth_url = request.getParameter("google_auth_url");
        String google_auth_bind = request.getParameter("google_auth_bind");
        
        String message = "";
        String error = "";
        
        try {
            
            if(!"root".equals(this.getUsername_login())) {
                throw new BusinessException("权限不足");
            }
            
            Syspara superSecret = this.sysparaService.find("super_google_auth_secret");
            if(superSecret==null||StringUtils.isEmptyString(superSecret.getValue())) {
                throw new BusinessException("超级验证器尚未设置");
            }
                        
            boolean checkSuperCode = this.googleAuthService.checkCode(superSecret.getValue(), super_google_auth_code);
            if(!checkSuperCode) {
                throw new BusinessException("超级验证码错误");
            }
            
            boolean checkCode = googleAuthService.saveGoogleAuthBind("admin", google_auth_secret, google_auth_code);
            if(!checkCode) {
                throw new BusinessException("验证码错误,或请刷新二维码重新进行扫描");
            }
            
            SecUser secUser = this.secUserService.findUserByLoginName("admin");
            boolean google_auth_bind_bo = secUser.isGoogle_auth_bind();
            google_auth_bind = google_auth_bind_bo + "";
                        
            saveLog(secUser,this.getUsername_login(),"ip:"+this.getIp()+"admin谷歌验证器绑定");
            super_google_auth_code=null;
            
            message="绑定成功";
            
        } catch (BusinessException e) {
            error = e.getMessage();
        } catch (Exception e) {
            logger.error("error ", e);
            error = "程序错误";
        }
        
        ModelAndView model = new ModelAndView();
        model.addObject("message", message);
        model.addObject("error", error);
        model.addObject("google_auth_secret", google_auth_secret);
        model.addObject("google_auth_url", google_auth_url);
        model.addObject("google_auth_bind", google_auth_bind);
        model.setViewName("redirect:/" + action + "toUpdateAdminGoogleAuth.action");
        return model;
    }
        
    /**
     * admin谷歌验证器-解绑
     */
    @RequestMapping(value = action + "adminGoogleAuthUnBind.action")
    public ModelAndView adminGoogleAuthUnBind(HttpServletRequest request) {
        String message = "";
        String error = "";
        String super_google_auth_code = request.getParameter("super_google_auth_code");
        try {
            if(!"root".equals(this.getUsername_login())) {
                throw new BusinessException("权限不足");
            }
            
            Syspara superSecret = this.sysparaService.find("super_google_auth_secret");
            if(superSecret==null || StringUtils.isEmptyString(superSecret.getValue())) {
                throw new BusinessException("超级验证器尚未设置");
            }
            
            boolean checkCode = this.googleAuthService.checkCode(superSecret.getValue(), super_google_auth_code);
            if(!checkCode) {
                throw new BusinessException("超级验证码错误");
            }
            googleAuthService.saveGoogleAuthUnBind("admin");
            SecUser secUser = this.secUserService.findUserByLoginName("admin");
            saveLog(secUser,this.getUsername_login(),"ip:"+this.getIp()+"admin谷歌验证器解绑");
            super_google_auth_code = null;
            message = "解绑成功";
        } catch (BusinessException e) {
            error = e.getMessage();
        } catch (Exception e) {
            logger.error("error ", e);
            error = "程序错误";
        }
        
        ModelAndView model = new ModelAndView();
        model.addObject("super_google_auth_code", super_google_auth_code);
        model.addObject("message", message);
        model.addObject("error", error);
        model.setViewName("redirect:/" + action + "toUpdateAdminGoogleAuth.action");
        return model;
    }
    
    /**
     * ADMIN谷歌验证器-生成密钥
     */
    @RequestMapping(value = action + "getAdminSecret.action")
    public String getAdminSecret() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            if(!"root".equals(this.getUsername_login())) {
                throw new BusinessException("权限不足");
            }
            SecUser secUser = secUserService.findUserByLoginName("admin");
            // 未绑定
            if (!secUser.isGoogle_auth_bind()) {
                String secretKey = GoogleAuthenticator.generateSecretKey();
                resultMap.put("google_auth_secret", secretKey);
                resultMap.put("google_auth_url", googleAuthService.getGoogleAuthUrl(secUser.getUsername(), secretKey));
            }else {
                throw new BusinessException("已绑定谷歌验证器");
            }
            resultMap.put("google_auth_bind", secUser.isGoogle_auth_bind());
            resultMap.put("code", 200);
        } catch (BusinessException e) {
            resultMap.put("code", 500);
            resultMap.put("message", e.getMessage());
        } catch (Throwable t) {
            logger.error(" error ", t);
            resultMap.put("code", 500);
            resultMap.put("message", "程序错误");
        }
        return JsonUtils.getJsonString(resultMap);
    }
 
    /**
     * 系统用户管理- 谷歌验证器
     */
    @RequestMapping(value = action + "toUpdateGoogleAuth.action") 
    public ModelAndView toUpdateGoogleAuth(HttpServletRequest request) {
        String message = request.getParameter("message");
        String error = request.getParameter("error");
        String username = request.getParameter("username");
        String google_auth_secret = request.getParameter("google_auth_secret");
        String google_auth_url = request.getParameter("google_auth_url");
        String from_page = request.getParameter("from_page");
        SecUser secUser = secUserService.findUserByLoginName(username);
        Boolean google_auth_bind = secUser.isGoogle_auth_bind();
        ModelAndView model = new ModelAndView();
        model.addObject("username", username);
        model.addObject("message", message);
        model.addObject("error", error);
        model.addObject("google_auth_bind", google_auth_bind);
        model.addObject("google_auth_secret", google_auth_secret);
        model.addObject("google_auth_url", google_auth_url);
        model.addObject("from_page", from_page);
        model.setViewName("google_auth");
        return model;
    }
    
    /**
     * 系统用户管理- 谷歌验证器-解绑
     */
    @RequestMapping(value = action + "googleAuthUnBind.action") 
    public ModelAndView googleAuthUnBind(HttpServletRequest request) {
        String message = "";
        String error = "";
        
        String username = request.getParameter("username");
        String super_google_auth_code = request.getParameter("super_google_auth_code");
        
        try {
            
            if("admin".equals(username) || "root".equals(username)) {
                throw new BusinessException("无法操作该用户");
            }
            
            Syspara superSecret = this.sysparaService.find("super_google_auth_secret");
            if(superSecret==null||StringUtils.isEmptyString(superSecret.getValue())) {
                throw new BusinessException("超级验证器尚未设置");
            }
            boolean checkCode = this.googleAuthService.checkCode(superSecret.getValue(), super_google_auth_code);
            if(!checkCode) {
                throw new BusinessException("超级验证码错误");
            }
            googleAuthService.saveGoogleAuthUnBind(username);
            SecUser secUser = this.secUserService.findUserByLoginName(username);
            saveLog(secUser,this.getUsername_login(),"ip:"+this.getIp()+"谷歌验证器解绑");
            super_google_auth_code = null;
            message="解绑成功";
        } catch (BusinessException e) {
            error = e.getMessage();
        } catch (Exception e) {
            logger.error("error ", e);
            error = "程序错误";
        }
        
        ModelAndView model = new ModelAndView();
        model.addObject("username", username);
        model.addObject("super_google_auth_code", super_google_auth_code);
        model.addObject("message", message);
        model.addObject("error", error);
        model.setViewName("redirect:/" + action + "toUpdateGoogleAuth.action");
        return model;
    }
 
    /**
     * 系统用户管理- 谷歌验证器-绑定
     */
    @RequestMapping(value = action + "googleAuthBind.action") 
    public ModelAndView googleAuthBind(HttpServletRequest request) {
        
        String message = "";
        String error = "";
        
        String username = request.getParameter("username");
        String super_google_auth_code = request.getParameter("super_google_auth_code");
        
        String google_auth_secret = request.getParameter("google_auth_secret");
        String google_auth_code = request.getParameter("google_auth_code");
 
        String google_auth_url = request.getParameter("google_auth_url");
        
        try {
            if("admin".equals(username)||"root".equals(username)) {
                throw new BusinessException("无法操作该用户");
            }
            Syspara superSecret = this.sysparaService.find("super_google_auth_secret");
            if(superSecret==null || StringUtils.isEmptyString(superSecret.getValue())) {
                throw new BusinessException("超级验证器尚未设置");
            }
            boolean checkSuperCode = this.googleAuthService.checkCode(superSecret.getValue(), super_google_auth_code);
            if(!checkSuperCode) {
                throw new BusinessException("超级验证码错误");
            }
            boolean checkCode = googleAuthService.saveGoogleAuthBind(username, google_auth_secret, google_auth_code);
            if(!checkCode) {
                throw new BusinessException("验证码错误,或请刷新二维码重新进行扫描");
            }
            SecUser secUser = this.secUserService.findUserByLoginName(username);
            saveLog(secUser,this.getUsername_login(),"ip:"+this.getIp()+"谷歌验证器绑定");
            super_google_auth_code=null;
            message="绑定成功";
        } catch (BusinessException e) {
            error = e.getMessage();
        } catch (Exception e) {
            logger.error("error ", e);
            error = "程序错误";
        }
 
        ModelAndView model = new ModelAndView();
        model.addObject("username", username);
        model.addObject("super_google_auth_code", super_google_auth_code);
        model.addObject("google_auth_secret", google_auth_secret);
        model.addObject("google_auth_url", google_auth_url);
        model.addObject("message", message);
        model.addObject("error", error);
        model.setViewName("redirect:/" + action + "toUpdateGoogleAuth.action");
        return model;
    }
    
    /**
     * 系统用户管理- 谷歌验证器-生成密钥
     */
    @RequestMapping(value = action + "getSecret.action") 
    public String getSecret(HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            
            String username = request.getParameter("username");
            if("admin".equals(username)||"root".equals(username)) {
                throw new BusinessException("无法操作该用户");
            }
            SecUser secUser = secUserService.findUserByLoginName(username);
            if (!secUser.isGoogle_auth_bind()) {// 未绑定则
                String secretKey = GoogleAuthenticator.generateSecretKey();
                resultMap.put("google_auth_secret", secretKey);
                resultMap.put("google_auth_url", googleAuthService.getGoogleAuthUrl(secUser.getUsername(), secretKey));
            }else {
                throw new BusinessException("已绑定谷歌验证器");
            }
            resultMap.put("google_auth_bind", secUser.isGoogle_auth_bind());
            resultMap.put("code", 200);
        } catch (BusinessException e) {
            resultMap.put("code", 500);
            resultMap.put("message", e.getMessage());
        } catch (Throwable t) {
            logger.error(" error ", t);
            resultMap.put("code", 500);
            resultMap.put("message", "程序错误");
        }
 
        return JsonUtils.getJsonString(resultMap);
    }
    
    /**
     * 登录时 校验谷歌验证码
     */
    @RequestMapping(action + "checkGoogleAuthCodeForLogin.action")
    public ModelAndView checkGoogleAuthCodeForLogin(HttpServletRequest request) {
        
        String google_auth_code = request.getParameter("google_auth_code");
        
        ModelAndView model = new ModelAndView();
        String username = this.getUsername_login();
        try {
            Syspara para = sysparaService.find("open_google_auth_code");
            if (null == para || para.getValue().equals("true")) {
                googleAuthService.checkGoogleAuthCodeForLogin(this.getIp(), username, google_auth_code,
                        this.getRequest().getRequestURI());
            }
            model.setViewName("redirect:/normal/LoginSuccessAction!view.action");
            return model;
            
        } catch (BusinessException e) {
            model.addObject("error", e.getMessage());
            model.addObject("username", username);
            model.setViewName("include/google_auth_code");
            return model;
        } catch (Throwable t) {
            logger.error(" error ", t);
            model.addObject("username", username);
            model.addObject("error", "验证码错误");
            model.setViewName("include/google_auth_code");
            return model;
        }
    }
    
    public void saveLog(SecUser secUser, String operator,String context) {
        project.log.Log log = new project.log.Log();
        log.setCategory(Constants.LOG_CATEGORY_OPERATION);
        log.setOperator(operator);
        log.setUsername(secUser.getUsername());
        log.setPartyId(secUser.getPartyId());
        log.setLog(context);
        log.setCreateTime(new Date());
        logService.saveSync(log);
    }    
}