Skip to content

Commit

Permalink
Fix aliyun provider jar conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
cooperlyt committed Aug 10, 2024
1 parent bd1aab6 commit 8da690c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 33 deletions.
5 changes: 4 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ COPY providers/keycloak-sms-provider-yunxin.jar /opt/keycloak/providers/

#tencent provider
COPY providers/keycloak-sms-provider-tencent.jar /opt/keycloak/providers/
#

#aliyun provider
COPY providers/keycloak-sms-provider-aliyun.jar /opt/keycloak/providers/

#aws provider
COPY providers/keycloak-sms-provider-aws-sns.jar /opt/keycloak/providers/

#wx app provider
COPY providers/keycloak-wx-provider-app.jar /opt/keycloak/providers/

RUN mkdir /opt/keycloak/certs

RUN /opt/keycloak/bin/kc.sh build
Expand Down
2 changes: 1 addition & 1 deletion examples/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:
ports:
- 8080:8080
command:
- start-dev --spi-phone-default-service=dummy
- start-dev
environment:
KEYCLOAK_ADMIN: admin
KEYCLOAK_ADMIN_PASSWORD: admin
Expand Down
2 changes: 1 addition & 1 deletion keycloak-sms-provider-aliyun/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cp target/providers/keycloak-sms-provider-aliyun.jar ${KEYCLOAK_HOME}/providers/
${KEYCLOAK_HOME}/bin/kc.sh build

${KEYCLOAK_HOME}/bin/kc.sh start --spi-phone-default-service=aliyun \
--spi-message-sender-service-aliyun-region=cn-hangzhou \
--spi-message-sender-service-aliyun-token=${token} \
--spi-message-sender-service-aliyun-key=${accessKey} \
--spi-message-sender-service-aliyun-secret=${accessSecret} \
--spi-message-sender-service-aliyun-otp-template={templateId}
Expand Down
17 changes: 15 additions & 2 deletions keycloak-sms-provider-aliyun/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,23 @@
<version>2.4.1-snapshot</version>
<scope>provided</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.aliyun</groupId>-->
<!-- <artifactId>aliyun-java-sdk-dysmsapi</artifactId>-->
<!-- <version>2.2.1</version>-->
<!-- </dependency>-->


<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.3</version>
<artifactId>alibabacloud-dysmsapi20170525</artifactId>
<version>3.0.0</version>
<exclusions>
<exclusion>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,67 @@
import cc.coopersoft.keycloak.phone.providers.exception.MessageSendException;
import cc.coopersoft.keycloak.phone.providers.spi.MessageSenderService;
import cc.coopersoft.common.OptionalUtils;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import darabonba.core.client.ClientOverrideConfiguration;
import org.jboss.logging.Logger;
import org.keycloak.Config;
import org.keycloak.models.RealmModel;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;

public class AliyunSmsSenderServiceProvider implements MessageSenderService {

private static final Logger logger = Logger.getLogger(AliyunSmsSenderServiceProvider.class);

private final Config.Scope config;
private final RealmModel realm;
private final IAcsClient client;
private final AsyncClient client;

public AliyunSmsSenderServiceProvider(Config.Scope config, RealmModel realm) {
this.config = config;
this.realm = realm;
DefaultProfile profile = DefaultProfile.getProfile(config.get("region") , config.get("key"), config.get("secret"));
client = new DefaultAcsClient(profile);

// HttpClient Configuration
/*HttpClient httpClient = new ApacheAsyncHttpClientBuilder()
.connectionTimeout(Duration.ofSeconds(10)) // Set the connection timeout time, the default is 10 seconds
.responseTimeout(Duration.ofSeconds(10)) // Set the response timeout time, the default is 20 seconds
.maxConnections(128) // Set the connection pool size
.maxIdleTimeOut(Duration.ofSeconds(50)) // Set the connection pool timeout, the default is 30 seconds
// Configure the proxy
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<your-proxy-hostname>", 9001))
.setCredentials("<your-proxy-username>", "<your-proxy-password>"))
// If it is an https connection, you need to configure the certificate, or ignore the certificate(.ignoreSSL(true))
.x509TrustManagers(new X509TrustManager[]{})
.keyManagers(new KeyManager[]{})
.ignoreSSL(false)
.build();*/


// Configure Credentials authentication information, including ak, secret, token
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
.accessKeyId(config.get("key"))
.accessKeySecret(config.get("secret"))
.securityToken(config.get("token")) // use STS token
.build());

// Configure the Client
client = AsyncClient.builder()
//.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
.credentialsProvider(provider)
//.serviceConfiguration(Configuration.create()) // Service-level configuration
// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
.overrideConfiguration(
ClientOverrideConfiguration.create()
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
.setEndpointOverride("dysmsapi.ap-southeast-1.aliyuncs.com")
//.setConnectTimeout(Duration.ofSeconds(30))
)
.build();

}

Expand All @@ -40,27 +75,35 @@ public void sendSmsMessage(TokenCodeType type, String phoneNumber, String code,
String templateId = Optional.ofNullable(config.get(realm.getName().toLowerCase() + "-" + kindName + "-template"))
.orElse(config.get(kindName + "-template"));

CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("dysmsapi.aliyuncs.com");
request.setSysVersion("2017-05-25");
request.setSysAction("SendSms");
request.putQueryParameter("RegionId", config.get("region"));
request.putQueryParameter("PhoneNumbers", phoneNumber);
request.putQueryParameter("SignName", realm.getDisplayName().toLowerCase());
request.putQueryParameter("TemplateCode", templateId);

request.putQueryParameter("TemplateParam", String.format("{\"code\":\"%s\",\"expires\":\"%s\"}",code,expires / 60));
try {
CommonResponse response = client.getCommonResponse(request);
logger.debug(response.getData());
} catch (ClientException e) {
throw new MessageSendException(500,e.getErrCode(),e.getMessage());
}
// Parameter settings for API request
SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
.phoneNumbers(phoneNumber)
.signName(realm.getDisplayName().toLowerCase())
.templateCode(templateId)
.templateParam(String.format("{\"code\":\"%s\",\"expires\":\"%s\"}",code,expires / 60))
// Request-level configuration rewrite, can set Http request parameters, etc.
// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
.build();

// Asynchronously get the return value of the API request
CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
// Synchronously get the return value of the API request
//SendSmsResponse resp = response.get();
//System.out.println(new Gson().toJson(resp));
// Asynchronous processing of return values
/*response.thenAccept(resp -> {
System.out.println(new Gson().toJson(resp));
}).exceptionally(throwable -> { // Handling exceptions
System.out.println(throwable.getMessage());
return null;
});*/

// Finally, close the client
client.close();
}

@Override
public void close() {

client.close();
}
}

0 comments on commit 8da690c

Please sign in to comment.