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

net.shmin.auth.token.impl.SimpAuthTokenGenerator Maven / Gradle / Ivy

There is a newer version: 2.3.1-RELEASE
Show newest version
package net.shmin.auth.token.impl;

import net.shmin.auth.AuthContext;
import net.shmin.auth.token.IAuthTokenGenerator;
import net.shmin.auth.token.Token;
import net.shmin.auth.token.TokenType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.UUID;

/**
 * Token生成器
 * Created by benjamin on 9/3/14.
 */
@Component
public class SimpAuthTokenGenerator implements IAuthTokenGenerator {

    private Logger logger = LoggerFactory.getLogger(SimpAuthTokenGenerator.class);

    @Autowired
    private AuthContext authContext;

    @PostConstruct
    public void init() {
        logger.info("access_token有效期为{}分钟", authContext.getAccessTokenExpire() / 60000);
        logger.info("refresh_token有效期为{}分钟", authContext.getRefreshTokenExpires() / 60000);
        logger.info("authorization_code有效期为{}分钟", authContext.getAuthorizationCodeExpires() / 60000);
    }

    @Override
    public Token generateAuthorizationCode() {
        Token token = generateToken();
        if (token != null) {
            token.setExpires( authContext.getAuthorizationCodeExpires());
            token.setTokenType(TokenType.authorizationCode);
        }
        return token;
    }

    @Override
    public Token generateAccessToken(boolean useRefreshToken) {
        Token token = generateToken();
        token.setTokenType(TokenType.accessToken);
        token.setExpires(authContext.getAccessTokenExpire());
        if (useRefreshToken) {
            Token refreshToken = generateRefreshToken();
            token.setRefreshToken(refreshToken);
        }
        return token;
    }

    private Token generateRefreshToken() {
        Token token = generateToken();
        if (token != null) {
            token.setExpires(authContext.getRefreshTokenExpires());
            token.setTokenType(TokenType.refreshToken);
        }
        return token;
    }

    private Token generateToken() {
        String uuid = UUID.randomUUID().toString();
        String[] uuidShorts = uuid.split("-");
        StringBuilder stringBuilder = new StringBuilder();
        for (String uuidString : uuidShorts) {
            stringBuilder.append(uuidString);
        }
        Token token;
        token = new Token();
        String value = stringBuilder.toString();
//    value = name + TOKEN_SEPARATOR + value;
        token.setValue(value);
        token.setGeneratorTime(System.currentTimeMillis());
        return token;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy