首页>代码>基于SSM开发的网上订餐系统,包括前台页面和后台管理系统>/sourcecode/src/main/java/com/byh/biyesheji/controller/LoginController.java
package com.byh.biyesheji.controller;

import com.byh.biyesheji.pojo.User;
import com.byh.biyesheji.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
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 java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 后台登陆
 */
@Controller
@RequestMapping("")
public class LoginController {

    @Autowired
    UserService userService;

    @RequestMapping(value="/login",method=RequestMethod.POST)
    public String login(Model model, String name, String password){//throws ParseException
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(name,password);
        try {
            subject.login(token);
            User us = userService.getByName(name);
            String lastLoginTime = "";
            if(us!=null){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //上次时间
                Date time = us.getLasttime();
                lastLoginTime = sdf.format(time);
                //新时间
                String format = sdf.format(new Date());
                //string转date  不处理时间格式会不理想
                ParsePosition pos = new ParsePosition(0);
                Date strtodate = sdf.parse(format, pos);
                us.setLasttime(strtodate);
                userService.update(us);
            }
            if (us.getStatus()==1){
                Session session=subject.getSession();
                session.setAttribute("subject", subject);
                session.setAttribute("lastLoginTime",lastLoginTime);
                return "redirect:index";
            }else {
                model.addAttribute("error", "账号已被停用!");
                return "/login";
            }

        } catch (AuthenticationException e) {
            model.addAttribute("error", "验证失败!");
            return "/login";
        }
    }


}
最近下载更多
奋斗的小蚂蚁  LV17 10月22日
微信网友_7298640909209600  LV2 2024年12月18日
120_wu  LV4 2024年12月3日
charleswang  LV7 2024年10月19日
柳咪华沙  LV7 2024年6月28日
rain112  LV31 2024年6月13日
qtingchh  LV3 2024年5月3日
saaaaaa  LV14 2024年4月16日
2131234536546  LV7 2024年3月31日
123ggf  LV8 2024年3月15日
最近浏览更多
奋斗的小蚂蚁  LV17 10月22日
962154437 9月21日
暂无贡献等级
krispeng  LV15 6月24日
xp95323  LV15 6月10日
委屈99 3月2日
暂无贡献等级
哪里的完整版  LV8 3月1日
zyfile  LV1 1月9日
ximM2025  LV1 1月8日
微信网友_7298640909209600  LV2 2024年12月18日
120_wu  LV4 2024年12月3日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友