Java教程

springboot笔记三

本文主要是介绍springboot笔记三,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
 

------------恢复内容开始------------

一.springboot发送消息到邮箱并设置60秒有效时长(存到redis里面)

导入两个依赖:

<!--邮箱-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>

    <!--reids-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

前端页面:

<!DOCTYPE html>
<html xmlns:>

<!-- Head -->
<head>
    <title>注册页面</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="application/x-javascript"> addEventListener("load", function () {
        setTimeout(hideURLbar, 0);
    }, false);

    function hideURLbar() {
        window.scrollTo(0, 1);
    } </script>
    <link rel="stylesheet" href="css/loginRegister.css" type="text/css" media="all">
</head>

<body>

<h1>springboot学习系统</h1>

<div class="container">
    <div class="login">
        <h2>员工登录</h2>
        <div class="form">
            <input type="text"  placeholder="请输入公司邮箱" v-model="email">
            <button class="sendCode" @click="sendCheckCode">发送验证码</button>
            <input type="text" placeholder="密码" >
            <button class="loginBtn">登录</button>
        </div>
    </div>
</div>

<script src="./js/vue.min.js"></script>
<script src="./js/axios.min.js"></script>
</body>


<script>

    new Vue({
        el:".login",
        data:{
            email:"1136403002@qq.com"
        },
        methods:{
            sendCheckCode(){
                // console.log("蛋妞点击")
                axios.get("sendEmailCode?email="+this.email)
            }

        }


    })


</script>

</html>

controller

@RestController
public class EmailController {

//        与component注解共同使用
@Autowired
private EmailService emailService;
@Autowired
private JavaMailSender javaMailSender;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("sendEmailCode")
    public String sendEmailCode(String email){
        String code="1346";
        SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
        simpleMailMessage.setFrom("验证码,请查收<18859986862@163.com>");
        simpleMailMessage.setSubject("验证码");
        simpleMailMessage.setText(code);
        simpleMailMessage.setTo(email);

        stringRedisTemplate.opsForValue().set("redis_key_"+email,code,60,TimeUnit.SECONDS);
        javaMailSender.send(simpleMailMessage);

        return "success";
    }


}

 部分配置:

  mail:
    host: smtp.163.com
    username: 
    password: 
    default-encoding: utf-8
  redis:
    database: 1

 跳转config:(实现一进去就跳到login.xml)

package stu.adam.springmybaitsgen.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class MVCConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/","login.html");
    }
}

效果:(redis数据库的东西将在一分钟后消失)

 

 

 

二. 前端点击登入发送验证码

 

三.异步任务(解决发送的速度慢的问题)

 

 

 

 

 

 

 方法抽取:

 

 

 

 默认线程池大小是八个

补充:线程池配置:

 

 可用属性:
\

 

这篇关于springboot笔记三的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!