Springboot实现登录退出功能

登录认证实体类

@Data
public class LoginTicket {

    private int id;
    private int userId;
    private String ticket;
    private int status;
    private Date expired;
}

dao层

@Mapper
public interface LoginTicketMapper {

    @Insert({
            "insert into login_ticket(user_id,ticket,status,expired) ",
            "values(#{userId},#{ticket},#{status},#{expired})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertLoginTicket(LoginTicket loginTicket);

    @Select({
            "select id,user_id,ticket,status,expired ",
            "from login_ticket where ticket=#{ticket}"
    })
    LoginTicket selectByTicket(String ticket);

    @Update({
            "<script>",
            "update login_ticket set status=#{status} where ticket=#{ticket} ",
            "<if test=\"ticket!=null\"> ",
            "and 1=1 ",
            "</if>",
            "</script>"
    })
    int updateStatus(String ticket, int status);

}

Service层

@Service
public class UserService implements CommunityConstant {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MailClient mailClient;

    @Autowired
    private TemplateEngine templateEngine;

    @Value("${community.path.domain}")
    private String domain;

    @Value("${server.servlet.context-path}")
    private String contextPath;

    @Autowired
    private LoginTicketMapper loginTicketMapper;

    
    public Map<String, Object> login(String username, String password, int expiredSeconds) {
        Map<String, Object> map = new HashMap<>();

        // 空值处理
        if (StringUtils.isBlank(username)) {
            map.put("usernameMsg", "账号不能为空!");
            return map;
        }
        if (StringUtils.isBlank(password)) {
            map.put("passwordMsg", "密码不能为空!");
            return map;
        }

        // 验证账号
        User user = userMapper.selectByName(username);
        if (user == null) {
            map.put("usernameMsg", "该账号不存在!");
            return map;
        }

        // 验证状态
        if (user.getStatus() == 0) {
            map.put("usernameMsg", "该账号未激活!");
            return map;
        }

        // 验证密码
        password = CommunityUtil.md5(password + user.getSalt());
        if (!user.getPassword().equals(password)) {
            map.put("passwordMsg", "密码不正确!");
            return map;
        }

        // 生成登录凭证
        LoginTicket loginTicket = new LoginTicket();
        loginTicket.setUserId(user.getId());
        loginTicket.setTicket(CommunityUtil.generateUUID());
        loginTicket.setStatus(0);
        loginTicket.setExpired(new Date(System.currentTimeMillis() + expiredSeconds * 1000));
        loginTicketMapper.insertLoginTicket(loginTicket);

        map.put("ticket", loginTicket.getTicket());
        return map;
    }

    public void logout(String ticket) {
        loginTicketMapper.updateStatus(ticket, 1);
    }

}

controller层

@Value("${server.servlet.context-path}")
private String contextPath;

@RequestMapping(path = "/login", method = RequestMethod.POST)
public String login(String username, String password, String code, boolean rememberme,
                    Model model, HttpSession session, HttpServletResponse response) {
    // 检查验证码
    String kaptcha = (String) session.getAttribute("kaptcha");
    if (StringUtils.isBlank(kaptcha) || StringUtils.isBlank(code) || !kaptcha.equalsIgnoreCase(code)) {
        model.addAttribute("codeMsg", "验证码不正确!");
        return "/site/login";
    }

    // 检查账号,密码
    int expiredSeconds = rememberme ? REMEMBER_EXPIRED_SECONDS : DEFAULT_EXPIRED_SECONDS;
    Map<String, Object> map = userService.login(username, password, expiredSeconds);
    if (map.containsKey("ticket")) {
        Cookie cookie = new Cookie("ticket", map.get("ticket").toString());
        cookie.setPath(contextPath);
        cookie.setMaxAge(expiredSeconds);
        response.addCookie(cookie);
        return "redirect:/index";
    } else {
        model.addAttribute("usernameMsg", map.get("usernameMsg"));
        model.addAttribute("passwordMsg", map.get("passwordMsg"));
        return "/site/login";
    }
}

@RequestMapping(path = "/logout", method = RequestMethod.GET)
public String logout(@CookieValue("ticket") String ticket) {
    userService.logout(ticket);
    return "redirect:/login";
}

视图层

<div class="main">
   <div class="container pl-5 pr-5 pt-3 pb-3 mt-3 mb-3">
      <h3 class="text-center text-info border-bottom pb-3">&nbsp;&nbsp;</h3>
      <form class="mt-5" method="post" th:action="@{/login}">
         <div class="form-group row">
            <label for="username" class="col-sm-2 col-form-label text-right">账号:</label>
            <div class="col-sm-10">
               <input type="text" th:class="|form-control ${usernameMsg!=null?'is-invalid':''}|"
                     th:value="${param.username}"
                     id="username" name="username" placeholder="请输入您的账号!" required>
               <div class="invalid-feedback" th:text="${usernameMsg}">
                  该账号不存在!
               </div>
            </div>
         </div>
         <div class="form-group row mt-4">
            <label for="password" class="col-sm-2 col-form-label text-right">密码:</label>
            <div class="col-sm-10">
               <input type="password" th:class="|form-control ${passwordMsg!=null?'is-invalid':''}|"
                     th:value="${param.password}"
                     id="password" name="password" placeholder="请输入您的密码!" required>
               <div class="invalid-feedback" th:text="${passwordMsg}">
                  密码长度不能小于8位!
               </div>                   
            </div>
         </div>
         <div class="form-group row mt-4">
            <label for="verifycode" class="col-sm-2 col-form-label text-right">验证码:</label>
            <div class="col-sm-6">
               <input type="text" th:class="|form-control ${codeMsg!=null?'is-invalid':''}|"
                     id="verifycode" name="code" placeholder="请输入验证码!">
               <div class="invalid-feedback" th:text="${codeMsg}">
                  验证码不正确!
               </div>
            </div>
            <div class="col-sm-4">
               <img th:src="@{/kaptcha}" id="kaptcha" style="width:100px;height:40px;" class="mr-2"/>
               <a href="javascript:refresh_kaptcha();" class="font-size-12 align-bottom">刷新验证码</a>
            </div>
         </div>          
         <div class="form-group row mt-4">
            <div class="col-sm-2"></div>
            <div class="col-sm-10">
               <input type="checkbox" id="remember-me" name="rememberme"
                     th:checked="${param.rememberme}">
               <label class="form-check-label" for="remember-me">记住我</label>
               <a href="forget.html" class="text-danger float-right">忘记密码?</a>
            </div>
         </div>          
         <div class="form-group row mt-4">
            <div class="col-sm-2"></div>
            <div class="col-sm-10 text-center">
               <button type="submit" class="btn btn-info text-white form-control">立即登录</button>
            </div>
         </div>
      </form>             
   </div>
</div>
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值