這裡以redis為例
建立一個RedisAppSecretManager類實現AppSecretManager介面
/** * 使用方式: * * <pre>@Autowiredprivate AppSecretManager appSecretManager;@Overrideprotected void initApiConfig(ApiConfig apiConfig) { ... apiConfig.setAppSecretManager(appSecretManager); ...} * </pre> * * @author tanghc * */@Componentpublic class RedisAppSecretManager implements AppSecretManager { public static String APP_KEY_PREFIX = "easyopen_app_key:"; @Autowired private StringRedisTemplate stringRedisTemplate; @Override public void addAppSecret(Map<String, String> appSecretStore) { stringRedisTemplate.opsForHash().putAll(APP_KEY_PREFIX, appSecretStore); } @Override public String getSecret(String appKey) { return (String)stringRedisTemplate.opsForHash().get(APP_KEY_PREFIX, appKey); } @Override public boolean isValidAppKey(String appKey) { if (appKey == null) { return false; } return getSecret(appKey) != null; }}
存放app_key和secret採用hash set的方式,這樣在redis中查看會比較方便,一目瞭然.
然後在IndexController中:
@Autowiredprivate AppSecretManager appSecretManager;@Overrideprotected void initApiConfig(ApiConfig apiConfig) { ... apiConfig.setAppSecretManager(appSecretManager); ...}