Spring Cloud云架构 - commonservice-sso服务搭建

前面几篇我们已经介绍了Spring Cloud和oauth2的知识点,今天我们要利用Spring Cloud和oauth2进行commonservice-sso服务搭建,本节我们只是搭建commonservice-sso的基础平台,闲话少说,直接将步...

前面几篇我们已经介绍了Spring Cloud和oauth2的知识点,今天我们要利用Spring Cloud和oauth2进行commonservice-sso服务搭建,本节我们只是搭建commonservice-sso的基础平台,闲话少说,直接将步骤记录下来:

1. 创建maven项目commonservice-sso,其中pom.xml文件配置如下:

<?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>  

        <groupId>com.ml.honghu</groupId>  

        <artifactId>commonservice</artifactId>  

        <version>0.0.1-SNAPSHOT</version>  

    </parent>  

  

    <artifactId>commonservice-sso</artifactId>  

    <packaging>jar</packaging>  

  

    <dependencies>  

        <dependency>  

            <groupId>org.springframework.cloud</groupId>  

            <artifactId>spring-cloud-starter-eureka</artifactId>  

        </dependency>  

        <dependency>  

            <groupId>org.springframework.cloud</groupId>  

            <artifactId>spring-cloud-starter-config</artifactId>  

        </dependency>  

        <dependency>  

            <groupId>org.springframework.boot</groupId>  

            <artifactId>spring-boot-starter-actuator</artifactId>  

        </dependency>  

        <dependency>  

            <groupId>org.springframework.boot</groupId>  

            <artifactId>spring-boot-starter-data-rest</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-security</artifactId>  

        </dependency>  

  

        <dependency>  

            <groupId>org.springframework.security.oauth</groupId>  

            <artifactId>spring-security-oauth2</artifactId>  

        </dependency>  

  

        <dependency>  

            <groupId>org.springframework.boot</groupId>  

            <artifactId>spring-boot-starter-test</artifactId>  

        </dependency>  

        <dependency>  

            <groupId>org.springframework.hateoas</groupId>  

            <artifactId>spring-hateoas</artifactId>  

        </dependency>  

        <dependency>  

            <groupId>org.springframework.boot</groupId>  

            <artifactId>spring-boot-starter-data-rest</artifactId>  

        </dependency>  

        <dependency>  

            <groupId>com.ml.honghu.common.framework</groupId>  

            <artifactId>common-framework-dao</artifactId>  

            <version>1.0.0-SNAPSHOT</version>  

        </dependency>  

        <dependency>  

            <groupId>org.springframework.boot</groupId>  

            <artifactId>spring-boot-starter-web</artifactId>  

        </dependency>  

        <dependency>  

            <groupId>org.springframework.boot</groupId>  

            <artifactId>spring-boot-starter-freemarker</artifactId>  

        </dependency>  

        <dependency>  

            <groupId>com.ml.honghu</groupId>  

            <artifactId>component-base</artifactId>  

        </dependency>  

        </dependency>  

    </dependencies>  

  

    <!-- 打包插件,其中repackage、true是专门打spring boot专用包 -->  

    <build>  

        <plugins>  

            <plugin>  

                <groupId>org.springframework.boot</groupId>  

                <artifactId>spring-boot-maven-plugin</artifactId>  

                <executions>  

                    <execution>  

                        <id>1</id>  

                        <goals>  

                            <goal>repackage</goal>  

                        </goals>  

                    </execution>  

                    <execution>  

                        <id>2</id>  

                        <goals>  

                            <goal>build-info</goal>  

                        </goals>  

                    </execution>  

                </executions>  

            </plugin>  

        </plugins>  

    </build>  

</project>  


2. 配置bootstrap.yml文件

  1. spring:    

  2.   application:    

  3.     name: commonservice-sso    

  4.   profiles:     

  5.     active: dev,discoveryClient    

  6.   cloud:    

  7.     config:    

  8.       discovery:     

  9.         enabled: true    

  10.         service-id: commonservice-config-server    

  11. eureka:     

  12.   client:    

  13.     service-url:    

  14.       defaultZone: http://honghu:123456@localhost:8761/eureka    

  15.   instance:    

  16.     prefer-ip-address: true   

  17. 3. 配置项目启动文件

  18. package com.ml.honghu;  

  19.   

  20. import org.springframework.boot.SpringApplication;  

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

  22. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;  

  23.   

  24. @SpringBootApplication  

  25. @EnableEurekaClient  

  26. public class SSOApplication {  

  27.     public static void main(String[] args) {  

  28.         SpringApplication.run(SSOApplication.class, args);  

  29.     }  

  30. }  

  31. 4. 创建sso相关表:

  32. oauth_access_token、oauth_approvals、

  33. oauth_client_details、oauth_client_token、

  34. oauth_code、oauth_refresh_token

  35. /* 

  36. Navicat MySQL Data Transfer 

  37.  

  38. Source Server         : localhost 

  39. Source Server Version : 50621 

  40. Source Host           : localhost:3306 

  41. Source Database       : honghu 

  42.  

  43. Target Server Type    : MYSQL 

  44. Target Server Version : 50621 

  45. File Encoding         : 65001 

  46.  

  47. Date: 2017-10-26 20:12:56 

  48. */  

  49.   

  50. SET FOREIGN_KEY_CHECKS=0;  

  51.   

  52. -- ----------------------------  

  53. -- Table structure for `oauth_access_token`  

  54. -- ----------------------------  

  55. DROP TABLE IF EXISTS `oauth_access_token`;  

  56. CREATE TABLE `oauth_access_token` (  

  57.   `token_id` varchar(256) DEFAULT NULL,  

  58.   `token` blob,  

  59.   `authentication_id` varchar(128) NOT NULL,  

  60.   `user_name` varchar(256) DEFAULT NULL,  

  61.   `client_id` varchar(256) DEFAULT NULL,  

  62.   `authentication` blob,  

  63.   `refresh_token` varchar(256) DEFAULT NULL,  

  64.   PRIMARY KEY (`authentication_id`)  

  65. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  

  66.   

  67.   

  68. -- ----------------------------  

  69. -- Table structure for `oauth_approvals`  

  70. -- ----------------------------  

  71. DROP TABLE IF EXISTS `oauth_approvals`;  

  72. CREATE TABLE `oauth_approvals` (  

  73.   `userId` varchar(256) DEFAULT NULL,  

  74.   `clientId` varchar(256) DEFAULT NULL,  

  75.   `scope` varchar(256) DEFAULT NULL,  

  76.   `status` varchar(10) DEFAULT NULL,  

  77.   `expiresAt` datetime DEFAULT NULL,  

  78.   `lastModifiedAt` datetime DEFAULT NULL  

  79. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  

  80.   

  81. -- ----------------------------  

  82. -- Records of oauth_approvals  

  83. -- ----------------------------  

  84.   

  85. -- ----------------------------  

  86. -- Table structure for `oauth_client_details`  

  87. -- ----------------------------  

  88. DROP TABLE IF EXISTS `oauth_client_details`;  

  89. CREATE TABLE `oauth_client_details` (  

  90.   `client_id` varchar(128) NOT NULL,  

  91.   `resource_ids` varchar(256) DEFAULT NULL,  

  92.   `client_secret` varchar(256) DEFAULT NULL,  

  93.   `scope` varchar(256) DEFAULT NULL,  

  94.   `authorized_grant_types` varchar(256) DEFAULT NULL,  

  95.   `web_server_redirect_uri` varchar(256) DEFAULT NULL,  

  96.   `authorities` varchar(256) DEFAULT NULL,  

  97.   `access_token_validity` int(11) DEFAULT NULL,  

  98.   `refresh_token_validity` int(11) DEFAULT NULL,  

  99.   `additional_information` varchar(4096) DEFAULT NULL,  

  100.   `autoapprove` varchar(256) DEFAULT NULL,  

  101.   PRIMARY KEY (`client_id`)  

  102. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  

  103.   

  104.   

  105. -- ----------------------------  

  106. -- Table structure for `oauth_client_token`  

  107. -- ----------------------------  

  108. DROP TABLE IF EXISTS `oauth_client_token`;  

  109. CREATE TABLE `oauth_client_token` (  

  110.   `token_id` varchar(256) DEFAULT NULL,  

  111.   `token` blob,  

  112.   `authentication_id` varchar(128) NOT NULL,  

  113.   `user_name` varchar(256) DEFAULT NULL,  

  114.   `client_id` varchar(256) DEFAULT NULL,  

  115.   PRIMARY KEY (`authentication_id`)  

  116. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  

  117.   

  118. -- ----------------------------  

  119. -- Records of oauth_client_token  

  120. -- ----------------------------  

  121.   

  122. -- ----------------------------  

  123. -- Table structure for `oauth_code`  

  124. -- ----------------------------  

  125. DROP TABLE IF EXISTS `oauth_code`;  

  126. CREATE TABLE `oauth_code` (  

  127.   `code` varchar(256) DEFAULT NULL,  

  128.   `authentication` blob  

  129. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  

  130.   

  131. -- ----------------------------  

  132. -- Records of oauth_code  

  133. -- ----------------------------  

  134.   

  135. -- ----------------------------  

  136. -- Table structure for `oauth_refresh_token`  

  137. -- ----------------------------  

  138. DROP TABLE IF EXISTS `oauth_refresh_token`;  

  139. CREATE TABLE `oauth_refresh_token` (  

  140.   `token_id` varchar(256) DEFAULT NULL,  

  141.   `token` blob,  

  142.   `authentication` blob  

  143. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  

  144. 备注: oauth的相关表是用来存储用户的token信息和认证信息的。

  145. 从现在开始,我这边会将近期研发的spring cloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,大家来一起探讨spring cloud架构的搭建过程及如何运用于企业项目


  • 发表于 2018-06-25 17:07
  • 阅读 ( 1108 )
  • 分类:前端

1 条评论

请先 登录 后评论
不写代码的码农
IT 小兵

355 篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除