ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

05.获取验证码服务

2019-09-18 21:04:56  阅读:206  来源: 互联网

标签:String 05 sms 验证码 springframework 获取 import org com


1. 概述

创建一个sms模块用来实现短信功能



2. 创建短信模块sms

2.1. 创建一个名为 sms的模块,POM 配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.banmingi.blog</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>sms</artifactId>
    <packaging>pom</packaging>
    <inceptionYear>2019-Now</inceptionYear>

    <modules>
        <module>sms-checkcode-feign</module>
        <module>sms-checkcode-service</module>
    </modules>

    <dependencies>
        <!-- Spring Boot Begin -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <!-- Spring Boot End -->
        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>io.github.openfeign</groupId>-->
<!--            <artifactId>feign-okhttp</artifactId>-->
<!--        </dependency>-->
        <!-- Spring Cloud End -->
        <!-- 工具类 Begin -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
        </dependency>
        <!-- 工具类 End -->
    </dependencies>

    <licenses>
        <license>
            <name>Apache 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <id>374564448</id>
            <name>banmingi</name>
            <email>l374564448@qq.com</email>
        </developer>
    </developers>
</project>

2.2. 在sms模块下创建用户注册模块sms-checkcode-service

POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.banmingi.blog</groupId>
        <artifactId>sms</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>sms-checkcode-service</artifactId>
    <packaging>jar</packaging>
    <inceptionYear>2019-Now</inceptionYear>

    <dependencies>
        <!-- Alibaba Cloud Begin -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- Alibaba Cloud End -->
    </dependencies>

    <licenses>
        <license>
            <name>Apache 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>
    <developers>
        <developer>
            <id>374564448</id>
            <name>banmingi</name>
            <email>l374564448@qq.com</email>
        </developer>
    </developers>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.banmingi.blog.sms.SmsCheckCodeApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application.yml:

spring:
  application:
    name: sms-checkcode
    main:
      allow-bean-definition-overriding: true
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.xx.xxx:8848

server:
  port: 9999

#SMS
SMS:
  action: send
  userid: xxxx
  account: 130xxxx5135
  password: xxxxxx
  url: http://sms.hutonginfo.com/sms.aspx


management:
  endpoints:
    web:
      exposure:
        include: "*"

SmsCheckCodeApplication:
@EnableDiscoveryClient提供服务注册发现

package com.banmingi.blog.sms;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @auther 半命i 2019/9/17
 * @description
 */
@SpringBootApplication
@EnableDiscoveryClient
public class SmsCheckCodeApplication {
    public static void main(String[] args) {
        SpringApplication.run(SmsCheckCodeApplication.class, args);
    }
}

controller:

package com.banmingi.blog.sms.controller;

import com.banmingi.blog.sms.service.CheckCodeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * 验证码.
 * @auther 半命i 2019/9/17
 * @description
 */
@RestController
@RequestMapping(value = "checkcode")
public class CheckCodeController {
    @Resource
    private CheckCodeService checkCodeService;

    /**
     * 获取验证码.
     * @param number 接收验证码的手机
     * @return
     */
    @GetMapping(value = "/getCheckCode")
    private String getCheckCode(@RequestParam(name = "number") String number) {
        //生成6位随机验证码
        String checkCode = Integer.toString((int) ((Math.random() * 9 + 1) * 100000));
        return checkCodeService.getCheckCode(number,checkCode);
    }
}

service:

package com.banmingi.blog.sms.service;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.XML;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @auther 半命i 2019/9/17
 * @description 获取验证码
 */
@Service
public class CheckCodeService {

    @Value("${SMS.action}")
    private String action;
    @Value("${SMS.userid}")
    private String userid;
    @Value("${SMS.account}")
    private String account;
    @Value("${SMS.password}")
    private String password;
    @Value("${SMS.url}")
    private String url;

    /**
     * 获取验证码.
     * @param mobile 接收验证码的手机号码
     * @param checkCode 指定的6位验证码
     * @return
     */
    public  String getCheckCode(String mobile,String checkCode) {
        OkHttpClient client = new OkHttpClient();

        FormBody.Builder body = new FormBody.Builder();
        body.add("action",action);
        body.add("userid",userid);
        body.add("account",account);
        body.add("password",password);
        body.add("mobile",mobile);
        String content = "【banmingi】您的验证码是" + checkCode + "。如非本人操作,请忽略此短信。";
        body.add("content",content);
        Request request = new Request.Builder()
                .url(url)
                .post(body.build()) //post请求
                .build();
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()){
                //接收到的是xml格式的数据
                String string = response.body().string();
                //xml转换为json
                return XML.toJSONObject(string).toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

2.3. 在sms模块下创建用户注册模块sms-checkcode-feign向外提供Feign接口

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.banmingi.blog</groupId>
        <artifactId>sms</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>sms-checkcode-feign</artifactId>
    <packaging>jar</packaging>
    <inceptionYear>2019-Now</inceptionYear>

    <licenses>
        <license>
            <name>Apache 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>
    <developers>
        <developer>
            <id>374564448</id>
            <name>banmingi</name>
            <email>l374564448@qq.com</email>
        </developer>
    </developers>
</project>

feign:
通过 @FeignClient(“服务名”) 注解来指定调用哪个服务

package com.banmingi.blog.sms.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @auther 半命i 2019/9/17
 * @description
 */
@FeignClient(value = "sms-checkcode",path = "checkcode")
public interface CheckCodeFeign {

    @GetMapping(value = "/getCheckCode")
    public String getCheckCode(@RequestParam(name = "number") String number);
}


3. 在consumer-reg-service中使用getCheckCode功能

首先POM新增sms-checkcode-feign模块:

<dependency>
   <groupId>com.banmingi.blog</groupId>
    <artifactId>sms-checkcode-feign</artifactId>
    <version>${project.parent.version}</version>
</dependency>

在主启动类上通过 @EnableFeignClients 注解开启 Feign 功能

package com.banmingi.blog.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @auther 半命i 2019/9/13
 * @description
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients("com.banmingi.blog.sms.feign")
public class ConsumerRegApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerRegApplication.class,args);
    }
}

注册服务的RegController增加获取验证码的请求:

 @Resource
    private CheckCodeFeign checkCodeFeign;

    /**
     * 获取验证码
     * @param number
     * @return
     */
    @GetMapping(value = "/getCheckCode")
    public String getCheckCode(@RequestParam(name = "number") String number) {
        return checkCodeFeign.getCheckCode(number);
    }

标签:String,05,sms,验证码,springframework,获取,import,org,com
来源: https://blog.csdn.net/banmingi/article/details/100997127

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有