认证服务
一、初始化
- 创建认证模块
- 统一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);
}
}
- 启动服务
- 发现服务注册进 Nacos
- 拉入登录页面,将资料高级篇登录页面和注册页面放到 templates 下,并改名为login、reg.html
- 为了测试直接访问登录页,把login.html改名为index.html
- C:\Windows\System32\drivers\etc\hosts,添加本地域名映射
192.168.109.101 auth.achangmall.com
- 静态文件可以选择 Nginx 动静分离配置
-
修改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/
,访问成功!!!
- 修改product服务的注册和登录的uri
- 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
-
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");
}
}
- 明天继续!!!