package com.css.simulation.resource.common.infra.config.oauth;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;

import javax.annotation.Resource;

@Configuration
public class MyTokenServices {

    @Resource
    private OauthParameter oauthParameter;


    /**
     * access_token 验证策略
     * DefaultTokenServices()
     * RemoteTokenServices()：远程验证，通过 /oauth/check_token
     * SpringSocialTokenServices()
     * UserInfoTokenServices()
     */
    @Bean
    public ResourceServerTokenServices tokenServices() {
        RemoteTokenServices services = new RemoteTokenServices();
        services.setCheckTokenEndpointUrl(oauthParameter.getCheckTokenEndpointUrl());  // 需要在授权服务器公开 /oauth/check_token
        services.setClientId(oauthParameter.getClientId());
        services.setClientSecret(oauthParameter.getClientSecret());
        return services;
    }

//* -------------------------------- 使用 jwt 令牌，无需访问远程授权服务器，也可以两者结合 --------------------------------
//    /**
//     * 令牌存储
//     * <p>
//     * （推荐）new JwtTokenStore()：基于 Jwt
//     * （推荐）new RedisTokenStore()：基于 redis
//     * new InMemoryTokenStore()：基于内存
//     * new JwkTokenStore()：基于 Jwk
//     * new JdbcTokenStore()：基于 Jdbc
//     */
//    @Bean
//    public TokenStore tokenStore() {
////        return new InMemoryTokenStore();
//        return new JwtTokenStore(accessTokenConverter());
//    }
//
//    @Bean
//    public JwtAccessTokenConverter accessTokenConverter() {
//        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
//        jwtAccessTokenConverter.setSigningKey("project");
//        return jwtAccessTokenConverter;
//    }
}
