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

com.biz.web.token.AbstractToken Maven / Gradle / Ivy

Go to download

BizX 是一个灵活而高效的业务开发框架, 其中也有很多为业务开发所需要的工具类的提供。

The newest version!
package com.biz.web.token;


import com.biz.common.bean.BizXBeanUtils;
import com.biz.common.utils.Common;
import com.biz.web.account.BizAccount;
import com.biz.web.account.BizAccountFactory;
import com.biz.web.session.SessionManage;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.annotation.Order;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.Optional;

/**
 * Token
 *
 * @author francis
 * @since 1.0.1
 **/
@Order(81)
public class AbstractToken implements Token, ApplicationListener {

    /**
     *
     */
    private final ThreadLocal token = new ThreadLocal<>();

    private final ThreadLocal set = ThreadLocal.withInitial(() -> false);

    /**
     * 当前线程本次请求的 HttpServletResponse 对象
     */
    private final ThreadLocal responseThreadLocal = ThreadLocal.withInitial(() -> null);

    private SessionManage sessionManage;

    private static final String HEADER_KEY = "Biz-Token";


    @Override
    public String getCurrentToken() {
        return token.get();
    }

    @Override
    public BizAccount getCurrentUser() {
        if (token.get() == null) {
            return null;
        }
        Optional session = sessionManage.getSession(token.get());
        if (!session.isPresent()) {
            return null;
        }
        try {
            BizAccountFactory bean = BizXBeanUtils.getBean(BizAccountFactory.class);
            return bean.getBizAccount(session.get());
        } catch (Exception e) {
            throw new RuntimeException("BizAccountFactory is not definition");
        }
    }

    @Override
    public void setCurrentUser(BizAccount bizAccount) {
        String session = sessionManage.createSession(bizAccount);
        token.set(session);
        set.set(true);
        HttpServletResponse httpServletResponse = responseThreadLocal.get();
        httpServletResponse.addHeader(HEADER_KEY, session);
    }

    @Override
    public void destroy() {
        if (Common.isBlank(token.get())) {
            return;
        }
        // 销毁Session
        sessionManage.destroySession(token.get());
        token.remove();
    }

    @Override
    public boolean isSetAccount() {
        return set.get();
    }

    @Override
    public void initAccount(String token) {
        this.token.set(token);
    }

    @Override
    public void setHttpServletResponse(HttpServletResponse response) {
        responseThreadLocal.set(response);
    }

    @Override
    public boolean checkTokenIsExpire() {
        Optional session = sessionManage.getSession(token.get());
        return !session.isPresent();
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 当所有的bean都被成功装载、初始化和刷新后,调用这里
        try {
            this.sessionManage = BizXBeanUtils.getBean(SessionManage.class);
        } catch (Exception e) {
            throw new RuntimeException("not find sessionManage");
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy