|
@@ -2,13 +2,17 @@ package com.chelvc.framework.sms.config;
|
|
|
|
|
|
import com.aliyun.dysmsapi20170525.Client;
|
|
|
import com.aliyun.teaopenapi.models.Config;
|
|
|
+import com.chelvc.framework.sms.support.SwitchableCaptchaSmsHandler;
|
|
|
import com.github.qcloudsms.SmsSingleSender;
|
|
|
import com.github.qcloudsms.httpclient.PoolingHTTPClient;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.support.GenericApplicationContext;
|
|
|
|
|
|
/**
|
|
|
* 短信配置
|
|
@@ -18,7 +22,10 @@ import org.springframework.context.annotation.Configuration;
|
|
|
*/
|
|
|
@Configuration
|
|
|
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
|
|
-public class SmsConfigurer {
|
|
|
+public class SmsConfigurer implements BeanPostProcessor {
|
|
|
+ private volatile boolean initialized;
|
|
|
+ private final GenericApplicationContext applicationContext;
|
|
|
+
|
|
|
/**
|
|
|
* 构建阿里云短信配置实例
|
|
|
*
|
|
@@ -61,4 +68,28 @@ public class SmsConfigurer {
|
|
|
public SmsSingleSender tencentSmsSender(TencentSmsProperties properties) {
|
|
|
return new SmsSingleSender(properties.getAppid(), properties.getSecret(), new PoolingHTTPClient());
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化可切换短信处理器
|
|
|
+ */
|
|
|
+ private void initializeSwitchableSmsHandler() {
|
|
|
+ // 初始化可切换验证码短信处理器
|
|
|
+ if (this.applicationContext.containsBean(AliyunSmsProperties.class.getName())
|
|
|
+ || this.applicationContext.containsBean(TencentSmsProperties.class.getName())) {
|
|
|
+ this.applicationContext.registerBean(SwitchableCaptchaSmsHandler.class);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
|
|
+ if (!this.initialized) {
|
|
|
+ synchronized (this) {
|
|
|
+ if (!this.initialized) {
|
|
|
+ this.initializeSwitchableSmsHandler();
|
|
|
+ this.initialized = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
}
|