xuhy
2023-03-27 957a8e8f5a01de49baa837e30492de511bf407e3
management/guns-admin/src/main/java/com/stylefeng/guns/modular/system/controller/system/LoginController.java
@@ -2,12 +2,15 @@
import com.google.code.kaptcha.Constants;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.Const;
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.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.shiro.factory.IShiro;
import com.stylefeng.guns.core.shiro.factory.ShiroFactroy;
import com.stylefeng.guns.core.util.ApiMenuFilter;
import com.stylefeng.guns.core.util.KaptchaUtil;
import com.stylefeng.guns.core.util.SinataUtil;
@@ -16,6 +19,7 @@
import com.stylefeng.guns.modular.system.service.IMenuService;
import com.stylefeng.guns.modular.system.service.IUserService;
import com.stylefeng.guns.modular.system.util.AESUtils;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
@@ -50,7 +54,7 @@
    private Map<String, Integer> loginFailures = new HashMap<>();
    private List<String> ips = Arrays.asList("127.0.0.1");
    private List<String> ips = Arrays.asList("127.0.0.1","192.168.0.247");
@@ -103,7 +107,8 @@
     * 跳转到登录页面
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
    public String login(Model model) {
        model.addAttribute("number",0);
        if (ShiroKit.isAuthenticated() || ShiroKit.getUser() != null) {
            return REDIRECT + "/";
        } else {
@@ -148,29 +153,50 @@
        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{
        // 校验账号,密码是否正确,如果错误,对次数进行加1
        IShiro shiroFactory = ShiroFactroy.me();
        User user = userService.getByAccount(username);
        password = AESUtils.decrypt(password);
        if(null == user){
            f++;
            loginFailures.put(username, f);
        }
        if(f > 5 && (System.currentTimeMillis() - t) <= (30 * 60 * 1000)){
            model.addAttribute("tips", "登录次数过多,请等30分钟再试!");
            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();
        }else {
            // 校验密码
            ShiroUser shiroUser1 = shiroFactory.shiroUser(user);
            SimpleAuthenticationInfo info = shiroFactory.info(shiroUser1, user, username);
            String pass = ShiroKit.md5(password, user.getSalt());
            if(!info.getCredentials().equals(pass)){
                f++;
                loginFailures.put(username, f);
                if(f<5){
                    model.addAttribute("number", "3");
                }
                if(f==5){
                    model.addAttribute("number", "5");
                }
                return "/login.html";
            }
        }
        password = AESUtils.decrypt(password);
        model.addAttribute("number", "3");
        /*if(f > 5 ){
            model.addAttribute("tips", "登录次数过多,请等30分钟再试!");
            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();
                }
            }
            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());
@@ -179,11 +205,12 @@
//        } else {
//            token.setRememberMe(false);
//        }
        token.setRememberMe(false);//关闭记住我功能
        currentUser.login(token);
        // 登录成功,错误次数值改0
        loginFailures.put(username, 0);
        ShiroUser shiroUser = ShiroKit.getUser();
        super.getSession().setAttribute("shiroUser", shiroUser);
        super.getSession().setAttribute("username", shiroUser.getAccount());