(十)、SpringBoot+Security 发送短信验证码

发布时间:2018-03-20 14:35  浏览次数:17

可以前往第一篇博客查看目录结构 --> 这里 <http://blog.csdn.net/zeketao/article/details/79522968>

一、在core模块下properties包中创建SmsCodeProperties
public class SmsCodeProperties { //短信验证码长度 private int length = 6; //验证码过期时间
private int expireIn = 60; private String url; public int getLength() { return
length; } public void setLength(int length) { this.length = length; } public
int getExpireIn() { return expireIn; } public void setExpireIn(int expireIn) {
this.expireIn = expireIn; } public String getUrl() { return url; } public void
setUrl(String url) { this.url = url; } }

二、在ValidateCodeProperties中new一个SmsCodeProperties对象,并实现getter、setter方法
public class ValidateCodeProperties { private ImageCodeProperties image = new
ImageCodeProperties(); private SmsCodeProperties sms = new SmsCodeProperties();
public SmsCodeProperties getSms() { return sms; } public void
setSms(SmsCodeProperties sms) { this.sms = sms; } public ImageCodeProperties
getImage() { return image; } public void setImage(ImageCodeProperties image) {
this.image = image; } }

三、在core模块下validate包中创建SmsCodeGenerator实现ValidateCodeGenerator接口。
@Component public class SmsCodeGenerator implements ValidateCodeGenerator {
@Autowired private SecurityProperties securityProperties; /** * 生成短信验证码 *
@param request * @return */ @Override public ValidateCode
generator(ServletWebRequest request) { Random random = new Random(); String
sRand = ""; for (int i = 0; i <
securityProperties.getCode().getSms().getLength(); i++){ String rand =
String.valueOf(random.nextInt(10)); sRand += rand; } return new
ValidateCode(sRand, securityProperties.getCode().getSms().getExpireIn()); } }

四、创建SmsCodeSender接口,定义发送短信的抽象方法
public interface SmsCodeSender { void send(String mobile , String code); }

五、创建SmsCodeSenderImpl实现SmsCodeSender接口
public class SmsCodeSenderImpl implements SmsCodeSender { @Override public
void send(String mobile, String code) { System.out.println("向手机" + mobile +
",发送短信验证码" + code); } }

六、在ValidateCodeBeanConfig中把SmsCodeSenderImpl注入到容器中
@Bean @ConditionalOnMissingBean(name = "smsCodeSender") public SmsCodeSender
smsCodeSender(){ return new SmsCodeSenderImpl(); }

七、在ValidateCodeController中创建短信验证接口方法,同时@Autowired进去两个接口
@Autowired private ValidateCodeGenerator smsCodeGenerator; @Autowired private
SmsCodeSender smsCodeSender; /** * 短信验证码生成、保存、发送 * @param request * @param
response * @throws ServletRequestBindingException */ @GetMapping("/code/sms")
public void createSmsCode(HttpServletRequest request, HttpServletResponse
response) throws ServletRequestBindingException { //生成短信验证码 ValidateCode
generator = smsCodeGenerator.generator(new ServletWebRequest(request));
//把短信验证码保存到session中 sessionStrategy.setAttribute(new
ServletWebRequest(request),SESSION_KEY,generator); //获取表单提交的手机号码 String mobile
= ServletRequestUtils.getRequiredStringParameter(request, "mobile");
//通过短信供应商写出去 smsCodeSender.send(mobile,generator.getCode()); }

八、在browser模块的resources -> zeke-login.html中创建短信验证登录的表单
<br> <br> <h3>短信验证登录</h3> <form action="/authentication/mobile"
method="post"> <table> <tr> <td>手机号:</td> <td><input type="text" name="mobile"
value="13544020124"/></td> </tr> <tr> <td>短信验证码:</td> <td><input type="text"
name="smsCode"/></td> <a href="/code/sms?mobile=13544020124">发送验证码</a> </tr>
<tr> <td colspan="2"><button type="submit">登录</button></td> </tr> </table>
</form>

九、在browser模块下BrowserSecurityConfig添加/code/sms到.antMatchers中




十、访问localhost/zeke-login.html,点击发送验证码,查看console打印的信息


标签

归档

阅读排行

支付宝搜索“559315787”,天天领红包