第十章:认证服务
一、环境的搭建
1)、新建gulimall-auth-server项目
2)、前端页面的搬运
3)、网关的配置
- id: gulimall-auth-route
uri: lb://gulimall-auth-server
predicates:
- Host=auth.gulimall.com
4)、页面的跳转
1、完善登录页auth.gulimall.com跳转到首页gulimall.com的逻辑
2、完善首页gulimall.com跳转到登录页auth.gulimall.com的逻辑
3、完善登录页auth.gulimall.com跳转到注册页auth.gulimall.com的逻辑
4、完善注册页auth.gulimall.com跳转到登录页auth.gulimall.com的逻辑
跳转页面
方法一、控制器(old)
@Controller
public class IndexController {
@GetMapping("/login.html")
public String loginPage(){
return "login";
}
@GetMapping("/reg.html")
public String regPage(){
return "reg";
}
}
方法二、视图设置(new)
@Configuration
public class GulimallWebConfig implements WebMvcConfigurer {
/**
* 视图映射
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
/**
* @GetMapping("/login.html")
* public String loginPage(){
*
* return "login";
* }
*/
registry.addViewController("/login.html").setViewName("login");
//只是get请求能映射
registry.addViewController("/reg.html").setViewName("reg");
}
}
二、验证码
前端验证码的修改
1)、修改显示页面为
2)、js的修改
$(function () {
$("#sendCode").click(function () {
//2、倒计时
if ($(this).hasClass("disabled")) {
//正在倒计时。
} else {
//1、给指定手机号发送验证码
$.get("/sms/sendcode?phone=" + $("#phoneNum").val(), function (data) {
if (data.code != 0) {
alert(data.msg);
}
});
timeoutChangeStyle();
}
});
})
var num = 60;
function timeoutChangeStyle() {
$("#sendCode").attr("class", "disabled");
if (num == 0) {
$("#sendCode").text("发送验证码");
num = 60;
$("#sendCode").attr("class", "");
} else {
var str = num + "s 后再次发送";
$("#sendCode").text(str);
setTimeout("timeoutChangeStyle()", 1000);
}
num--;
}
三、发送验证码功能
可以自己搭建短信签名模板,也可以去阿里云市场购买短信服务
1)、为第三方插件编写发送短信的功能
1.SmsSendController控制器
@RestController
@RequestMapping("/sms")
public class SmsSendController {
@Autowired
SmsComponent smsComponent;
/**
* 提供给别的服务进行调用
* @param phone
* @param code
* @return
*/
@GetMapping("/sendcode")
public R sendCode(@RequestParam("phone") String phone, @RequestParam("code") String code){
smsComponent.sendSmsCode(phone,code);
return R.ok();
}
}
2.SmsComponent实现类方法购买自己的api方法
@ConfigurationProperties(prefix = "spring.cloud.alicloud.sms")
@Data
@Component
public class SmsComponent {
private String host;
private String path;
private String skin;
private String sign;
private String appcode;
public void sendSmsCode(String phone,String code){
String urlSend = host + path + "?code=" + code + "&phone=" + phone + "&sign=" + sign + "&skin=" + skin ; // 【5】拼接请求链接
try {
URL url = new URL(urlSend);
HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
httpURLCon.setRequestProperty("Authorization", "APPCODE " + appcode);// 格式Authorization:APPCODE
// (中间是英文空格)
int httpCode = httpURLCon.getResponseCode();
if (httpCode == 200) {
String json = read(httpURLCon.getInputStream());
System.out.println("正常请求计费(其他均不计费)");
System.out.println("获取返回的json:");
System.out.print(json);
} else {
Map<String, List<String>> map = httpURLCon.getHeaderFields();
String error = map.get("X-Ca-Error-Message").get(0);
if (httpCode == 400 && error.equals("Invalid AppCode `not exists`")) {
System.out.println("AppCode错误 ");
} else if (httpCode == 400 && error.equals("Invalid Url")) {
System.out.println("请求的 Method、Path 或者环境错误");
} else if (httpCode == 400 && error.equals("Invalid Param Location")) {
System.out.println("参数错误");
} else if (httpCode == 403 && error.equals("Unauthorized")) {
System.out.println("服务未被授权&