Springboot短信验证和第三方登录

短信验证

准备工作:登录阿里云控制台->购买短息服务->创建签名->创建模板。
登录控制台购买短信服务
登录控制台购买短信服务
创建签名(注意:创建access的子账户时需保存access的相关信息值)
创建签名
创建发短信的短信内容的模板
创建发短信的短信内容的模板

1.加依赖

<!--阿里云短信包-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.0.0</version>
        </dependency>

2.创建发送短信的工具类

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class SendNoteUtil {
   

    //验证平台信息 开发者无需任何更改
    private static final String dysmsapi = "dysmsapi.aliyuncs.com";

    DefaultProfile profile = DefaultProfile.getProfile("default", "accessKeyId", "accessKeySecret");//此处需特别注意在创建access的子账户时需保存这写参数,后期找不到或者不好找
    IAcsClient client = new DefaultAcsClient(profile);
    //这一步的两个参数,一个是要发送验证码的手机号 一个是模板Code(区分登陆和注册(留白))
    public String sendNoteMessgae(String PhoneNumbers,String template){
   
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for(int i=0;i<6;i++){
   
            sb.append(random.nextInt(10));
        }
        CommonRequest request = new CommonRequest();
        //request.setSysProtocol(ProtocolType.HTTPS);
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(dysmsapi);
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", PhoneNumbers);//接受验证码的手机号
        request.putQueryParameter("SignName", "公司名称");//签名
        //模板代码,我暂时用的参数,你可以直接写成模板码
        request.putQueryParameter("TemplateCode", template);
        //用户定义的验证码内容
        request.putQueryParameter("TemplateParam","{code:"+sb.toString()+"}");
        try {
   
            CommonResponse response = client.getCommonResponse(request);
            String returnStr = response.getData();
            System.out.println(returnStr);
            JSONObject jsonObject = JSONObject.parseObject(returnStr);
            //返回的信息
            return jsonObject.getString("Message");
        } catch (ServerException e) {
   
            return e.getErrMsg();
        } catch (ClientException e) {
   
            return e.getErrMsg();
        }
    }
}

3.写controller验证

import com.example.util.SendNoteUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping(value = "/api/note")
public class SendNoteController {
   

    @Autowired
    private SendNoteUtil sendNoteUtil;
	//发送验证码(参数为手机号和response请求)
    @RequestMapping(value = "/sendNote",method = RequestMethod.GET)
    public void sendNote(String phone, HttpServletResponse response){
   
        String template = "SMS_185315027";
        try {
   
            response.getWriter().write(sendNoteUtil.sendNoteMessgae(phone,template));
        } catch (IOException e) {
   
            e.printStackTrace();
        }
    }
}

第三方登录

需创建6个类、1个html页面,增加配置文件内容
1.申请QQ的开发者(https://connect.qq.com/manage.html#/appinfo/web/101513767)
登录后点击右边的个人头像,填写基本信息后点击提交即可等待审核
在这里插入图片描述
审核成功后就可以创建应用了
在这里插入图片描述
创建好后有三个地方需要注意(APP ID、APP Key、网站回调域)
在这里插入图片描述
2.添加依赖到POM.xml的dependencies标签中

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</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>
            <optional>true</optional>
        </dependency> 
        
        <!-- thymeleaf 模版   -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        
		 <!-- web -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<!-- 以下是 qq wx 联合登陆需要的相关依赖工具 commons-io, commons-lang3,httpclient,fastjson-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值