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笔记三

 

 

 springboot笔记三

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

 springboot笔记三

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

 

 springboot笔记三

 

 

 springboot笔记三

 

 方法抽取:

springboot笔记三

 

 springboot笔记三

 

 默认线程池大小是八个

补充:线程池配置:
springboot笔记三

 

 可用属性:
springboot笔记三\

 

上一篇:Mybatis总结-基本使用


下一篇:git指定仓库使用特定用户名提交