2021-08-05 postMan

postMan模拟登陆用户以便查找信息xxl_sso_sessionid

在这里插入图片描述

package com.jxmcloud.core.utils.session;

import com.jxmcloud.business.shop.util.log.LogUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * @Description: 获取session
 */
@Component
@Slf4j
public class SessionUtil {

    private static  final String SESSIONID = "xxl_sso_sessionid";

    private static HttpServletRequest request = null;

    private static HttpSession session = null;

    //获取当前上下文对应的request请求
    public static HttpServletRequest getRequest(){
        if (request == null){
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
           request = attributes.getRequest();
        }
        return request;
    }

    //获取当前上下文对应的request请求
    public static HttpServletRequest getHttpRequest(){
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if(Objects.nonNull(attributes)){
                request = attributes.getRequest();
            }
        return request;
    }
    

    //获取当前上下文对应的session请求
    public static HttpSession getSession(){
        if (session == null){
            session = getRequest().getSession();
        }
        return session;
    }

    public static void sessionInvaild(){
        if(session != null){
            session.invalidate();
        }
    }

    //给session赋值
    public static void setAttribute(String key,Object value){
        getSession().setAttribute(key,value);
    }

    //session取值
    public static Object getAttribute(String key){
        return getSession().getAttribute(key);
    }
    
    //header取值
    public static Object getHeader(String key){
        Object obj=null;
        try {
            obj= getHttpRequest().getHeader(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    //header取值
    public static String getUserId(){
        Object xxl_sso_sessionid = getHeader(SESSIONID);
        String[] session =null;
        if(Objects.nonNull(xxl_sso_sessionid)){
            try{
                session=String.valueOf(xxl_sso_sessionid).split("_");
                return  session[0];
            }catch (Exception e){
                LogUtils.error(log,e,"获取userId转换异常","",String.valueOf(session));
            }
        }
        return "system";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值