一、项目简述
本系统功能包括: 基于vue + Springboot前后端分离项目精简版仿小米商城 系统,注册登录,首页展示,商品展示,商品购买,下单支 付(支付有点小问题仅支持单个商品支付)后台维护等 等。
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX ( Webstorm也 行)+ Eclispe ( IntelliJ IDEA,Eclispe,MyEclispe,Sts都支 持)。
项目技术: Springboot + Maven + Mybatis + Vue + Redis, B/S 模式+ Maven等等。
登陆注册功能代码:
@CrossOrigin
@RestController
@RequestMapping("login")
public class LogRegController {
@Autowired
private RegLogServiceImp regLogServiceImp;
//注册功能
@RequestMapping("/getRegister")
public RegResult Register(@RequestParam("username") String username,
@RequestParam("email") String email,
@RequestParam("password") String password,
@RequestParam("phone") String phone) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
RegResult result = new RegResult();
System.out.println(username.length());
if(username.equals("") || email.equals("") || password.equals("") || phone.equals("")){
result.setStatus(404);
result.setMsg("填写信息不全");
return result;
}
User user = regLogServiceImp.getUserByName(username);
if (user != null) {
result.setStatus(2002);
result.setMsg("账号已存在,注册失败!");
}
else {
String userid = UUID.randomUUID().toString();
Map<String, String> keyMap = RSAUtils.createKeys(512);
String publicKey = keyMap.get("publicKey");
String privateKey = keyMap.get("privateKey");
//公钥加密
String encodedData = RSAUtils.publicEncrypt(password,RSAUtils.getPublicKey(publicKey));
String date = DateUtil.ptfDate();
regLogServiceImp.insertUser(userid,username,email,encodedData,phone,"可用",date,privateKey);
result.setStatus(200);
result.setMsg("注册成功!");
}
return result;
}
//登录验证
@RequestMapping("/UserLogin")
public LoginResult Login(@RequestParam("username") String username,
@RequestParam("password") String password) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
User user = regLogServiceImp.getUserByUserName(username);
LoginResult result = new LoginResult();
LoginData loginData = new LoginData();
if(user==null){
result.setMsg("无此用户,请重新输入正确用户名");
result.setStatus(2008);
}
else {
if(RSAUtils.privateDecrypt(user.getPwd(), RSAUtils.getPrivateKey(user.getPrivatekey().trim())).equals(password)){
result.setStatus(0);
String token = TokenUtil.token(user.getUserid().trim());
loginData.setToken(token);
result.setMsg("登录成功");
result.setData(loginData);
}
else {
result.setStatus(2007);
result.setMsg("密码错误");
}
}
return result;
}
}
Token密钥生成的使用方法:
public class TokenUtil {
//token秘钥
private static final String TOKEN_SECRET = "tongyige";
private static final String ISSUSER = "zhongtongyi";
// 签名的主题
private static final String SUBJECT = "this is litemall token";
public static String token(String username) {
String token = "";
try {
//秘钥及加密算法
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
//设置头部信息
Map<String, Object> header = new HashMap<>();
Date nowDate = new Date();
// 过期时间:2小时
Date expireDate = getAfterDate(nowDate, 0, 0, 0, 2, 0, 0);
header.put("typ", "JWT");
header.put("alg", "HS256");
//携带username,password信息,生成签名
token = JWT.create()
// 设置头部信息 Header
.withHeader(header)
// 设置 载荷 Payload
.withClaim("username", username)
.withIssuer(ISSUSER)
.withSubject(SUBJECT)
.withIssuedAt(nowDate)
.withExpiresAt(expireDate)
.sign(algorithm);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return token;
}
public static boolean verify(String token) {
/**
* @desc 验证token,通过返回true
* @params [token]需要校验的串
**/
try {
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
JWTVerifier verifier = JWT.require(algorithm)
.withIssuer(ISSUSER)
.build();
DecodedJWT jwt = verifier.verify(token);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static Date getAfterDate(Date date, int year, int month, int day, int hour, int minute, int second) {
if (date == null) {
date = new Date();
}
Calendar cal = new GregorianCalendar();
cal.setTime(date);
if (year != 0) {
cal.add(Calendar.YEAR, year);
}
if (month != 0) {
cal.add(Calendar.MONTH, month);
}
if (day != 0) {
cal.add(Calendar.DATE, day);
}
if (hour != 0) {
cal.add(Calendar.HOUR_OF_DAY, hour);
}
if (minute != 0) {
cal.add(Calendar.MINUTE, minute);
}
if (second != 0) {
cal.add(Calendar.SECOND, second);
}
return cal.getTime();
}
}