一、 环境搭建

使用spring的初始化向导,创建出认证服务模块:
image.png
刚开始的pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.8.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.atguigu.gulimall</groupId>
  12. <artifactId>gulimall-auth-server</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>gulimall-auth-server</name>
  15. <description>认证中心(以及社交登录Oauth2.0、单点登录)</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>com.atguigu.gulimall</groupId>
  23. <artifactId>gulimall-common</artifactId>
  24. <version>0.0.1-SNAPSHOT</version>
  25. <exclusions>
  26. <exclusion>
  27. <groupId>com.baomidou</groupId>
  28. <artifactId>mybatis-plus-boot-starter</artifactId>
  29. </exclusion>
  30. </exclusions>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-web</artifactId>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.cloud</groupId>
  42. <artifactId>spring-cloud-starter-openfeign</artifactId>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-devtools</artifactId>
  47. <scope>runtime</scope>
  48. <optional>true</optional>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.projectlombok</groupId>
  52. <artifactId>lombok</artifactId>
  53. <optional>true</optional>
  54. </dependency>
  55. <dependency>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-starter-test</artifactId>
  58. <scope>test</scope>
  59. </dependency>
  60. </dependencies>
  61. <dependencyManagement>
  62. <dependencies>
  63. <dependency>
  64. <groupId>org.springframework.cloud</groupId>
  65. <artifactId>spring-cloud-dependencies</artifactId>
  66. <version>${spring-cloud.version}</version>
  67. <type>pom</type>
  68. <scope>import</scope>
  69. </dependency>
  70. </dependencies>
  71. </dependencyManagement>
  72. <build>
  73. <plugins>
  74. <plugin>
  75. <groupId>org.springframework.boot</groupId>
  76. <artifactId>spring-boot-maven-plugin</artifactId>
  77. <configuration>
  78. <excludes>
  79. <exclude>
  80. <groupId>org.projectlombok</groupId>
  81. <artifactId>lombok</artifactId>
  82. </exclude>
  83. </excludes>
  84. </configuration>
  85. </plugin>
  86. </plugins>
  87. </build>
  88. </project>

把认证模块加到nacos注册中心中:
application.properties:

spring.application.name=gulimall-auth-server
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
server.port=20000

启动类:

package com.atguigu.gulimall.auth;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class GulimallAuthServerApplication {

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

}

至此,认证中心模块已创建,整个项目的所有登录、注册、认证服务全部在这里进行;

二 短信验证码

发送验证码

使用阿里云短信服务,发送验证码;
参考gulimall-third-party下 SmsComponent 及SmsSendController

验证码防刷校验

对短信验证码,要实现接口防刷,防止恶意不断的发送短信验证码;以及对验证码的有效时间进行设置,,以及防止同一个手机号在固定时间内再次发送验证码;
参考gulimall-third-party下 SmsComponent 及SmsSendController

密码加密之MD5、盐值、BCrypt

密码字段应不可逆加密。
这里我们使用MD5盐值加密;
MD5本身是个信息摘要算法,是可以被逆向破解的,所以MD5不能直接用来进行密码的加密存储;加上“盐值”,使之不可逆;