inleft
2022-02-09 9bcb19959eeb9da9bde2561e7278f6d0a55eb151
commit | author | age
9bcb19 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.sys.modular.auth.controller;
26
27 import cn.hutool.core.lang.Dict;
28 import com.anji.captcha.model.vo.CaptchaVO;
29 import com.anji.captcha.service.CaptchaService;
30 import org.springframework.context.annotation.Lazy;
31 import org.springframework.web.bind.annotation.GetMapping;
32 import org.springframework.web.bind.annotation.PostMapping;
33 import org.springframework.web.bind.annotation.RequestBody;
34 import org.springframework.web.bind.annotation.RestController;
35 import vip.xiaonuo.core.context.constant.ConstantContextHolder;
36 import vip.xiaonuo.core.context.login.LoginContextHolder;
37 import vip.xiaonuo.core.exception.AuthException;
38 import vip.xiaonuo.core.exception.enums.AuthExceptionEnum;
39 import vip.xiaonuo.core.pojo.response.ResponseData;
40 import vip.xiaonuo.core.pojo.response.SuccessResponseData;
41 import vip.xiaonuo.sys.modular.auth.service.AuthService;
42
43 import javax.annotation.Resource;
44
45 /**
46  * 登录控制器
47  *
48  * @author xuyuxiang
49  * @date 2020/3/11 12:20
50  */
51 @RestController
52 public class SysLoginController {
53
54     @Resource
55     private AuthService authService;
56
57     @Lazy
58     @Resource
59     private CaptchaService captchaService;
60
61     /**
62      * 获取是否开启租户的标识
63      *
64      * @author xuyuxiang
65      * @date 2020/9/4
66      */
67     @GetMapping("/getTenantOpen")
68     public ResponseData getTenantOpen() {
69         return new SuccessResponseData(ConstantContextHolder.getTenantOpenFlag());
70     }
71
72     /**
73      * 账号密码登录
74      *
75      * @author xuyuxiang
76      * @date 2020/3/11 15:52
77      */
78     @PostMapping("/login")
79     public ResponseData login(@RequestBody Dict dict) {
80         String account = dict.getStr("account");
81         String password = dict.getStr("password");
82         String tenantCode = dict.getStr("tenantCode");
83
84         //检测是否开启验证码
85         if (ConstantContextHolder.getCaptchaOpenFlag()) {
86             verificationCode(dict.getStr("code"));
87         }
88
89         //如果系统开启了多租户开关,则添加租户的临时缓存
90         if (ConstantContextHolder.getTenantOpenFlag()) {
91             authService.cacheTenantInfo(tenantCode);
92         }
93
94         String token = authService.login(account, password);
95         return new SuccessResponseData(token);
96     }
97
98     /**
99      * 退出登录
100      *
101      * @author xuyuxiang
102      * @date 2020/3/16 15:02
103      */
104     @GetMapping("/logout")
105     public void logout() {
106         authService.logout();
107     }
108
109     /**
110      * 获取当前登录用户信息
111      *
112      * @author xuyuxiang
113      * @date 2020/3/23 17:57
114      */
115     @GetMapping("/getLoginUser")
116     public ResponseData getLoginUser() {
117         return new SuccessResponseData(LoginContextHolder.me().getSysLoginUserUpToDate());
118     }
119
120     /**
121      * 获取验证码开关
122      *
123      * @author Jax
124      * @date 2021/1/21 15:27
125      */
126     @GetMapping("/getCaptchaOpen")
127     public ResponseData getCaptchaOpen() {
128         return new SuccessResponseData(ConstantContextHolder.getCaptchaOpenFlag());
129     }
130
131     /**
132      * 校验验证码
133      *
134      * @author Jax
135      * @date 2021/1/21 15:27
136      */
137     private boolean verificationCode(String code) {
138         CaptchaVO vo = new CaptchaVO();
139         vo.setCaptchaVerification(code);
140         if (!captchaService.verification(vo).isSuccess()) {
141             throw new AuthException(AuthExceptionEnum.CONSTANT_EMPTY_ERROR);
142         }
143         return true;
144     }
145
146 }