栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

统一管理session

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

统一管理session

老项目ssh 要限制用户做唯一登录,比如在谷歌浏览器登录之后,然后再用IE登录,那么谷歌 上的用户就会失效,项目用的session。所以记录一下如何管理,接下来演示代码以springboot的使用测试,原理一样。主要以session监听器做处理操作。

项目的权限校验使用拦截器做处理

@Component
public class LoginIntecptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        User user = (User)request.getSession().getAttribute("user");
        if(user==null){
            response.sendRedirect("/toLogin");
            return false;
        }
        return true;
    }
}

让拦截器生效

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    LoginIntecptor loginIntecptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginIntecptor).addPathPatterns("
    @Override
    public void sessionCreated(HttpSessionEvent e) {
        System.out.println("监听到 session 创建, sessionid 是: " + e.getSession().getId());
        myc.addSession(e.getSession());
    }

    
    @Override
    public void sessionDestroyed(HttpSessionEvent e) {
        System.out.println("监听到 session 销毁, sessionid 是: " + e.getSession().getId());
        myc.delSession(e.getSession());
    }
}

用户和session对应关系容器

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class UserContext {

    private static Map userMap = new ConcurrentHashMap<>();

    
    public static void put(Integer userId,String sessionId){
        userMap.put(userId,sessionId);
    }

    
    public static String get(Integer userId){
        return userMap.get(userId);
    }

    
    public static void remove(Integer userId){
         userMap.remove(userId);
    }

    
    public static void removeKeyByValue(String value){
        Set keys = userMap.keySet();
        for(Integer userId: keys){
            String s = userMap.get(userId);
            if(s.equals(value)){
                userMap.remove(userId);
            }
        }
    }
}

测试代码

@GetMapping("/toLogin")
public ModelAndView loginPage(){
  return new ModelAndView("login");
}

@GetMapping("/success")
public ModelAndView success(){
  return new ModelAndView("success");
}

@GetMapping("/login")
public void login(User user, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
  if(user==null){
      response.sendRedirect("toLogin");
  }
  String sessionId = UserContext.get(user.getId());
  if(!StringUtils.isEmpty(sessionId)){//说明用户已经登录了
      HttpSession session1 = MySessionContext.getInstance().getSession(sessionId);//移除原来的
      session1.invalidate();
  }
  UserContext.put(user.getId(),session.getId());//重新覆盖
  request.getSession().setAttribute("user",user);
  response.sendRedirect("success");
}

@GetMapping("/logout")
public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
  request.getSession().invalidate();
  //return new ModelAndView("login");
  response.sendRedirect("toLogin");
}

效果就不给你们提供了,随便看看就行了,代码也比较简单,了解即可。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/832062.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号