package com.css.simulation.oauth.server.cofiguration.oauth;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;

@Data
@Configuration
@ConfigurationProperties("oauth.token")
public class MyTokenService {

    private int accessTokenValiditySeconds;
    private int refreshTokenValiditySeconds;

    @Autowired
    private ClientDetailsService clientDetailsService;  // 客户端详情服务
    @Autowired
    private TokenStore tokenStore;  // 客户端详情服务

    /**
     * 令牌管理策略
     */
    @Bean
    public AuthorizationServerTokenServices authorizationServerTokenServices() {
        DefaultTokenServices services = new DefaultTokenServices();
        services.setClientDetailsService(clientDetailsService);     // 客户端详情服务，获取 ClientDetailsServiceConfigurer 中配置的客户端
        services.setSupportRefreshToken(true);      // 允许令牌自动刷新
        services.setReuseRefreshToken(false);       //禁止refreshToken重复使用
        services.setTokenStore(tokenStore);         // 令牌存储策略
//        services.setTokenEnhancer(jwtAccessTokenConverter);         // 使用 jwt 令牌
        services.setAccessTokenValiditySeconds(accessTokenValiditySeconds);   // 令牌默认有效期
        services.setRefreshTokenValiditySeconds(refreshTokenValiditySeconds);    // 刷新令牌默认有效期 。
        return services;
    }

}
