今日推荐:微服务架构:由浅入深带你了解底层注册中心
文章链接:https://cloud.tencent.com/developer/article/2465281
推荐理由:本文详细的介绍了微服务的整体架构和相应的基本概念,并结合实战代码演示来提供了对应的案例。
Java
实现<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>3.0.3</version>
</dependency>
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
@Configuration
public class CacheConfig {
@Bean
public Cache<String, String> resetTokenCache() {
return Caffeine.newBuilder()
.expireAfterWrite(15, TimeUnit.MINUTES)
.maximumSize(1000)
.build();
}
}
import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.mail.MessagingException;
import java.util.UUID;
@RestController
@RequestMapping("/api/password-reset")
public class PasswordResetController {
@Autowired
private Cache<String, String> resetTokenCache;
@Autowired
private EmailService emailService;
@PostMapping("/request")
public String requestPasswordReset(@RequestParam("email") String email) {
// 生成重置令牌
String token = UUID.randomUUID().toString();
// 将令牌存储到缓存中,key为email
resetTokenCache.put(email, token);
// 构建重置链接
String resetLink = "https://example.com/reset-password?token=" + token + "&email=" + email;
// 发送重置链接到用户邮箱
try {
emailService.sendEmail(email, "Password Reset Request",
"Click the following link to reset your password: " + resetLink);
} catch (MessagingException e) {
return "Failed to send email.";
}
return "Password reset link sent successfully.";
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
@Service
public class EmailService {
@Autowired
private JavaMailSender mailSender;
public void sendEmail(String to, String subject, String text) throws MessagingException {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(text);
mailSender.send(message);
}
}
import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/password-reset")
public class PasswordResetController {
@Autowired
private Cache<String, String> resetTokenCache;
@Autowired
private UserService userService; // 假设有用户服务可以处理密码更新
@PostMapping("/reset")
public String resetPassword(@RequestParam("token") String token,
@RequestParam("email") String email,
@RequestParam("newPassword") String newPassword) {
// 从缓存中获取令牌
String cachedToken = resetTokenCache.getIfPresent(email);
// 验证令牌有效性
if (cachedToken == null || !cachedToken.equals(token)) {
return "The reset link is invalid or has expired. Please request a new one.";
}
// 更新用户密码
userService.updatePassword(email, newPassword);
// 清除缓存中的令牌
resetTokenCache.invalidate(email);
return "Password has been reset successfully.";
}
}
import org.springframework.stereotype.Service;
@Service
public class UserService {
// 示例方法,实际应与数据库操作关联
public void updatePassword(String email, String newPassword) {
// 在此处更新数据库中的密码哈希
}
}
Java
实现了一个通过邮箱发送重置链接重置密码的简单功能,上述方式还有一个简单变种是仅发送验证码到邮箱,通过前端输入验证码请求后端验证,验证通过后生成令牌返回前端,最后输入新密码请求重置链接进行重置密码。👋 你好,我是 Lorin 洛林,一位 Java 后端技术开发者!座右铭:Technology has the power to make the world a better place.
🚀 我对技术的热情是我不断学习和分享的动力。我的博客是一个关于Java生态系统、后端开发和最新技术趋势的地方。
🧠 作为一个 Java 后端技术爱好者,我不仅热衷于探索语言的新特性和技术的深度,还热衷于分享我的见解和最佳实践。我相信知识的分享和社区合作可以帮助我们共同成长。
💡 在我的博客上,你将找到关于Java核心概念、JVM 底层技术、常用框架如Spring和Mybatis 、MySQL等数据库管理、RabbitMQ、Rocketmq等消息中间件、性能优化等内容的深入文章。我也将分享一些编程技巧和解决问题的方法,以帮助你更好地掌握Java编程。
🌐 我鼓励互动和建立社区,因此请留下你的问题、建议或主题请求,让我知道你感兴趣的内容。此外,我将分享最新的互联网和技术资讯,以确保你与技术世界的最新发展保持联系。我期待与你一起在技术之路上前进,一起探讨技术世界的无限可能性。
📖 保持关注我的博客,让我们共同追求技术卓越。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。