Java教程

Day421.认证服务 -谷粒商城

本文主要是介绍Day421.认证服务 -谷粒商城,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

认证服务

一、初始化

  • 创建认证模块

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2X13xOiM-1634396228713)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211016211306992.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oIWVYnkX-1634396228715)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211016211424288.png)]

  • 统一springboot版本2.2.1.RELEASE,并引入Common服务依赖,因为不操作数据库,所以排除mybaitsplus依赖
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<dependency>
    <groupId>com.achang.achangmall</groupId>
    <artifactId>achangmall-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <exclusions>
        <exclusion>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </exclusion>
    </exclusions>
</dependency>
  • application.properties
spring.application.name=achang-auth-server
spring.cloud.nacos.server-addr=127.0.0.1:8848
server.port=20000
spring.thymeleaf.cache=false
  • com.achang.achangmall.auth.AchangAuthServerApplication
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class AchangAuthServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(AchangAuthServerApplication.class, args);
    }
}
  • 启动服务

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dlA3Cwnw-1634396228717)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211016212619429.png)]

  • 发现服务注册进 Nacos

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EqYAPVS9-1634396228720)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211016212640220.png)]

  • 拉入登录页面,将资料高级篇登录页面和注册页面放到 templates 下,并改名为login、reg.html
  • 为了测试直接访问登录页,把login.html改名为index.html

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vbmpRH2k-1634396228721)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211016220636486.png)]

  • C:\Windows\System32\drivers\etc\hosts,添加本地域名映射
192.168.109.101 auth.achangmall.com

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bzacnjcQ-1634396228726)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211016214303159.png)]

  • 静态文件可以选择 Nginx 动静分离配置

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-11tfs3Qd-1634396228729)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211016215151482.png)]

  • 修改reg.html、login.html里面的路径引用

  • 添加网关转发配置,achangmall-gateway/src/main/resources/application.yml

        - id: auth_route
          uri: lb://achang-auth-server
          predicates:
            - Host=auth.achangmall.com
  • 启动网关服务AchangmallGatewayApplication +AchangAuthServerApplication 测试转发效果

访问http://auth.achangmall.com/,访问成功!!!

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-95pmxVvH-1634396228731)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211016220650202.png)]

  • 修改product服务的注册和登录的uri

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8KdRQaHy-1634396228733)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211016222147788.png)]

  • com.achang.achangmall.auth.controller.LoginController
@Controller
public class LoginController {
    @GetMapping("/login.html")
    public String loginPage(){
        return "login";
    }

    @GetMapping("/reg.html")
    public String register(){
        return "reg";
    }
}
  • achang-auth-server/src/main/resources/templates/login.html
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QynKa696-1634396228735)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211016222717413.png)]

  • achang-auth-server/src/main/resources/templates/reg.html

在这里插入图片描述


二、短信验证码

  • 前端验证码代码
<a id="sendCode">发送验证码</a>
$(function (){
    $("#sendCode").click(function (){
        if ($(this).hasClass("disabled")){
            //todo 发送手机验证码业务
        }{
            timeoutChangeSytle()
        }
    });
})
var num = 60;
function timeoutChangeSytle(){
    $("#sendCode").attr("class","disabled")
    if (num==0){
        $("#sendCode").text("发送验证码")
        num = 60;
        $("#sendCode").attr("class","")
    }else {
        var str = num+"后再次发送"
        $("#sendCode").text(str)
        setTimeout("timeoutChangeSytle()",1000)
    }
    num--;
}
  • 直接通过mvc做视图映射

com.achang.achangmall.auth.conf.AchangWebConfig

@Configuration
public class AchangWebConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login.html").setViewName("login");
        registry.addViewController("/reg.html").setViewName("reg");
    }
}

  • 明天继续!!!
这篇关于Day421.认证服务 -谷粒商城的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!