短信验证码

1.使用阿里云的短信api,将其进行组件抽取

(1)创建一个spring.factories文件(META-INF/spring.factories)

#声明我要自动装配一个smsTemplate
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
\com.itcast.tanhua.autoconfig.TanHuaAutoConfiguration

(2)定义配置对象,引入配置

<1>添加配置application.yml文件

sms:
  signName: 验证码短信
  templateCode: SMS_280126445
  accessKey: 
  secret: 

<2>引用配置文件参数

(注意参数名要相同)

@Data
@ConfigurationProperties(prefix = "tanhua.sms")
public class SmsProperties {
    //这些属性名称需要与app-server中的属性名称一致
    private String signName;
    private String templateCode;
    private String accessKey;
    private String secret;
}

<3>开启注解自动识别配置对象

@EnableConfigurationProperties({SmsProperties.class})

(3)添加模板类对象

public class SmsTemplate {

    @Autowired
    private SmsProperties properties;

    public SmsTemplate(SmsProperties properties) {
        this.properties = properties;
    }

    public void sendSms(String mobile, String code) {
        try {
//        配置阿里云
            Config config = new Config().
                    setAccessKeyId(properties.getAccessKey()).
                    setAccessKeySecret(properties.getSecret());
//        设置访问域名
            config.endpoint = "dysmsapi.aliyuncs.com";

            Client client = new Client(config);

            SendSmsRequest request = new SendSmsRequest().
                    setPhoneNumbers(mobile).
                    setSignName(properties.getSignName()).
                    setTemplateCode(properties.getTemplateCode()).
                    setTemplateParam("{\"code\":\"" + code + "\"}");

            SendSmsResponse response = client.sendSms(request);

            SendSmsResponseBody body = response.getBody();
            System.out.println(body.getMessage());


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

 (4)定义一个自动装配类

//开启注解使能够自动识别道smsproperties,在其中可以添加多个
@EnableConfigurationProperties({
        SmsProperties.class
})
public class TanHuaAutoConfiguration {
    @Bean
    public SmsTemplate smsTemplate(SmsProperties properties) {
        return new SmsTemplate(properties);
    }
}

2.获取短信验证码业务流程以及实现

(1)短信验证码接口

(2)流程分析

(3)获取登录验证码流程

1、搭建SpringBoot运行环境(引导类,配置文件)
2、编写Controller接受请求参数
3、定义业务层方法,根据手机号码发送短信
4、数据响应

 <1>编写Controller

    @PostMapping("/login")
    public ResponseEntity String(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        userService.sendMsg(phone);
//        正常返回状态码200
        return ResponseEntity.ok(null);
    }

<2>业务层

    public void sendMsg(String phone) {
//        1.随机生成六位数字
        String code = RandomStringUtils.randomNumeric(6);
//        2.调用ssmtemplate,发送手机验证码
        smsTemplate.sendSms(phone, code);
//        3.将验证码存到redis中 存活时间设置为5分钟
        redisTemplate.opsForValue().set("CHECK_CODE_" + phone, code, Duration.ofMinutes(5));
    }

<3>响应

使用responseEntity来实现

//        正常返回状态码200
        return ResponseEntity.ok(null);
### Mish 激活函数简介 Mish 是一种自门控激活函数,其定义为 \( f(x) = x \cdot \tanh(\text{softplus}(x)) \),其中 softplus 函数表示 \( \text{softplus}(x) = \ln(1 + e^x) \)[^4]。相比传统的 ReLU 其他变体,Mish 的设计使其能够更好地处理梯度消失问题并提供更平滑的导数特性。 #### Mish 在不同框架中的实现与使用方法 #### PyTorch 中的 Mish 实现 在 PyTorch 中,虽然官方并未直接内置 Mish 激活函数,但通过扩展 `torch.nn.Module` 可轻松实现该功能[^3]: ```python import torch import torch.nn as nn class Mish(nn.Module): def __init__(self): super(Mish, self).__init__() def forward(self, x): return x * (torch.tanh(torch.functional.F.softplus(x))) ``` 此外,借助第三方库如 **PyTorch Lightning**,可以直接调用已有的 Mish API 来简化开发流程[^2]。 #### TensorFlow/Keras 中的 Mish 实现 对于 TensorFlow 用户而言,可通过安装额外插件包 **TensorFlow Addons** 获得对 Mish 支持的能力[^1]。具体操作如下所示: ```python import tensorflow_addons as tfa from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense model = Sequential([ Dense(64, activation=tfa.activations.mish), ]) ``` 上述代码片段展示了如何利用 TensorFlow Addons 提供的功能快速构建基于 Mish 激活层的神经网络结构。 #### 性能表现评估 经过广泛实验验证,在超过七十项任务场景下对比分析表明,相较于其它常见候选方案(例如 Swish 或 GELU),采用 Mish 后模型往往具备更高精度以及更快收敛速度特点[^5]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值