package com.sptg.gateway.security;

import com.sptg.common.core.constant.HttpConstant;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.common.core.util.WebUtils;
import com.sptg.common.security.service.impl.BaseRedisSecurityService;
import com.sptg.permissions.api.entity.DeviceAdminUser;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * title: SecurityServiceImpl
 * package: com.sptg.gateway.security
 * description: 安全接口实现类
 *                  
 *                 
 */
@Service
public class SecurityServiceImpl extends BaseRedisSecurityService<DeviceAdminUser> {

    public SecurityServiceImpl(RedisTemplate<String, String> redisTemplate) {
        super(redisTemplate);
    }

    @Override
    protected DeviceAdminUser jsonToPojo(String json) {
        return JsonUtils.jsonToPojo(json,DeviceAdminUser.class);
    }

    @Override
    protected String getCacheName() {
        return "on-line:device-admin-user:";
    }

    /**
     * 登陆超时时间 8小时
     */
    private final static long TIME_OUT = 60 * 60 * 12;

    @Override
    protected long getTimeOut() {
        return TIME_OUT;
    }

    @Override
    protected String getTokenByRequest() {
        //  从请求头中获取Token
        HttpServletRequest request = WebUtils.getHttpServletRequest();
        return request.getHeader(HttpConstant.ACCESS_TOKEN);
    }
}
