Java案例-用户注册邮箱绑定激活功能实现
<–start–>
需求描述:当客户打开收到邮箱激活码的邮件,点击激活链接,正确填写激活码后就会完成邮箱激活的步骤。
在后台编程代码编写中,有以下几个要点:
① 接收客户的手机号码和邮箱激活码。
② 先判断激活码是否有效。如果激活码无效,提示用户。
③ 如果激活码有效,接下来就要判断用户是否在重复绑定邮箱,customer表中
的type字段就是用来甄别邮箱是否已经激活,默认该字段的值是空值,如果type字段的值为1表示用户已经绑定邮箱。
④ 如果用户没有绑定过邮箱,就完成邮箱的绑定。
编写CustomerAction类,提供activeMail方法:
① 使用属性驱动接收激活码。
② 判断激活码是否有效。因为先前已经将邮箱激活码存入了redis中,所以我们
可以直接从redis中获取激活码,判断用户提交的激活码是否为空或者与redis中存储的不同,这两种情况均表示激活码无效。
③ 解决响应到客户端的中文乱码的问题。
④ 激活码有效的话,为了避免用户重复点击激活地址导致重复绑定情形发生,
就需要通过webservice查询crm系统中的客户信息,判断是否已经绑定。
⑤ 对于已经激活邮箱的用户,在redis中删除邮箱激活码。
完整的CustomerAction代码:
@Action("customer_activeMail") public String activeMail() throws IOException { ServletActionContext.getResponse().setContentType("text/html;charset=utf-8"); // 判断激活码是否有效 String activecodeRedis = redisTemplate.opsForValue().get(model.getTelephone()); if (activecodeRedis == null || !activecodeRedis.equals(activecodeRedis)) {// 激活码无效ServletActionContext.getResponse().getWriter().println("激活码无效,请登录系统,重新绑定邮箱!"); } else {// 激活码有效// 防止重复绑定// 调用CRM webService 查询客户信息,判断是否已经绑定Customer customer = WebClient.create("http://localhost:9002/crm_management/services"+ "/customerService/customer/telephone/"+ model.getTelephone()).accept(MediaType.APPLICATION_JSON).get(Customer.class);if (customer.getType() == null || customer.getType() != 1) {// 没有绑定,进行绑定WebClient.create("http://localhost:9002/crm_management/services"+ "/customerService/customer/updatetype/"+ model.getTelephone()).get();ServletActionContext.getResponse().getWriter().println("邮箱绑定成功!");} else {// 已经绑定过ServletActionContext.getResponse().getWriter().println("邮箱已经绑定过,无需重复绑定!");}// 删除redis的激活码redisTemplate.delete(model.getTelephone()); } return NONE; }在crm_management系统中,编写webservice服务接口findByTelephone,通过手机号码来查询客户信息。
@Path("/customer/telephone/{telephone}") @GET @Consumes({ "application/xml", "application/json" }) public Customer findByTelephone(@PathParam("telephone") String telephone);编写updateType服务接口,当激活码有效时,就修改customer表中的type字段的值为1。
@Path("/customer/updatetype/{telephone}") @GET public void updateType(@PathParam("telephone") String telephone);在实现类CustomerServiceImpl中实现findByTelephone和updateType这两个方法。
@Override public Customer findByTelephone(String telephone) {return customerRepository.findByTelephone(telephone); }@Override public void updateType(String telephone) {customerRepository.updateType(telephone); }在CustomerRepository的dao中编写方法,运用spring data jpa完成持久层的操作。
public Customer findByTelephone(String telephone);@Query("update Customer set type=1 where telephone= ?") @Modifying public void updateType(String telephone);完整的CustomerService服务接口代码:
public interface CustomerService {@Path("/customer/telephone/{telephone}") @GET @Consumes({ "application/xml", "application/json" }) public Customer findByTelephone(@PathParam("telephone") String telephone);@Path("/customer/updatetype/{telephone}") @GET public void updateType(@PathParam("telephone") String telephone);}完整的CustomerServiceImpl实现类代码:
@Service @Transactional public class CustomerServiceImpl implements CustomerService {// 注入DAO @Autowired private CustomerRepository customerRepository;@Override public Customer findByTelephone(String telephone) {return customerRepository.findByTelephone(telephone); }@Override public void updateType(String telephone) {customerRepository.updateType(telephone); }}完整的CustomerRepository持久层代码:
public interface CustomerRepository extends JpaRepository<Customer, Integer> {public Customer findByTelephone(String telephone);@Query("update Customer set type=1 where telephone= ?") @Modifying public void updateType(String telephone);}<–end–>
与50位技术专家面对面20年技术见证,附赠技术全景图总结
以上是生活随笔为你收集整理的Java案例-用户注册邮箱绑定激活功能实现的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Java案例-用户注册邮箱验证将邮箱激活
- 下一篇: SSH框架整合实现Java三层架构实例(