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";
}
}