创建新module,作为认证中心(社交登陆、OAuth2.0、单点登录)
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
public class MyWebConfigeration implements WebMvcConfigurer {
// 视图映射 不需要编写控制层,直接将请求跳转相应页面
// **/login 跳转页面 login.html
// 路径映射默认get
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
}
}
短信再次校验问题
验证码60秒过期,60秒内同一个手机号不能重复发送
-- 使用redis,存储的值后面拼上时间,发送之前首先获取时间判断是否60秒
接口防刷问题
?
import com.example.beans.UserRegistDTO;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.validation.Valid;
import java.util.Map;
import java.util.stream.Collectors;
@Controller
public class TestController {
// 注册数据校验接口
// 重定向携带数据,利用session原理,将数据放在session中
// 只要跳到下一个页面且取出数据后,session中数据就会删掉
@PostMapping("/regist")
public String regist (@Valid UserRegistDTO vo, BindingResult result,
// 重定向方式的数据转发
RedirectAttributes redirectAttributes){
if(result.hasErrors()){
Map<String, String> errors = result.getFieldErrors().stream()
.collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
redirectAttributes.addFlashAttribute("errors",errors);
// 校验出错,重定向到注册页面
// 重定向带上全路径,不然会使用当前项目ip、端口地址而不经过域名网关等服务
return "redirect:http://auth.gulimall.com/reg.html";
// 转发方式会把post请求方式转发过去,而注册页面是get请求,会报错
// return "forward:reg";
}
// TODO 注册业务...
// 1、验证码对比--> 对比完删除
// 2、密码加密
// 3、数据存储
}
}
分布式服务下会出现session问题!!!!!!!!!
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
// 利用MD5加密的抗修改性
// 暴力破解方式,使用彩虹表,大量存储MD5加密数据 123456->******
// 所以不能直接用MD5加密
String s = DigestUtils.md5Hex("123456");
// 盐值加密
// 验证:密码盐值(盐值可以存储在数据库)加密然后和数据库中的密码对比
Md5Crypt.apr1Crypt("123456".getBytes(), "$s54g5s4d");
// spring的加密工具
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
// 加密
String encode = bCryptPasswordEncoder.encode("123456");
// 匹配,spring的加密数据中可以体现出加密的盐值
boolean matches = bCryptPasswordEncoder.matches("123456","#89Hsd454$YHioh498s456dg46sd");
OAuth2.0
OAuth:OAuth(开放授权)是一个开放标准,允许用户授权第三方网站访问他们存储
在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方网站或分享他们
数据的所有内容。
OAuth2.0:对于用户相关的 OpenAPI(例如获取用户信息,动态同步,照片,日志,分
享等),为了保护用户数据的安全和隐私,第三方网站访问用户数据前都需要显式的向
用户征求授权。
官方版流程
授权登录页面--> 返回code--> 使用code换区AccessToken,code只能使用一次--> 使用AccessToken查询开放接口
原文:https://www.cnblogs.com/yanyaqiblog/p/14179507.html