All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.huanminabc.utils_common.spring.SessionUtil Maven / Gradle / Ivy

There is a newer version: 1.0.5-RELEASE
Show newest version
package com.gitee.huanminabc.utils_common.spring;

import org.springframework.core.env.Environment;

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

public class SessionUtil {
    private static Environment environment = SpringContextHolder.getApplicationContext().getEnvironment();
    private static int time; //秒
    static {
        time = Integer.parseInt(Objects.requireNonNull(environment.getProperty("session.time")));
    }
    //默认session的时间是30分钟,
    public static void  setSession(String key ,Object value) {
        HttpSession session = ContextAttribuesUtils.getRequest().getSession();
        session.setAttribute(key,value);
        session.setMaxInactiveInterval(time);

    }
    public static  T  getSession(String key,Class clazz ) {
        HttpSession session = ContextAttribuesUtils.getRequest().getSession();
        return (T)session.getAttribute(key);
    }
    public static void delSession(String key ) {
        HttpSession session = ContextAttribuesUtils.getRequest().getSession();
        session.removeAttribute(key);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy