app_key和secret存放在数据库或redis中

这里以redis为例

新建一个RedisAppSecretManager类实现AppSecretManager接口

  1. /**
  2. * 使用方式:
  3. *
  4. * <pre>
  5. @Autowired
  6. private AppSecretManager appSecretManager;
  7. @Override
  8. protected void initApiConfig(ApiConfig apiConfig) {
  9. ...
  10. apiConfig.setAppSecretManager(appSecretManager);
  11. ...
  12. }
  13. * </pre>
  14. *
  15. * @author tanghc
  16. *
  17. */
  18. @Component
  19. public class RedisAppSecretManager implements AppSecretManager {
  20. public static String APP_KEY_PREFIX = "easyopen_app_key:";
  21. @Autowired
  22. private StringRedisTemplate stringRedisTemplate;
  23. @Override
  24. public void addAppSecret(Map<String, String> appSecretStore) {
  25. stringRedisTemplate.opsForHash().putAll(APP_KEY_PREFIX, appSecretStore);
  26. }
  27. @Override
  28. public String getSecret(String appKey) {
  29. return (String)stringRedisTemplate.opsForHash().get(APP_KEY_PREFIX, appKey);
  30. }
  31. @Override
  32. public boolean isValidAppKey(String appKey) {
  33. if (appKey == null) {
  34. return false;
  35. }
  36. return getSecret(appKey) != null;
  37. }
  38. }

存放app_key和secret采用hash set的方式,这样在redis中查看会比较方便,一目了然.

然后在IndexController中:

  1. @Autowired
  2. private AppSecretManager appSecretManager;
  3. @Override
  4. protected void initApiConfig(ApiConfig apiConfig) {
  5. ...
  6. apiConfig.setAppSecretManager(appSecretManager);
  7. ...
  8. }