puzhibing
2023-06-13 7860e5cb6db60aeb82c640651998f8294635df5b
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
package com.dsh.guns.modular.system.controller.system;
 
import com.dsh.course.feignClient.auth.UserClient;
import com.dsh.course.feignClient.auth.model.GetTokenRequest;
import com.dsh.course.feignClient.auth.model.TokenResponse;
import com.dsh.course.model.node.MenuNode;
import com.dsh.guns.config.UserExt;
import com.dsh.guns.modular.system.controller.util.MD5;
import com.dsh.guns.modular.system.model.Role;
import com.dsh.guns.modular.system.service.IRelationService;
import com.dsh.guns.modular.system.service.IRoleService;
import com.dsh.guns.modular.system.util.CacheUtil;
import com.google.code.kaptcha.Constants;
import com.dsh.guns.core.base.controller.BaseController;
import com.dsh.guns.core.common.exception.InvalidKaptchaException;
import com.dsh.guns.core.util.KaptchaUtil;
import com.dsh.guns.core.util.SinataUtil;
import com.dsh.guns.modular.system.model.User;
import com.dsh.guns.modular.system.service.IMenuService;
import com.dsh.guns.modular.system.service.IUserService;
import com.dsh.guns.modular.system.util.AESUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
 
/**
 * 登录控制器
 *
 * @author fengshuonan
 * @Date 2017年1月10日 下午8:25:24
 */
@Controller
public class LoginController extends BaseController {
 
    @Autowired
    private IMenuService menuService;
 
    @Autowired
    private IUserService userService;
 
    @Autowired
    private IRelationService relationService;
 
    @Autowired
    private IRoleService roleService;
 
    @Resource
    private UserClient userClient;
 
    private Map<String, Long> loginTime = new HashMap<>();
 
    private Map<String, Integer> loginFailures = new HashMap<>();
 
    private List<String> ips = Arrays.asList("127.0.0.1","192.168.0.14","192.168.0.20","192.168.0.23","192.168.0.214","192.168.0.239","192.168.0.13","192.168.0.214");
 
 
 
 
 
    /**
     * 跳转到主页
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("tips", "");
        //获取菜单列表
        User user = UserExt.getUser();
        if(null == user){
            return "/login.html";
        }
        List<String> roleList = Arrays.asList(user.getRoleid().split(","));
        if (roleList == null || roleList.size() == 0) {
            model.addAttribute("tips", "该用户没有角色,无法登陆");
            return "/login.html";
        }
        Integer language = UserExt.getLanguage();
        List<MenuNode> menus = menuService.getMenusByRoleIds(roleList);
        List<MenuNode> titles = MenuNode.buildTitle(menus);
        if(language==1 || language==null){
 
        }else if(language==2){
            for (MenuNode menu : menus) {
                menu.setName(menu.getEname());
            }
        }else if(language==3){
            for (MenuNode menu : menus) {
                menu.setName(menu.getBname());
            }
        }
 
 
 
        List<String> resUrlsByRoleId = menuService.getResUrlsByRoleId(Integer.valueOf(roleList.get(0)));
        CacheUtil.menus.put(user.getAccount(), resUrlsByRoleId);
 
        if (SinataUtil.isEmpty(user)){
            String a ="请重新登录";
            if(language==2){
                a="Please log in again";
            }else if(language==3){
                a="Silakan log masuk lagi";
            }
 
            model.addAttribute("tips", a);
            return "/login.html";
        }
        //设置上级ID
        if (SinataUtil.isNotEmpty(user.getObjectId())){
//            ShiroKit.getUser().setObjectId(user.getObjectId());
        }
        //设置角色
//        ShiroKit.getUser().setRoleType(user.getRoleType());
 
        model.addAttribute("titles", titles);
 
        Role role = roleService.getBaseMapper().selectById(roleList.get(0));
 
        User byId = userService.getById(user.getId());
        String a = "您的密码已经90天未更换了,请及时更换密码!!!";
        if(language==2){
            a="Your password has not been changed for 90 days, please change your password in time!!";
        }else if(language==3){
            a="Kata sandi Anda belum diubah selama 90 hari, silakan merubah kata sandi Anda pada waktu!!";
        }
        //获取用户头像 Obtain user profile
        String avatar = byId.getAvatar();
        model.addAttribute("roleNames", role.getName());
        model.addAttribute("language", language);
        model.addAttribute("avatar", avatar);
        model.addAttribute("userName", user.getName());
        model.addAttribute("type", 0);
        model.addAttribute("passwordHint",
                null == user.getPassWordUpdate()
                        || user.getPassWordUpdate().getTime() + 7776000000L <= System.currentTimeMillis()
                        ? a: "");
        return "/index.html";
    }
 
    /**
     * 跳转到登录页面
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        if (false || false) {
            return REDIRECT + "/";
        } else {
            return "/login.html" ;
        }
    }
 
    /**
     * 点击登录执行的动作
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String loginVali(String username, String password, String remember, Model model, HttpServletRequest request,Integer language) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值,第一个ip才是真实ip
            //After multiple reverse proxies, there will be multiple IP values, and the first IP is the real IP.
            if (ip.indexOf(",") != -1) {
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
//        if (!ips.contains(ip)) {
//            model.addAttribute("tips", "请在特定的网络下使用系统");
//            return "/login.html";
//        }
 
        Integer f = loginFailures.get(username);
        f = f == null ? 0 : f;
        Long t = loginTime.get(username);
        t = null == t ? 0 : t;
        if (System.currentTimeMillis() - t > (30 * 60 * 1000)) {
            loginFailures.put(username, f = 1);
            loginTime.put(username, System.currentTimeMillis());
        } else {
            f++;
            loginFailures.put(username, f);
        }
 
//        if (f > 5 && (System.currentTimeMillis() - t) <= (30 * 60 * 1000)) {
//            model.addAttribute("tips", "登录次数过多,请等30分钟再试!");
//            return "/login.html";
//        }
        //验证验证码是否正确
        //Verify if the verification code is correct.
        if (KaptchaUtil.getKaptchaOnOff()) {
            String kaptcha = super.getPara("kaptcha").trim();
            String code = (String) super.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
            if (Objects.isNull(kaptcha) || !kaptcha.equalsIgnoreCase(code)) {
                throw new InvalidKaptchaException();
            }
        }
        User user = userService.getByAccount(username);
        password = AESUtils.decrypt(password);
        String s = MD5.md5(password);
        if(!user.getPassword().equals(s)){
            model.addAttribute("tips", "Account password error");
            return "/login.html";
        }
 
 
        GetTokenRequest tokenRequest = new GetTokenRequest();
        tokenRequest.setUsername(username);
        tokenRequest.setPassword(password);
        tokenRequest.setObject(user);
        TokenResponse token = userClient.getToken(tokenRequest);
        super.getSession().setAttribute("user", user);
        super.getSession().setAttribute("username", user.getAccount());
        super.getSession().setAttribute("token", token);
        super.getSession().setAttribute("language",language);
 
        return REDIRECT + "/";
    }
 
        /**
         * 退出登录
         */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logOut() {
        deleteAllCookie();
        return REDIRECT + "/login";
    }
 
}