ITPub博客

首页 > 应用开发 > Java > (十五) 整合spring cloud云架构 - commonservice-sso服务搭建(一)

(十五) 整合spring cloud云架构 - commonservice-sso服务搭建(一)

原创 Java 作者:我祈美如画 时间:2020-11-09 11:14:30 0 删除 编辑

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

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

Xml代码   收藏代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < project   xmlns = "http://maven.apache.org/POM/4.0.0"   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  3.      xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >   
  4.      < modelVersion > 4.0.0 </ modelVersion >   
  5.       
  6.      < parent >   
  7.          < groupId > com.ml.honghu </ groupId >   
  8.          < artifactId > commonservice </ artifactId >   
  9.          < version > 0.0.1-SNAPSHOT </ version >   
  10.      </ parent >   
  11.   
  12.      < artifactId > commonservice-sso </ artifactId >   
  13.      < packaging > jar </ packaging >   
  14.   
  15.      < dependencies >   
  16.          < dependency >   
  17.              < groupId > org.springframework.cloud </ groupId >   
  18.              < artifactId > spring-cloud-starter-eureka </ artifactId >   
  19.          </ dependency >   
  20.          < dependency >   
  21.              < groupId > org.springframework.cloud </ groupId >   
  22.              < artifactId > spring-cloud-starter-config </ artifactId >   
  23.          </ dependency >   
  24.          < dependency >   
  25.              < groupId > org.springframework.boot </ groupId >   
  26.              < artifactId > spring-boot-starter-actuator </ artifactId >   
  27.          </ dependency >   
  28.          < dependency >   
  29.              < groupId > org.springframework.boot </ groupId >   
  30.              < artifactId > spring-boot-starter-data-rest </ artifactId >   
  31.          </ dependency >   
  32.          < dependency >   
  33.              < groupId > org.springframework.boot </ groupId >   
  34.              < artifactId > spring-boot-starter-web </ artifactId >   
  35.          </ dependency >   
  36.          < dependency >   
  37.              < groupId > org.springframework.boot </ groupId >   
  38.              < artifactId > spring-boot-starter-security </ artifactId >   
  39.          </ dependency >   
  40.   
  41.          < dependency >   
  42.              < groupId > org.springframework.security.oauth </ groupId >   
  43.              < artifactId > spring-security-oauth2 </ artifactId >   
  44.          </ dependency >   
  45.   
  46.          < dependency >   
  47.              < groupId > org.springframework.boot </ groupId >   
  48.              < artifactId > spring-boot-starter-test </ artifactId >   
  49.          </ dependency >   
  50.          < dependency >   
  51.              < groupId > org.springframework.hateoas </ groupId >   
  52.              < artifactId > spring-hateoas </ artifactId >   
  53.          </ dependency >   
  54.          < dependency >   
  55.              < groupId > org.springframework.boot </ groupId >   
  56.              < artifactId > spring-boot-starter-data-rest </ artifactId >   
  57.          </ dependency >   
  58.          < dependency >   
  59.              < groupId > com.ml.honghu.common.framework </ groupId >   
  60.              < artifactId > common-framework-dao </ artifactId >   
  61.              < version > 1.0.0-SNAPSHOT </ version >   
  62.          </ dependency >   
  63.          < dependency >   
  64.              < groupId > org.springframework.boot </ groupId >   
  65.              < artifactId > spring-boot-starter-web </ artifactId >   
  66.          </ dependency >   
  67.          < dependency >   
  68.              < groupId > org.springframework.boot </ groupId >   
  69.              < artifactId > spring-boot-starter-freemarker </ artifactId >   
  70.          </ dependency >   
  71.          < dependency >   
  72.              < groupId > com.ml.honghu </ groupId >   
  73.              < artifactId > component-base </ artifactId >   
  74.          </ dependency >   
  75.          </ dependency >   
  76.      </ dependencies >   
  77.   
  78.      <!-- 打包插件,其中repackage、true是专门打spring boot专用包 -->   
  79.      < build >   
  80.          < plugins >   
  81.              < plugin >   
  82.                  < groupId > org.springframework.boot </ groupId >   
  83.                  < artifactId > spring-boot-maven-plugin </ artifactId >   
  84.                  < executions >   
  85.                      < execution >   
  86.                          < id > 1 </ id >   
  87.                          < goals >   
  88.                              < goal > repackage </ goal >   
  89.                          </ goals >   
  90.                      </ execution >   
  91.                      < execution >   
  92.                          < id > 2 </ id >   
  93.                          < goals >   
  94.                              < goal > build-info </ goal >   
  95.                          </ goals >   
  96.                      </ execution >   
  97.                  </ executions >   
  98.              </ plugin >   
  99.          </ plugins >   
  100.      </ build >   
  101. </ project >   

 2. 配置bootstrap.yml文件

Xml代码   收藏代码
  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  

 3. 配置项目启动文件

Java代码   收藏代码
  1. package  com.ml.honghu;  
  2.   
  3. import  org.springframework.boot.SpringApplication;  
  4. import  org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import  org.springframework.cloud.netflix.eureka.EnableEurekaClient;  
  6.   
  7. @SpringBootApplication   
  8. @EnableEurekaClient   
  9. public   class  SSOApplication {  
  10.      public   static   void  main(String[] args) {  
  11.         SpringApplication.run(SSOApplication. class , args);  
  12.     }  
  13. }  

 4. 创建sso相关表:

oauth_access_token、oauth_approvals、

oauth_client_details、oauth_client_token、

oauth_code、oauth_refresh_token

 

脚本如下:

Java代码   收藏代码
  1. /* 
  2. Navicat MySQL Data Transfer 
  3.  
  4. Source Server         : localhost 
  5. Source Server Version : 50621 
  6. Source Host           : localhost:3306 
  7. Source Database       : honghu 
  8.  
  9. Target Server Type    : MYSQL 
  10. Target Server Version : 50621 
  11. File Encoding         : 65001 
  12.  
  13. Date: 2017-10-26 20:12:56 
  14. */   
  15.   
  16. SET FOREIGN_KEY_CHECKS= 0 ;  
  17.   
  18. -- ----------------------------  
  19. -- Table structure  for  `oauth_access_token`  
  20. -- ----------------------------  
  21. DROP TABLE IF EXISTS `oauth_access_token`;  
  22. CREATE TABLE `oauth_access_token` (  
  23.   `token_id` varchar( 256 ) DEFAULT NULL,  
  24.   `token` blob,  
  25.   `authentication_id` varchar( 128 ) NOT NULL,  
  26.   `user_name` varchar( 256 ) DEFAULT NULL,  
  27.   `client_id` varchar( 256 ) DEFAULT NULL,  
  28.   `authentication` blob,  
  29.   `refresh_token` varchar( 256 ) DEFAULT NULL,  
  30.   PRIMARY KEY (`authentication_id`)  
  31. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  32.   
  33.   
  34. -- ----------------------------  
  35. -- Table structure  for  `oauth_approvals`  
  36. -- ----------------------------  
  37. DROP TABLE IF EXISTS `oauth_approvals`;  
  38. CREATE TABLE `oauth_approvals` (  
  39.   `userId` varchar( 256 ) DEFAULT NULL,  
  40.   `clientId` varchar( 256 ) DEFAULT NULL,  
  41.   `scope` varchar( 256 ) DEFAULT NULL,  
  42.   `status` varchar( 10 ) DEFAULT NULL,  
  43.   `expiresAt` datetime DEFAULT NULL,  
  44.   `lastModifiedAt` datetime DEFAULT NULL  
  45. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  46.   
  47. -- ----------------------------  
  48. -- Records of oauth_approvals  
  49. -- ----------------------------  
  50.   
  51. -- ----------------------------  
  52. -- Table structure  for  `oauth_client_details`  
  53. -- ----------------------------  
  54. DROP TABLE IF EXISTS `oauth_client_details`;  
  55. CREATE TABLE `oauth_client_details` (  
  56.   `client_id` varchar( 128 ) NOT NULL,  
  57.   `resource_ids` varchar( 256 ) DEFAULT NULL,  
  58.   `client_secret` varchar( 256 ) DEFAULT NULL,  
  59.   `scope` varchar( 256 ) DEFAULT NULL,  
  60.   `authorized_grant_types` varchar( 256 ) DEFAULT NULL,  
  61.   `web_server_redirect_uri` varchar( 256 ) DEFAULT NULL,  
  62.   `authorities` varchar( 256 ) DEFAULT NULL,  
  63.   `access_token_validity`  int ( 11 ) DEFAULT NULL,  
  64.   `refresh_token_validity`  int ( 11 ) DEFAULT NULL,  
  65.   `additional_information` varchar( 4096 ) DEFAULT NULL,  
  66.   `autoapprove` varchar( 256 ) DEFAULT NULL,  
  67.   PRIMARY KEY (`client_id`)  
  68. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  69.   
  70.   
  71. -- ----------------------------  
  72. -- Table structure  for  `oauth_client_token`  
  73. -- ----------------------------  
  74. DROP TABLE IF EXISTS `oauth_client_token`;  
  75. CREATE TABLE `oauth_client_token` (  
  76.   `token_id` varchar( 256 ) DEFAULT NULL,  
  77.   `token` blob,  
  78.   `authentication_id` varchar( 128 ) NOT NULL,  
  79.   `user_name` varchar( 256 ) DEFAULT NULL,  
  80.   `client_id` varchar( 256 ) DEFAULT NULL,  
  81.   PRIMARY KEY (`authentication_id`)  
  82. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  83.   
  84. -- ----------------------------  
  85. -- Records of oauth_client_token  
  86. -- ----------------------------  
  87.   
  88. -- ----------------------------  
  89. -- Table structure  for  `oauth_code`  
  90. -- ----------------------------  
  91. DROP TABLE IF EXISTS `oauth_code`;  
  92. CREATE TABLE `oauth_code` (  
  93.   `code` varchar( 256 ) DEFAULT NULL,  
  94.   `authentication` blob  
  95. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  96.   
  97. -- ----------------------------  
  98. -- Records of oauth_code  
  99. -- ----------------------------  
  100.   
  101. -- ----------------------------  
  102. -- Table structure  for  `oauth_refresh_token`  
  103. -- ----------------------------  
  104. DROP TABLE IF EXISTS `oauth_refresh_token`;  
  105. CREATE TABLE `oauth_refresh_token` (  
  106.   `token_id` varchar( 256 ) DEFAULT NULL,  
  107.   `token` blob,  
  108.   `authentication` blob  
  109. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  

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

 

本节搭建先搭建那么多,后面的业务代码太多,我们会在后面的章节中放出来。

 

从现在开始,我这边会将近期研发的spring cloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,大家来一起探讨spring cloud架构的搭建过程及如何运用于企业项目。(企业架构源码可以加求球:叁五三陆二肆柒二伍玖)

 


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69984951/viewspace-2732938/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-10-05

  • 博文量
    33
  • 访问量
    16576