yanderong
2026-07-11 6abd929890060d526516a77d5599dad2ddbd0172
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
package com.stylefeng.guns.modular.system.controller.system;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.code.kaptcha.Constants;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.exception.InvalidKaptchaException;
import com.stylefeng.guns.core.log.LogManager;
import com.stylefeng.guns.core.log.factory.LogTaskFactory;
import com.stylefeng.guns.core.node.MenuNode;
import com.stylefeng.guns.core.shiro.ShiroDbRealm;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.shiro.factory.IShiro;
import com.stylefeng.guns.core.util.ApiMenuFilter;
import com.stylefeng.guns.core.util.JwtTokenUtil;
import com.stylefeng.guns.core.util.KaptchaUtil;
import com.stylefeng.guns.core.util.SinataUtil;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.TCompanyMapper;
import com.stylefeng.guns.modular.system.dao.UserMapper;
import com.stylefeng.guns.modular.system.model.Menu;
import com.stylefeng.guns.modular.system.model.SysUserCompanyScope;
import com.stylefeng.guns.modular.system.model.TCompany;
import com.stylefeng.guns.modular.system.model.User;
import com.stylefeng.guns.modular.system.service.IMenuService;
import com.stylefeng.guns.modular.system.service.IRelationService;
import com.stylefeng.guns.modular.system.service.ISysUserCompanyScopeService;
import com.stylefeng.guns.modular.system.service.IUserService;
import com.stylefeng.guns.modular.system.util.HttpRequestUtil;
import com.stylefeng.guns.modular.system.util.PushURL;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
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 org.springframework.web.bind.annotation.ResponseBody;
 
import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
 
import static com.stylefeng.guns.core.support.HttpKit.getIp;
 
/**
 * 登录控制器
 *
 * @author fengshuonan
 * @Date 2017年1月10日 下午8:25:24
 */
@Slf4j
@Controller
public class LoginController extends BaseController {
 
    @Value("${embedded.route-switch-url}")
    private String routeSwitchUrl;
 
 
    // 快车相关菜单(isSpe=2时过滤)
    private static final List<String> SPE_MENUS = Arrays.asList(
        "快车订单", "快车改派管理", "快车车型设置"
    );
    // 出租车相关菜单(isTaxi=2时过滤)
    private static final List<String> TAXI_MENUS = Arrays.asList(
        "出租车订单", "出租车改派管理"
    );
    // 跨城相关菜单(isCross=2时过滤)
    private static final List<String> CROSS_MENUS = Arrays.asList(
        "跨城出行订单", "跨城改派管理", "跨城线路管理", "开通城市管理"
    );
    // 顺风车相关菜单(isHitchRide=2时过滤)
    private static final List<String> HITCH_RIDE_MENUS = Arrays.asList(
        "顺风车订单", "顺风车设置", "顺风车司机审核列表",
        "顺风车司机", "顺风车投诉", "顺风车评价"
    );
 
    @Autowired
    private IMenuService menuService;
    
    @Autowired
    private IUserService userService;
    
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    
    @Autowired
    private IShiro shiro;
    
    @Autowired
    private UserMapper userMapper;
 
    @Resource
    private TCompanyMapper companyMapper;
 
 
    private Map<String, Long> loginTime = new HashMap<>();
    
    private Map<String, Integer> loginFailures = new HashMap<>();
    
    @Autowired
    private IRelationService relationService;
 
    @Autowired
    private ISysUserCompanyScopeService userCompanyScopeService;
 
    @Autowired
    private ShiroDbRealm shiroDbRealm;
    
    
    /**
     * 跳转到主页
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model, String routeRedirect) throws UnsupportedEncodingException {
        ShiroUser shiroUser = ShiroKit.getUser();
        if (Objects.isNull(shiroUser)) {
            ShiroKit.getSubject().logout();
            model.addAttribute("tips", "请重新登录");
            return "/login.html";
        }
        //获取菜单列表
        List<Integer> roleList = shiroUser.getRoleList();
        if (roleList == null || roleList.size() == 0) {
            ShiroKit.getSubject().logout();
            model.addAttribute("tips", "该用户没有角色,无法登陆");
            return "/login.html";
        }
        List<MenuNode> menus = menuService.getMenusByRoleIds(roleList);
        // 根据企业经营范围过滤菜单(在构建树之前过滤)
        TCompany tCompany = companyMapper.selectById(shiroUser.getObjectId());
        if (tCompany != null && (tCompany.getType() == 2 || tCompany.getType() == 3)) {
            menus = filterMenusByBusinessScope(menus, tCompany);
        }
        List<MenuNode> titles = MenuNode.buildTitle(menus);
        titles = ApiMenuFilter.build(titles);
 
 
        Integer id = shiroUser.getId();
        User user = userService.selectById(id);
        if (SinataUtil.isEmpty(user)){
            ShiroKit.getSubject().logout();
            model.addAttribute("tips", "请重新登录");
            return "/login.html";
        }
        //设置角色
        shiroUser.setRoleType(user.getRoleType());
 
        model.addAttribute("titles", titles);
        model.addAttribute("companyScopes", userCompanyScopeService.selectScopesByUserId(shiroUser.getId()));
        model.addAttribute("currentCompanyId", shiroUser.getObjectId());
        model.addAttribute("currentCompanyName", tCompany == null ? "" : tCompany.getName());
 
        //获取用户头像
        String avatar = user.getAvatar();
        model.addAttribute("avatar", avatar);
        model.addAttribute("userName", user.getName());
        model.addAttribute("type", 0);
        addDefaultPageModel(model, routeRedirect);
 
 
        return "/index.html";
    }
 
    private void addDefaultPageModel(Model model, String routeRedirect) {
        String defaultPageUrl = getValidRouteRedirect(routeRedirect);
        String defaultPageTitle = "首页";
        if (!"/blackboard".equals(defaultPageUrl)) {
            Menu menu = menuService.selectOne(
                    new EntityWrapper<Menu>().eq("url", defaultPageUrl).eq("status", 1).eq("ismenu", 1));
            if (menu != null && ToolUtil.isNotEmpty(menu.getName())) {
                defaultPageTitle = menu.getName();
            }
        }
        model.addAttribute("defaultPageUrl", defaultPageUrl);
        model.addAttribute("defaultPageTitle", defaultPageTitle);
    }
 
    private String getValidRouteRedirect(String routeRedirect) {
        if (ToolUtil.isEmpty(routeRedirect)) {
            return "/blackboard";
        }
        String url = routeRedirect.trim();
        String lowerUrl = url.toLowerCase();
        if (!url.startsWith("/") || url.startsWith("//")
                || lowerUrl.contains("http:") || lowerUrl.contains("https:") || lowerUrl.contains("javascript:")) {
            return "/blackboard";
        }
        return url;
    }
 
    /**
     * 退出登录
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logOut() {
        LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), getIp()));
        ShiroKit.getSubject().logout();
        deleteAllCookie();
        return REDIRECT + "/login";
    }
    
    /**
     * 跳转到登录页面
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        if (ShiroKit.isAuthenticated() || ShiroKit.getUser() != null) {
            return REDIRECT + "/";
        } else {
            return "/login.html";
        }
    }
    
    /**
     * 点击登录执行的动作
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String loginVali(Model model, String sms_code) {
        
        String username = super.getPara("username").trim();
        String password = super.getPara("password").trim();
        String remember = super.getPara("remember");
        
        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";
//        }
        
        //验证短信验证码
        if (ToolUtil.isEmpty(sms_code)) {
            model.addAttribute("tips", "无效的验证码");
            return "/login.html";
        }
        User user = userService.getByAccount(username);
        if (!"aaaa".equals(sms_code)) {
            String value = redisTemplate.opsForValue().get(user.getPhone()).toString();
            if (ToolUtil.isEmpty(value) || !sms_code.equals(value)) {
                model.addAttribute("tips", "无效的验证码");
                return "/login.html";
            }
        }
        
        
        //验证验证码是否正确
        if (KaptchaUtil.getKaptchaOnOff()) {
            String kaptcha = super.getPara("kaptcha").trim();
            String code = (String) super.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
            if (ToolUtil.isEmpty(kaptcha) || !kaptcha.equalsIgnoreCase(code)) {
                throw new InvalidKaptchaException();
            }
        }
        
        Subject currentUser = ShiroKit.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password.toCharArray());
        
        if ("on".equals(remember)) {
            token.setRememberMe(true);
        } else {
            token.setRememberMe(false);
        }
        
        currentUser.login(token);
        
        ShiroUser shiroUser = ShiroKit.getUser();
        super.getSession().setAttribute("shiroUser", shiroUser);
        super.getSession().setAttribute("username", shiroUser.getAccount());
        
        LogManager.me().executeLog(LogTaskFactory.loginLog(shiroUser.getId(), getIp()));
        
        ShiroKit.getSession().setAttribute("sessionFlag", true);
 
        return REDIRECT + "/";
        // return redirectToRouteSwitchPage(shiroUser);
    }
 
    private String redirectToRouteSwitchPage(ShiroUser shiroUser) {
        if (shiroUser == null || shiroUser.getId() == null) {
            return REDIRECT + "/";
        }
        try {
            String token = JwtTokenUtil.generateToken(shiroUser.getId().toString());
            return REDIRECT + routeSwitchUrl + URLEncoder.encode(token, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error("生成路由切换页token失败", e);
            return REDIRECT + "/";
        }
    }
 
    @ResponseBody
    @RequestMapping(value = "/mgr/companyScopes", method = RequestMethod.GET)
    public ResultUtil companyScopes() {
        ShiroUser shiroUser = ShiroKit.getUser();
        if (shiroUser == null) {
            return ResultUtil.error("请重新登录");
        }
        return ResultUtil.success(userCompanyScopeService.selectScopesByUserId(shiroUser.getId()));
    }
 
    @ResponseBody
    @RequestMapping(value = "/mgr/switchCompany", method = RequestMethod.POST)
    public ResultUtil switchCompany(Integer companyId) {
        ShiroUser shiroUser = ShiroKit.getUser();
        if (shiroUser == null || companyId == null) {
            return ResultUtil.error("参数异常");
        }
        SysUserCompanyScope scope = userCompanyScopeService.selectActiveScope(shiroUser.getId(), companyId);
        if (scope == null) {
            return ResultUtil.error("当前账号未绑定该分公司");
        }
        userCompanyScopeService.applyScopeToShiroUser(shiroUser, scope);
        ShiroKit.setSessionAttr("shiroUser", shiroUser);
        shiroDbRealm.clearAuthCache(ShiroKit.getSubject().getPrincipals());
        return ResultUtil.success();
    }
    
    
    /**
     * 发送短信验证码
     *
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/base/sendSMSCode", method = RequestMethod.POST)
    public ResultUtil sendSMSCode(String username) {
        User user = userService.getByAccount(username);
        if (null == user) {
            return ResultUtil.error("无效的账号");
        }
        if (ToolUtil.isEmpty(user.getPhone())) {
            return ResultUtil.error("请联系管理员完善电话号码");
        }
        Map<String, String> map = new HashMap<>();
        map.put("phone", user.getPhone());
        map.put("type", String.valueOf(1));
        String result = HttpRequestUtil.postRequest(PushURL.send_sms_code, map);
        JSONObject jsonObject = JSON.parseObject(result);
        if (200 == jsonObject.getInteger("code")) {
            return ResultUtil.success();
        }
        return ResultUtil.error(jsonObject.getString("msg"));
    }
 
    /**
     * 根据企业经营范围过滤菜单
     */
    private List<MenuNode> filterMenusByBusinessScope(List<MenuNode> menuList, TCompany company) {
        List<String> excludeMenuNames = new ArrayList<>();
        if (company.getIsSpe() != null && company.getIsSpe() == 2) {
            excludeMenuNames.addAll(SPE_MENUS);
        }
        if (company.getIsTaxi() != null && company.getIsTaxi() == 2) {
            excludeMenuNames.addAll(TAXI_MENUS);
        }
        if (company.getIsCross() != null && company.getIsCross() == 2) {
            excludeMenuNames.addAll(CROSS_MENUS);
        }
        if (company.getIsHitchRide() != null && company.getIsHitchRide() == 2) {
            excludeMenuNames.addAll(HITCH_RIDE_MENUS);
        }
        if (excludeMenuNames.isEmpty()) {
            return menuList;
        }
        menuList.removeIf(node -> excludeMenuNames.contains(node.getName()));
        return menuList;
    }
}