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

matrix.boot.based.utils.WebUtil Maven / Gradle / Ivy

There is a newer version: 2.1.10
Show newest version
package matrix.boot.based.utils;

import matrix.boot.common.utils.RandomUtil;
import matrix.boot.common.utils.http.CookieUtil;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 通用的容器工具
 *
 * @author 36509
 */
public class WebUtil implements ApplicationContextAware {

    /**
     * 用户唯一标识Key
     */
    private static final String LOGIN_KEY = "UserTokenID";

    /**
     * 容器
     */
    private static ApplicationContext applicationContext;

    /**
     * 初始化 ApplicationContext 用于没初始化成功的时候获取数据
     *
     * @param applicationContext ApplicationContext
     */
    public static void initApplicationContext(ApplicationContext applicationContext) {
        if (WebUtil.applicationContext == null) {
            WebUtil.applicationContext = applicationContext;
        }
    }

    /**
     * 获取ApplicationContext
     *
     * @return ApplicationContext
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    @SuppressWarnings("NullableProblems")
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        WebUtil.applicationContext = applicationContext;
    }

    /**
     * 根据class获取bean
     *
     * @param clazz bean的类型
     * @return T
     */
    public static  T getBean(Class clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 根据beanName获取bean
     *
     * @param name bean名称
     * @return bean实例
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 根据beanName和requiredType获取bean
     *
     * @param name         bean名称
     * @param requiredType bean类型
     * @return bean实例
     */
    public static  T getBean(String name, Class requiredType) {
        return getApplicationContext().getBean(name, requiredType);
    }

    /**
     * 根据beanName查找bean是否存在
     *
     * @param name bean名称
     * @return 是否包含
     */
    public static boolean containsBean(String name) {
        return getApplicationContext().containsBean(name);
    }

    /**
     * 根据beanType查找bean是否存在
     *
     * @param clazz bean类型
     * @return 是否包含
     */
    public static boolean containsBean(Class clazz) {
        return !getApplicationContext().getBeansOfType(clazz).isEmpty();
    }

    /**
     * 根据beanName判断是否是单例
     *
     * @param name bean名称
     * @return 是否
     */
    public static boolean isSingleton(String name) {
        return getApplicationContext().isSingleton(name);
    }

    /**
     * 根据beanName获取类型
     *
     * @param name bean名称
     * @return bean类型
     */
    public static Class getType(String name) {
        return getApplicationContext().getType(name);
    }

    /**
     * 根据beanName获取别名
     *
     * @param name bean名称
     * @return bean别名
     */
    public static String[] getAliases(String name) {
        return getApplicationContext().getAliases(name);
    }

    /**
     * 获取容器context
     *
     * @param request 请求头
     * @return WebApplicationContext
     */
    public static WebApplicationContext getContext(HttpServletRequest request) {
        return WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
    }

    /**
     * 获取用户唯一标识
     *
     * @param request  请求头
     * @param response 响应头
     * @param isCors   是否跨域(跨域必须是https)
     * @return tokenId
     */
    public static String getUserTokenId(HttpServletRequest request, HttpServletResponse response, boolean isCors) {
        String tokenId = CookieUtil.getCookieValue(request, WebUtil.LOGIN_KEY);
        if (tokenId != null && !"".equals(tokenId)) {
            return tokenId;
        } else {
            tokenId = RandomUtil.getUUID();
            if (CookieUtil.addSimpleHttpOnlyCookie(response, WebUtil.LOGIN_KEY, tokenId, isCors))
                return tokenId;
        }
        return null;
    }

    /**
     * 获取request请求
     *
     * @return request请求
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert servletRequestAttributes != null;
        return servletRequestAttributes.getRequest();
    }

    /**
     * 获取response响应
     *
     * @return response响应
     */
    public static HttpServletResponse getResponse() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert servletRequestAttributes != null;
        return servletRequestAttributes.getResponse();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy