一、登录
1、登录的准备
1.1).使用技术 OAuth2 认证中心
1.2).创建认证中心项目
1.2).配置中心 application.properties
spring.application.name=gulimall-auth-server
server.port=20000
spring.cloud.nacos.discovery.server-addr=192.168.56.10:8848
1.3).启动类
@EnableDiscoveryClient //开启nacos
@EnableFeignClients //开启feign远程调用
1.4).nacos首页
2.导入资源
2.1).导入登录注册index.html到项目templates,修改名称为login(登录)、reg(注册)
2.2).修改hosts地址,修改访问路径
2.3).导入注册、注册的静态资源到nginx下(html/static/login、reg下)
2.4).修改html的静态资源路径
点击Ctrl+R
2.5).修改nginx配置文件
把gulimall.conf的host修改为 **.gulimall.com,在前面商品详情已经修改过。
2.6).配置网关gateway
2.7).测试–ok
3.修改点击,进行跳转页面
3.1).登录页面,点击谷粒商城 跳转到 谷粒商城(在auth的login.html修改)
3.2)谷粒商城页面,点击登录 跳转到 登录页面(在product的index.html修改)
3.3)谷粒商城页面,点击注册 跳转到 注册页面(在product的index.html修改)
3.4)登录页面,点击注册 跳转到 注册页面(在auth的login.html修改)
3.5)注册页面,点击登录 跳转到 登录页面(在auth的reg.html修改)
3.6).写controller
3.6).替换controller,删除controller
@Configuration
public class GulimallWebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/login.html").setViewName("login");
registry.addViewController("/reg.html").setViewName("reg");
}
}
3.7).测试,点击是否跳转成功–ok
4.注册页–获取验证码
4.1).修改下验证码的标签
4.2).点击事件,点击发送验证码
4.3).阿里云短信服务
阿里云短信服务
注册可以
616f544d97d84ce586af557d30fe2e6a
5.获取验证码-(自己写的1.3.4.5)
5.1).gulinal-third-party在第三方项目,创建SmsComponent
@Component
@Data
@ToString
public class SmsComponent {
private String code;
private String phone;
public SmsComponent(){}
public SmsComponent(String phone, String code) {
this.code = code;
this.phone = phone;
}
public void sendMsm(String phone, String code) {
String host = "https://gyytz.market.alicloudapi.com";
String path = "/sms/smsSend";
String method = "POST";
String appcode = "616f544d97d84ce586af557d30fe2e6a";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
Map<String, String> querys = new HashMap<String, String>();
querys.put("mobile", phone);
querys.put("param", "**code**:" + code + ",**minute**:5");
querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");
querys.put("templateId", "908e94ccf08b4476ba6c876d13f084ad");
Map<String, String> bodys = new HashMap<String, String>();
try {
/**
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
*/
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
// System.out.println(response.toString());
//获取response的body
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
5.2).gulinal-third-party的HttpUtils工具类
5.3).gulinal-third-party的SmsSendController
//发送验证码
@Controller
@RequestMapping("/sms")
public class SmsSendController {
@GetMapping("/sendcode")
@ResponseBody
public R sendCode(@RequestParam("phone") String phone, @RequestParam("code") String code) {
new SmsComponent().sendMsm(phone, code);
return R.ok();
}
}
5.4).gulimall-auth-server的远程调用service
@FeignClient("gulinal-third-party")
public interface ThirdPartyFeignService {
//1.验证码
@GetMapping("/sms/sendcode")
public R sendCode(@RequestParam("phone") String phone, @RequestParam("code") String code);
}
5.5).gulimall-auth-server的LoginController
@Controller
public class LoginController {
@Autowired
ThirdPartyFeignService thirdPartyFeignService;
@GetMapping("/sms/sendcode")
@ResponseBody
public R sendcode(@RequestParam("phone") String phone){
String code = UUIDGenrateUtil.getRandom(4);
System.out.println("code: "+code);
System.out.println("phone: "+phone);
thirdPartyFeignService.sendCode(phone,code);
return R.ok();
}
}
5.6).auth的注册页面使用,获取验证码reg.html
5.7).gulimall-auth-server的远程调用service产生随机数的工具类
/**
* @描述: 随机四位uuid
*/
public class UUIDGenrateUtil {
public static String[] chars = new String[]{"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9"};
public synchronized static String generateShortUuid() {
StringBuffer shortBuffer = new StringBuffer();
String uuid = getUniqueOrder();
for (int i = 0; i < 5; i++) {
String str = uuid.substring(i * 5, i * 5 + 5);
int x = Integer.parseInt(str, 16);
shortBuffer.append(chars[x % 0x3E]);
}
return shortBuffer.toString();
}
/**
* 获得唯一订单号
*/
public static String getUniqueOrder() {
int hashCodeV = Math.abs(UUID.randomUUID().toString().hashCode());
long timeInMillis = Calendar.getInstance().getTimeInMillis();
String format = String.format("%032d", hashCodeV);
String uuid = "0" + timeInMillis + format;
return uuid;
}
private static final SecureRandom secureRandom = new SecureRandom();//获取随机数
private static final String SYMBOLS = "0123456789"; // 数字
/**
* 获取N位随机数
*
* @return
*/
public static String getRandom(Integer n) {
String randomNum = "";
char[] nonceChars = new char[n];
for (int index = 0; index < nonceChars.length; ++index) {
nonceChars[index] = SYMBOLS.charAt(secureRandom.nextInt(SYMBOLS.length()));
}
randomNum = new String(nonceChars);
return randomNum;
}
}