单点登录的实现效果


实现的单点登录效果是在不同浏览器使用同一账户登录时,让先登录的一方下线,并给出提示

目录

数据库

这里使用的是一个比较简单的数据库

CREATE TABLE `spring`.`Untitled`  (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
  `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
  `type` int(255) NULL DEFAULT 0,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;



INSERT INTO `user` VALUES (1, 'a', '123', 0);
INSERT INTO `user` VALUES (2, 'b', '456', 0);
INSERT INTO `user` VALUES (3, 'c', '789', 0);
INSERT INTO `user` VALUES (5, 'qw', '456', 0);
INSERT INTO `user` VALUES (6, 'er', '666', 0);
INSERT INTO `user` VALUES (7, 'er', '99', 0);
INSERT INTO `user` VALUES (8, '6', '6', 0);

实体类

package com.dan;

import lombok.Data;

/**
 * com.dan
 * liu
 * 2022/11/25
 * dan
 * 2022年11月25日21时05分
 */
@Data
public class User {
    String name;
    String password;
    int type; //type是我做的一个判断状态的东西
    int id;
}

dao层

我这里只写了dao层,没有去写service层

package com.dan;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import javax.annotation.Resource;
import java.util.List;

/**
 * com.dan
 * liu
 * 2022/11/25
 * dan
 * 2022年11月25日21时07分
 */
@Mapper
public interface UserDao {
    @Select("<script>select * from user where name=#{name} and password=#{password} </script>")
	User login(String name,String password);//登录的sql条件
    @Update("<script>update user set type=#{type} where id=#{id} </script>")
    int update(User user);
}

拦截器

在跳转页面时自动拦截,然后我这里是通过type的值来进行判断是否登录过,并且加了session来存储登录的姓名,密码,type值

package com.dan;


import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

import java.util.List;


/**
 * com.dan
 * liu
 * 2022/11/25
 * dan
 * 2022年11月25日21时34分
 */
public class Intercepor implements HandlerInterceptor {
    UserDao userDao;
    public Intercepor(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        String password = (String) session.getAttribute("password");
        if (name != null) {
            User login = userDao.login(name, password);
            Integer type = login.getType();
            if (type != 0) {
                System.out.println(response);
                System.out.println("==================" + request.getRequestURL());
                login.setType(0);
                userDao.update(login);
                response.setStatus(404);
                response.sendRedirect("index");
                return false;
            }
        }
        return true;
    }

}


配置类

package com.dan;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * com.dan
 * liu
 * 2022/11/25
 * dan
 * 2022年11月25日21时58分
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {//通过实现addInterceptors来进行配置
    @Autowired
    private UserDao userDao;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//除了index页面,其他进行一个页面的拦截
        registry.addInterceptor(new Intercepor(userDao))
                .excludePathPatterns("/index");
    }

}

web层

Controller,把login的值传到session中去,然后拦截器去获取session里面的值

package com.dan;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * com.dan
 * liu
 * 2022/11/25
 * dan
 * 2022年11月25日21时12分
 */
@Controller
public class UserWeb {
    @Autowired
    HttpServletRequest request; //通过注解获取一个request
    @Autowired
    UserDao userDao;
    @RequestMapping("/login")
    public ModelAndView login(String name, String password, ModelAndView modelAndView) {
        HttpSession session = request.getSession();
        User login = userDao.login(name, password);
        if (login != null) {
            session.setAttribute("name", login.getName());
            session.setAttribute("password", login.getPassword());
            session.setAttribute("type", login.getType());
            session.setAttribute("id", login.getId());
            login.setType(1);
            userDao.update(login);
        }

        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping("index")
    public ModelAndView jin() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }
}

HTML

登录成功后的页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="refresh" content="2" />
    <title>Title</title>
</head>
<script src="jquery-3.3.1.min.js"></script>
<body>
<h1>欢迎</h1>

</body>
</html>

login页面,登录

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="login">
    姓名<input type="text" name="name">
    密码<input type="text" name="password">
    <input type="submit">
</form>
</body>

效果图+项目结构

在这里插入图片描述

单点登录的实现效果

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值