微服务从零开始之登录与注册二

书接上文 微服务从零开始之登录与注册一, 表现层及前端代码搞定, 现在开始来设计和实现后端的 Java Web Service 代码

按照惯例, 基于 Spring boot 用 maven 来构造项目, pom.xml 如下



    4.0.0

    com.github.walterfan
    checklist
    0.0.1-SNAPSHOT
    jar

    demo
    Checklist 

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.1.RELEASE
    

    
        UTF-8
        UTF-8
        1.8
    

    
    

        
            org.freemarker
            freemarker
            2.3.19
        

        
            org.springframework
            spring-context-support
            3.2.4.RELEASE
        

        
            org.apache.commons
            commons-lang3
            3.5
        


        
            commons-beanutils
            commons-beanutils
            1.9.3
        

        
            org.springframework.boot
            spring-boot-starter-data-rest
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
            org.springframework.boot
            spring-boot-starter-mail
        

        
            com.h2database
            h2
            runtime
        

        
            org.springframework.boot
            spring-boot-devtools
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            javax.inject
            javax.inject
            1
            test
        
        
            org.testng
            testng
            6.9.10
            test
        


        
            mysql
            mysql-connector-java
            5.1.41
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    




入口程序基于 SpringBoot , 就几行代码, 看起来非常简单, 其实隐藏了许多细节

package com.github.walterfan.checklist;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebApplication  {

    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class, args);
    }
}

Spring Boot 一改Java Web 项目之前琐碎的配置和大量不必要的配置和胶水代码, 代之以非常简洁的实现方式, 极大地解放了 Java 程序员的生产力.

当然, 在自动配置, 封装复杂性的同时, 也增加了错误排查的难度, 你必须了解它背后隐藏的许多细节, 知其然并知其所以然, 自动化按约定配置搭建, 你得知道约定是什么.

详情参见 Spring Boot 的文档, spring boot 的 spring-boot-starter 其实背后还是粘合了众多 Spring 的成熟子项目 spring-core, spring-tx, spring-context, spring-aop, spring-web, spring-data 等等

其中有一个 spring-boot-starter-actuator 很有意思, 对监控和诊断 web service 很有帮助

还有比如以上我们引入了 spring-boot-starter-data-rest , 其实它又包含了如下依赖


        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.fasterxml.jackson.core
            jackson-annotations
        
        
            com.fasterxml.jackson.core
            jackson-databind
        
        
            org.springframework.data
            spring-data-rest-webmvc
        
    

参考它的 pom.xml

示例代码参见 https://github.com/walterfan/msa/tree/master/examples/checklist

调用Restful API 将注册请求写入数据库,并发送一封确认的邮件到用户注册的邮箱

以下五个类实现注册基本的基本功能

class ChecklistController

应用入口很简单, 将请求分派到相应的 web 页面

package com.github.walterfan.checklist.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by walterfan on 30/1/2016.
 */
@Controller
public class ChecklistController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView home() {
        return new ModelAndView("index");
    }

    @RequestMapping(value = "/admin", method = RequestMethod.GET)
    public ModelAndView adminForm() {
        return new ModelAndView("admin");
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView loginForm() {
        return new ModelAndView("login");
    }

    @RequestMapping(value = "/about", method = RequestMethod.GET)
    public ModelAndView about() {
        return new ModelAndView("about");
    }
}

REST API 的处理也很简单, 存储注册请求并发送一封确认激活的邮件给注册者

class Registration

应用了 Hibernate validator 的一些注解作为输入校验

package com.github.walterfan.checklist.dto;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;

import javax.validation.constraints.Size;

public class Registration {
    @NotBlank
    private String username;
    @Size(min = 6, max = 32)
    private String password;
    @Size(min = 6, max = 32)
    private String passwordConfirmation;
    @Email
    private String email;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPasswordConfirmation() {
        return passwordConfirmation;
    }

    public void setPasswordConfirmation(String passwordConfirmation) {
        this.passwordConfirmation = passwordConfirmation;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


}

Class UserController

package com.github.walterfan.checklist.controller;

import com.github.walterfan.checklist.domain.User;
import com.github.walterfan.checklist.dto.Activation;
import com.github.walterfan.checklist.dto.Registration;
import com.github.walterfan.checklist.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by walterfan on 4/2/2017.
 */
@RestController
@RequestMapping("/checklist/api/v1/users")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value = "register", method = RequestMethod.POST)
    public ModelAndView register(@Valid @RequestBody Registration registration) {
        logger.info("register: " + registration.toString());
        userService.register(registration);

        return new ModelAndView("index");
    }

    @RequestMapping(value = "activate", method = RequestMethod.POST)
    public ModelAndView activate(@Valid @RequestBody Activation activation) {
        logger.info("activate: " + activation.toString());
        userService.activate(activation);

        return new ModelAndView("index");
    }


    @AuthorizationRole({ "admin" })
    @RequestMapping(method = RequestMethod.GET)
    public List getUsers() {
        logger.info("-----getUsers------");
        List list = userService.getUsers();
        list.stream().forEach(x ->  logger.info(x.toString()));
        return list;
    }
}

使用 JPA 框架和相关注解, 代码寥寥数行就搞定了DAO 层

class User

package com.github.walterfan.checklist.domain;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.Date;
import java.util.List;

/**
 * Created by walterfan on 7/2/2016.
 */
@Entity
@Table(name = "user")
public class User extends BaseObject {
    @Id
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid2")
    private String id;


    private String username;

    private String password;

    private String email;


    private String phoneNumber;


    private UserStatus status;


    private Date createTime;


    private Date lastModifiedTime;

    @OneToMany(cascade= CascadeType.ALL)
    private List tokens;


    public User() {
        this.status = UserStatus.pending;
        this.createTime = new Date(System.currentTimeMillis());
        this.lastModifiedTime = new Date(System.currentTimeMillis());
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public UserStatus getStatus() {
        return status;
    }

    public void setStatus(UserStatus status) {
        this.status = status;
    }

    public List getTokens() {
        return tokens;
    }

    public void setTokens(List tokens) {
        this.tokens = tokens;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getLastModifiedTime() {
        return lastModifiedTime;
    }

    public void setLastModifiedTime(Date lastModifiedTime) {
        this.lastModifiedTime = lastModifiedTime;
    }
}

class UserRepository

package com.github.walterfan.checklist.dao;

import com.github.walterfan.checklist.domain.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.Optional;

/**
 * Created by walterfan on 7/2/2016.
 */
@RepositoryRestResource
public interface UserRepository extends CrudRepository {

    Optional findByEmail(String email);
}


基本功能已经有了, 仅仅是把注册服务写入了数据库, 接下来要发送确认邮件, 完成注册, 及微服务的登录以及会话 Session 如何进行管理, 请参见微服务从零开始之登录与注册三

你可能感兴趣的:(微服务从零开始之登录与注册二)