Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now

メニュー

Expand
Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Twilio Verify Java Servlets Quickstart

With just a few lines of code, your Java application can verify phone numbers and add an additional layer of security with Twilio Verify.

This Java Verify Quickstart will teach you how to do this using our Verify REST API, the Twilio Java helper library, and Java Servlet Technology to ease development.

このクイックスタートでは、下記のことを学んでいきます:

  1. Twilioにサインアップ
  2. メッセージを送受信できるように開発環境をセットアップする
  3. Send your first SMS phone verification
  4. Check verification codes

Twilioにサインアップ

If you already have a Twilio account, you’re all set here! Feel free to jump to the next step.

Before you can send an SMS from Java, you'll need to sign up for a Twilio account or sign into your existing account.

You can sign up for a free Twilio trial account here.

  • When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to send test verification messages to your phone from your Twilio account while in trial mode. This phone verification step is exactly what you'll learn how to build in this tutorial!
  • Once you verify your number, you'll be asked to create a project. For the sake of this tutorial, you can click on the "Learn and Explore" template. Give your project a name, or just click "skip remaining steps" to continue with the default.
  • Once you get through the project creation flow, you'll arrive at your project dashboard in the Twilio Console. This is where you'll be able to access your Account SID, authentication token, create a verification service, and more.

Do I need a phone number?

If you've sent SMS with Twilio in the past, you might remember needing to buy a phone number. With Twilio Verify, we take care of that for you! The Verify API selects the best routes for quickly and reliably delivering verification codes globally.

Now that you have a Twilio account and a verification service, you can start writing some code! To make things even easier we will use Twilio's official helper for Java applications.

Javaインストール

If you’ve gone through one of our other Java Quickstarts already and have Java installed, you can skip this step and get straight to sending your first verification.

To start a phone verification, you’ll need to have Java and the Twilio Java helper library installed.

To send your first SMS, you’ll need to have the Java Standard Edition (SE) Development Kit (JDK) installed - if you don't know if you have the JDK installed, run the following command to see what version you have:

javac -version

下記の出力のようになります:

javac 1.8.0_92

Twilio SDKにはSE 7以降が必要です。 これはバージョン番号「1.7」以上として、あるいは上記のコマンドを実行時に表示されます。

If you have an older version of Java or no JDK at all, you'll need to install the JDK before going any further. Follow the directions for installing the Java SE Development Kit for your platform (Windows, Mac, Linux) from the Java SE Download Page.

検証アプリケーションをクローンおよびセットアップする

Start by cloning our Java Servlets repository.

git clone git@github.com:TwilioDevEd/verify-v2-quickstart-java.git

If you don't have git installed or prefer to download the source code you can grab a zip file of the project here.

Install project dependencies

For this project, we used Apache Maven as the management tool. If you're familiar with Java you probably have Apache Maven installed, if not follow these links to download and install it. Navigate into the project folder and compile the project to install your dependencies.

Twilio Java Helper Library is already in the dependencies of the project in the pom.xml file.

cd verify-v2-quickstart-python

mvn compile

Copy .env.example to .env. This is where we'll store sensitive data in environment variables.

cp .env.example .env

Run the application

mvn flyway:migrate

mvn jetty:run

If your credentials are set up correctly you'll soon get a message that the app is up!

Use the Servlet Twilio Verify Demo

Navigate to http://localhost:8080/register. You should see a registration form that looks like this:

sign up form with phone verification

Enter your phone number and choose which channel to request verification over. Finally hit the green Sign Up button and wait. You'll either receive a phone call or an SMS with the verification token. If you requested a phone call, as an additional security feature you may need to interact to proceed (the call will tell you to enter a number on the phone keypad).

Enter the token into the Verification entry form and click 'Verify':

verification entry form

これで、デモアプリケーションはTwilioの電話番号検証で保護されました! ログアウトして他のチャンネルもお試しいただけます。

関連トピック

これでデモアプリケーションは不正なユーザーによるあなたの会社への登録とデータベースの汚染から守ります。 次は電話番号検証APIリファレンスを参照して、利用可能なすべての変数およびオプションについてご確認ください。 また、(同一のコードベースで)引き続きあなたの顧客を保護するには、Java Servlets Authy二要素認証クイックスタートもお試しください。

After that, visit the Docs for more Account Security demos and tutorials and web applications using all of Twilio's products.

Rate this page:

ヘルプが必要ですか?

誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。