java单用户登录实现(详细代码)

本文介绍了如何在Java SpringBoot应用中实现单用户登录功能。通过检查Session,当同一账号尝试在另一设备登录时,系统将挤掉已登录账号或显示账号已登录的提示。详细代码示例包括LoginInterceptor拦截器和DemoOneServiceImpl服务类的实现,以及测试场景的描述。
摘要由CSDN通过智能技术生成

单用户登录的实现比较简单,只要在session上动动手脚就行了

单用户登录有两种实现方式
1)当前用户登录验证成功时,如果该用户的账号已经登录,则挤掉已登录的账号,由该用户登录
2)当前用户登录验证成功时,如果该用户的账号已经登录,则提示当前用户该账号已登录,并且不允许当前用户登录

环境

系统:win10
IDE:sts4
springboot2.2.2.RELEASE、 jdk8、 maven3.3.9

挤掉已登录用户

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
server.port=8081

spring.mvc.view.prefix=/templates/
spring.mvc.view.suffix=.html

登录页是Bootstrap框架中的

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="images/favicon.ico">

    <title>Signin Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="css/signin.css" rel="stylesheet">
  </head>

  <body class="text-center">
    <form class="form-signin" action="/login" method="post">
      <h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
      <p style="color:red;"  th:text="${failMsg}" th:if="${not #strings.isEmpty(failMsg)}"></p>
      <label for="inputEmail" class="sr-only">Email address</
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值