譯見 | 構建用戶管理微服務(終章):合而爲一

譯見

《譯見》系列「構建用戶管理微服務」已經向你們連載了六期,咱們從零起步已經成功開發出用戶管理應用程序的構建模塊。在最後一部分,將向你們展現如何將以前的所學合而爲一,來讓應用程序正常地運行下去。java

往期內容大合集


建立一個獨立的可執行模塊

毫無疑問的,創建 Spring-based 應用程序最簡單的方法即是使用 Spring Boot。由於使用它比在原生 Spring 使用起來更加有效, 因此它被大量的採用 。我曾在各類狀況下使用 Spring , 並在 servlet 容器和徹底成熟的 Java EE 應用程序服務器上構建應用程序, 但可以將全部內容打包在可執行捆綁包中能極大地下降開發成本。mysql

總而言之,第一步是爲應用程序建立一個新的模塊,即爲 springuni-auth-bootgit

Maven 配置github

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <parent>
    <artifactId>springuni-particles</artifactId>
    <groupId>com.springuni</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>springuni-auth-boot</artifactId>

  <name>SpringUni Auth User Boot</name>
  <description>Example module for assembling user authentication modules</description>

  <dependencies>
    <dependency>
      <groupId>com.springuni</groupId>
      <artifactId>springuni-auth-rest</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

    <dependency>
      <groupId>com.springuni</groupId>
      <artifactId>springuni-auth-user-jpa</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <!-- https://github.com/spring-projects/spring-boot/issues/6254#issuecomment-229600830 -->
        <configuration>
          <classifier>exec</classifier>
        </configuration>
      </plugin>
    </plugins>
  </build></project>複製代碼

Springuni-auth-rest 提供用於用戶管理的 REST 端點,它還將 springuni-auth-model 做爲傳遞依賴。springuni-auth-user-jpa 負責持久化用戶數據,並在以後將替換其餘持久性機制。web

第三個依賴是 MySQL 鏈接器,且它能夠根據需求進行替換。spring

Spring Boot 的角度來講,一下兩個依賴關係是很是重要的:spring-boot-starter-webspring-boot-starter-tomcat 。咱們須要用它們來建立 Web 應用程序。sql

應用程序的接入口apache

import com.springuni.auth.domain.model.AuthJpaRepositoryConfiguration;import com.springuni.auth.domain.service.AuthServiceConfiguration;import com.springuni.auth.rest.AuthRestConfiguration;import com.springuni.auth.security.AuthSecurityConfiguration;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;

@SpringBootApplication
@Configuration
@Import({
    AuthJpaRepositoryConfiguration.class,
    AuthServiceConfiguration.class,
    AuthRestConfiguration.class,
    AuthSecurityConfiguration.class})public class Application {  public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
  }

}複製代碼

在沒有 Spring Boot 的狀況下執行一下步驟會很是吃力(必須在web.xml中註冊上下文監聽器且爲應用程序設置容器)json

這幾乎是一個虛擬模塊且全部重要的舉措都將歸結爲必須導入一些 Java-based Spring 配置類。tomcat

啓動

Spring Boot 附帶了一個很是有用的 Maven 插件,它能夠將整個項目從新打包成一個能夠進行執行的 über JAR。它一樣也能夠在本地啓動項目。

mvn -pl springuni-auth-boot spring-boot:run複製代碼

測試驅動用戶管理應用程序

第一部分定義了全部可用的 REST 端點,如今用一些用例來對他們進行測試。

註冊新用戶

curl -H 'Content-Type: application/json' -XPOST http://localhost:5000/users -d \
'{ "screenName":"test2", "contactData": { "email": "test2@springuni.com" }, "password": "test" }'
HTTP/1.1 200複製代碼

首次登錄嘗試

此時進行首次登錄嘗試會失敗,由於未確認用戶帳號。

curl -D- -XPOST http://localhost:5000/auth/login -d '{ "username":"test5", "password": "test" }' 
HTTP/1.1 401 
{
 "statusCode" : 401,
 "reasonPhrase" : "Unauthorized"
}複製代碼

確認帳號

通常狀況下,用戶最終會收到一封附帶確認連接的電子電子郵件,點擊連接會啓動以下的請求。

curl -D- -XPUT http://localhost:5000/users/620366184447377/77fc990b-210c-4132-ac93-ec50522ba06f
HTTP/1.1 200複製代碼

第二次登錄嘗試

用戶的電子郵箱地址確認後,便可登陸。

curl -D- -XPOST http://localhost:5000/auth/login -d '{ "username":"test5", "password": "test" }'
HTTP/1.1 200
X-Set-Authorization-Bearer: eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiI2MjA1OTkwNjIwMTQ4ODEiLCJzdWIiOiI2MjAzNjYxODQ0NDczNzciLCJleHAiOjE0OTcxMDQ3OTAsImlhdCI6MTQ5NzAxODM5MCwiYXV0aG9yaXRpZXMiOiIifQ.U-GfabsdYidg-Y9eSp2lyyh7DxxaI-zaTOZISlCf3RjKQUTmu0-vm6DH80xYWE69SmoGgm07qiYM32JBd9d5oQ複製代碼

下一步是什麼?

正如我以前提到的,這個應用程序有不少工做要作。其中還有一些基本功能,也沒有UI。您能夠按照如下步驟進行: github.com/springuni/s…

原文連接:www.springuni.com/user-manage…


往期內容大合集

相關文章
相關標籤/搜索