使用AWS SNS从Java Web应用程序发送短信 [英] sending sms from java web app using aws sns

查看:95
本文介绍了使用AWS SNS从Java Web应用程序发送短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户将在我的Web应用程序上创建一个帐户.该应用将需要通过向用户提供的手机号码发送短信来对用户进行身份验证.短信是一则短短的唯一代码,用户需要将其输入到Web浏览器中,以便该应用对用户进行身份验证.

A user will create an account on my web app. The app will need to authenticate the user by sending a text message to the mobile phone number that the user provides. The text message is a short unique code, which the user will need to type in to the web browser in order for the app to authenticate the user.

如何为此用例配置Amazon AWS SNS?

根据我的阅读,SNS由网站管理员选择 topic 进行工作,然后每个用户都订阅该主题.然后,网站管理员将消息广播样式发送给该主题的所有订阅者.这不能满足我向每个用户发送唯一消息的要求.另外,为每个电话号码创建单独的 topic 太麻烦了,更不用说在保护所有电话号码的隐私方面造成安全问题.

From what I have read, SNS works by the webmaster selecting a topic and then each user subscribes to that topic. Then the webmaster sends messages broadcast style to all the subscribers to the topic. This would not meet my requirements of sending a unique message to each user. Alternatively, creating a separate topic for every phone number would be too cumbersome, not to mention creating security issues with respect to protecting the privacy of all the phone numbers.

可以为此用例配置Amazon AWS SNS吗?如果是这样,怎么办?

我正在使用Java.

推荐答案

AWS SDK中已弃用某些方法.

Some methods have been deprecated in AWS SDK.

BasicAWSCredentials awsCredentials = new BasicAWSCredentials("CLIENT-ID", "SECRET-KEY");
AmazonSNS snsClient = AmazonSNSClientBuilder.standard()
                     .withRegion(Regions.fromName("YOUR_REGION"))
     .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build();

Map<String, MessageAttributeValue> smsAttributes = new HashMap<String, MessageAttributeValue>();
smsAttributes.put("AWS.SNS.SMS.SenderID",new MessageAttributeValue().withStringValue("SENDER-ID").withDataType("String");
smsAttributes.put("AWS.SNS.SMS.SMSType",new MessageAttributeValue().withStringValue("Transactional").withDataType("String"));

PublishRequest request = new PublishRequest();
request.withMessage("YOUR MESSAGE")
.withPhoneNumber("E.164-PhoneNumber")
.withMessageAttributes(smsAttributes);
PublishResult result=snsClient.publish(request);

这篇关于使用AWS SNS从Java Web应用程序发送短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆