diff --git a/emsystem/pom.xml b/emsystem/pom.xml index 1e38a16da0e467d6d05b98af7bdc86e1c1307fa7..cd13258f62e3818b87ae8633d0970ee4392ccdbb 100644 --- a/emsystem/pom.xml +++ b/emsystem/pom.xml @@ -31,56 +31,93 @@ 17 + org.springframework.boot spring-boot-starter-jdbc + + org.springframework.boot spring-boot-starter-web + + org.mybatis.spring.boot mybatis-spring-boot-starter 3.0.3 - + + com.auth0 java-jwt 4.2.1 + org.springframework.boot spring-boot-devtools runtime true + + com.mysql mysql-connector-j runtime + + org.projectlombok lombok true + + org.springframework.boot spring-boot-starter-test test + + org.mybatis.spring.boot mybatis-spring-boot-starter-test 3.0.3 test - - + + + + org.apache.commons + commons-dbcp2 + + + + + jakarta.mail + jakarta.mail-api + 2.1.3 + + + + org.springframework.boot + spring-boot-starter-mail + + + + + jakarta.activation + jakarta.activation-api + 2.1.0 org.apache.commons commons-dbcp2 @@ -102,21 +139,6 @@ org.springframework.boot spring-boot-starter-log4j2 - - - com.sun.mail - javax.mail - 1.6.2 - - - org.springframework.boot - spring-boot-starter-mail - - - - jakarta.mail - jakarta.mail-api - diff --git a/emsystem/src/main/java/com/emsystem/config/TokenInterceptorConfig.java b/emsystem/src/main/java/com/emsystem/config/TokenInterceptorConfig.java index 8d1ea52a6c32894fffbce6da5833a0cee429fb27..6769531a5f20839be6e1db4ce4b931c2e21fa71e 100644 --- a/emsystem/src/main/java/com/emsystem/config/TokenInterceptorConfig.java +++ b/emsystem/src/main/java/com/emsystem/config/TokenInterceptorConfig.java @@ -1,28 +1,61 @@ package com.emsystem.config; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import com.emsystem.interceptor.TokenVerifyInterceptor; -//WebMvcConfigurer 替代 webmvc.xml +import java.util.Properties; + @Configuration public class TokenInterceptorConfig implements WebMvcConfigurer { //周筱鹏 @Autowired private TokenVerifyInterceptor t; - + + // 使用 @Value 注解注入邮件服务器配置 + @Value("${mail.host}") + private String mailHost; + + @Value("${mail.port}") + private int mailPort; + + @Value("${mail.username}") + private String mailUsername; + + @Value("${mail.password}") + private String mailPassword; + @Override public void addInterceptors(InterceptorRegistry registry) { - //利用参数registry进行拦截器注册t - //addPathPatterns 表示需要拦截的路径,允许多个String参数 - //excludePathPatterns 表示不需要拦截的路径(拦截路径中的例外) - - //案例中设置的是所有的请求(/**)都需要经过拦截器处理,但是/login是例外 + // 注册拦截器并设置拦截路径和排除路径 registry.addInterceptor(t).addPathPatterns("/**") - .excludePathPatterns("/login"); - + .excludePathPatterns("/login") + .excludePathPatterns("/email"); + } + + @Bean + public JavaMailSender javaMailSender() { + JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); + // 设置邮件服务器配置 + mailSender.setHost(mailHost); + mailSender.setPort(mailPort); + mailSender.setUsername(mailUsername); + mailSender.setPassword(mailPassword); + + Properties props = mailSender.getJavaMailProperties(); + props.put("mail.transport.protocol", "smtp"); + props.put("mail.smtp.auth", "true"); + props.put("mail.smtp.starttls.enable", "true"); + // 不要在生产环境中启用调试模式 + // props.put("mail.debug", "true"); + + return mailSender; } -} +} \ No newline at end of file diff --git a/emsystem/src/main/java/com/emsystem/controller/AuthController.java b/emsystem/src/main/java/com/emsystem/controller/AuthController.java new file mode 100644 index 0000000000000000000000000000000000000000..112591a7df74257f420d87d5c67428058cdb20fd --- /dev/null +++ b/emsystem/src/main/java/com/emsystem/controller/AuthController.java @@ -0,0 +1,34 @@ +package com.emsystem.controller; + +import com.emsystem.response.ResponseResult; +import com.emsystem.service.EmailService; +import com.emsystem.vo.LoginResVO; +import com.emsystem.vo.LoginVO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.Map; + +@RestController +@RequestMapping +public class AuthController { + @Autowired + private EmailService emailService; + @PostMapping("/email") + public ResponseResult sendVerificationCode(@RequestParam String email) { + String code = emailService.sendVerificationCode(email); + System.out.println(code); + ResponseResult r = new ResponseResult(); + r.setCode(200); + r.setMessage("OK"); + r.setData("密码码已发送到邮箱"); + LoginVO vo = new LoginVO(); + vo.setEmail(email); + vo.setPassword(code); + vo.setUsername(email); + emailService.updatePassword(vo); + return r; + } + +} diff --git a/emsystem/src/main/java/com/emsystem/mapper/LoginMapper.java b/emsystem/src/main/java/com/emsystem/mapper/LoginMapper.java index 9d8e6589338b2f7214f4035620b7a4972e686efc..6c5d1038781a4d1799c6e0998d1931bdb18f7731 100644 --- a/emsystem/src/main/java/com/emsystem/mapper/LoginMapper.java +++ b/emsystem/src/main/java/com/emsystem/mapper/LoginMapper.java @@ -12,4 +12,10 @@ public interface LoginMapper { LoginResVO getLoginByUsername(LoginVO vo); LoginResVO getLoginByEmail(LoginVO vo); + + void updatePassword(LoginVO code); + + void insertMessage(Integer id); + + Integer getEmpid(LoginVO vo); } diff --git a/emsystem/src/main/java/com/emsystem/service/EmailService.java b/emsystem/src/main/java/com/emsystem/service/EmailService.java new file mode 100644 index 0000000000000000000000000000000000000000..fe56fd56d9f2daee3dc6cd2f2cf9235029efe46c --- /dev/null +++ b/emsystem/src/main/java/com/emsystem/service/EmailService.java @@ -0,0 +1,9 @@ +package com.emsystem.service; + +import com.emsystem.vo.LoginVO; + +public interface EmailService { + public String sendVerificationCode(String to); + + public void updatePassword(LoginVO code); +} diff --git a/emsystem/src/main/java/com/emsystem/service/impl/EmailServiceImpl.java b/emsystem/src/main/java/com/emsystem/service/impl/EmailServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..7649c31e276cb977751ec01969df394478e708c1 --- /dev/null +++ b/emsystem/src/main/java/com/emsystem/service/impl/EmailServiceImpl.java @@ -0,0 +1,52 @@ +package com.emsystem.service.impl; + +import com.emsystem.mapper.LoginMapper; +import com.emsystem.service.EmailService; +import com.emsystem.vo.LoginVO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.stereotype.Service; + +import java.security.SecureRandom; + +@Service +public class EmailServiceImpl implements EmailService { + @Autowired + private JavaMailSender mailSender ; + @Autowired + private LoginMapper mapper; + public String sendVerificationCode(String to) { + String code = generateVerificationCode(); + SimpleMailMessage message = new SimpleMailMessage(); + message.setFrom("3245922940@qq.com"); // 设置发件人地址 + message.setTo(to); + message.setSubject("您的密码"); + message.setText("您的密码是: " + code); + mailSender.send(message); + return code; // 可以将验证码存储在内存中或数据库中以便后续验证 + } + + @Override + public void updatePassword(LoginVO vo) { + mapper.updatePassword(vo); + Integer id = mapper.getEmpid(vo); + mapper.insertMessage(id); + } + + private static final String UPPER_CASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + private static final String LOWER_CASE = UPPER_CASE.toLowerCase(); + private static final String DIGITS = "0123456789"; + private static final String ALL_CHARS = UPPER_CASE + LOWER_CASE + DIGITS; + private static final SecureRandom RANDOM = new SecureRandom(); + + public static String generateVerificationCode() { + StringBuilder sb = new StringBuilder(); + int length = 12; // 密码长度 + for (int i = 0; i < length; i++) { + int index = RANDOM.nextInt(ALL_CHARS.length()); + sb.append(ALL_CHARS.charAt(index)); + } + return sb.toString(); + } + } diff --git a/emsystem/src/main/resources/static/mapper/LoginMapper.xml b/emsystem/src/main/resources/static/mapper/LoginMapper.xml index bd1ca97f3a02c3145e01fd2926d8daeb923f0676..84db0696712ba861e579c826798cc37c25bd3dd5 100644 --- a/emsystem/src/main/resources/static/mapper/LoginMapper.xml +++ b/emsystem/src/main/resources/static/mapper/LoginMapper.xml @@ -15,7 +15,20 @@ FROM employee WHERE email = #{email} AND password = #{password} - + + UPDATE employee + SET password = #{password} + WHERE email = #{email} + + + INSERT INTO notification (`employee_id`, `message`, `type`, `status`) VALUES (#{id}, '修改密码', '修改密码', '未读') + + \ No newline at end of file