提交 154b4278 编写于 作者: cxt104926's avatar cxt104926

注册登录接口

上级 7c66e5cf
......@@ -25,14 +25,19 @@ public class ApiException extends RuntimeException {
@Getter
private String message;
// 系统错误
public static final ErrorVM VERIFICATION_CODE_FAIL = new ErrorVM(-50001, "获取验证码失败");
public static final ErrorVM E_LOGIN_EXCEPTION = new ErrorVM(-50002, "账户异常");
public static final ErrorVM E_REGISTER_CREATE_FAILURE = new ErrorVM(-50003, "账号创建失败");
public static final ErrorVM E_LOGIN_UNKNOWN_ACCOUNT = new ErrorVM(-10003, "验证码错误");
public static final ErrorVM E_LOGIN_INCORRECT_CREDENTIAL = new ErrorVM(-10004, "账号或者密码错误");
public static final ErrorVM E_LOGIN_LOCKED_ACCOUNT = new ErrorVM(-10005, "密码已锁定");
public static final ErrorVM E_LOGIN_EXCESSIVE_ATTEMPTS = new ErrorVM(-10006, "错误次数过多");
public static final ErrorVM E_LOGIN_EXCEPTION = new ErrorVM(-10007, "登录异常");
public static final ErrorVM E_USER_CREATE_FAILURE = new ErrorVM(-10008, "账号创建失败");
// 用户错误
public static final ErrorVM E_LOGIN_UNKNOWN_EMPTY = new ErrorVM(-10001, "验证码不能为空");
public static final ErrorVM E_LOGIN_UNKNOWN_CODE = new ErrorVM(-10002, "验证码错误");
public static final ErrorVM E_LOGIN_INCORRECT_CREDENTIAL = new ErrorVM(-10003, "账号或者密码错误");
public static final ErrorVM E_LOGIN_EXCESSIVE_ATTEMPTS = new ErrorVM(-10004, "错误次数过多");
public static final ErrorVM E_REGISTER_EXISTENCE_ACCOUNT = new ErrorVM(-10006, "注册账号已存在");
// 其他错误
public static final ErrorVM E_PASSWORD_NOT_SAME = new ErrorVM(-10009, "两次密码不一致");
public static final ErrorVM E_REPEAT_ADDITION = new ErrorVM(-10010, "不能重复添加");
public static final ErrorVM E_MODIFICATION_FAILURE = new ErrorVM(-10011, "操作失败");
......
......@@ -79,7 +79,7 @@ public class ApiResult<T> implements Serializable {
* 失败
*/
public static <T> ApiResult<T> fail() {
return new ApiResult<>(999, "成功");
return new ApiResult<>(999, "失败");
}
/**
......
......@@ -30,6 +30,7 @@ import javax.servlet.http.HttpServletResponse;
@Slf4j
@Api(tags = "登录接口")
public class LoginController {
private LoginService loginService;
/**
......@@ -67,15 +68,9 @@ public class LoginController {
@ResponseBody
@ApiOperation("登录验证")
public ApiResult<String> login(@RequestBody StuUserDTO stu, HttpServletRequest request) {
String verification = loginService.verification(stu, request);
switch (verification) {
case "codeFiled":
return ApiResult.success(100,"验证码不正确");
case "ok":
return ApiResult.success(200,"登录成功!");
default:
return ApiResult.success(300,"账号或者密码不正确!");
}
loginService.verification(stu.getVerification(), request);
loginService.verification(stu, request);
return ApiResult.success(200, "登录成功!");
}
/**
......@@ -85,12 +80,8 @@ public class LoginController {
@ResponseBody
@ApiOperation("注册验证")
public ApiResult<String> registers(@RequestBody RegisterDTO registerDTO) {
String register = loginService.register(registerDTO);
if ("ok".equals(register)) {
return ApiResult.success(200,"注册成功!");
} else {
return ApiResult.success(100,register);
}
loginService.register(registerDTO);
return ApiResult.success(200, "注册成功!");
}
/**
......@@ -100,14 +91,14 @@ public class LoginController {
*/
@GetMapping(value = "/getVerify")
@ApiOperation("获取验证码")
public void getVerify(HttpServletRequest request, HttpServletResponse response) throws Exception {
public void getVerify(HttpServletRequest request, HttpServletResponse response) {
try {
response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
response.setHeader("Pragma", "No-cache");//设置响应头信息,告诉浏览器不要缓存此内容
response.setContentType("image/jpeg"); // 设置相应类型,告诉浏览器输出的内容为图片
response.setHeader("Pragma", "No-cache"); // 设置响应头信息,告诉浏览器不要缓存此内容
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expire", 0);
ValidateCodeUtil randomValidateCode = new ValidateCodeUtil();
randomValidateCode.getRandcode(request, response);//输出验证码图片方法
randomValidateCode.getRandcode(request, response); // 输出验证码图片方法
} catch (Exception e) {
log.debug("获取验证码失败:" + e);
throw new ApiException(ApiException.VERIFICATION_CODE_FAIL);
......
package com.stu.stusystem.service;
import com.stu.stusystem.common.ApiException;
import com.stu.stusystem.controller.dto.RegisterDTO;
import com.stu.stusystem.controller.dto.StuUserDTO;
import com.stu.stusystem.mapper.StuMapper;
......@@ -37,46 +38,61 @@ public class LoginService {
/**
* 登录验证
*/
public String verification(StuUserDTO stu, HttpServletRequest request) {
public Boolean verification(StuUserDTO stu, HttpServletRequest request) {
boolean mg = false;
UsernamePasswordToken token = new UsernamePasswordToken(stu.getAccount(), stu.getPassword(), stu.getVerification());
Subject currentUser = SecurityUtils.getSubject();
try {
currentUser.login(token);
mg = true;
userLogMapper.insert(new UserLog(UUIDUtil.generate(), stu.getAccount(), IPUtil.getIpAddr(request), new Date()));
log.info("《账号:" + stu.getAccount() + "》登录成功");
} catch (UnknownAccountException | IncorrectCredentialsException uae) {
log.debug("登录失败:" + uae);
throw new ApiException(ApiException.E_LOGIN_INCORRECT_CREDENTIAL);
} catch (LockedAccountException lae) {
log.info("LockedAccountException");
throw new ApiException(ApiException.E_LOGIN_EXCEPTION);
} catch (ExcessiveAttemptsException eae) {
log.info("ExcessiveAttemptsException");
// 认证次数过限
throw new ApiException(ApiException.E_LOGIN_EXCESSIVE_ATTEMPTS);
} catch (AuthenticationException ae) {
log.info("AuthenticationException");
}
return mg;
}
/**
* 验证码验证
*/
public void verification(String newCode, HttpServletRequest request) {
if (newCode.isEmpty()) {
throw new ApiException(ApiException.E_LOGIN_UNKNOWN_EMPTY);
}
HttpSession session = request.getSession();
String code = (String) session.getAttribute("RANDOMVALIDATECODEKEY");
String c = code.toLowerCase();
String v = stu.getVerification().toLowerCase();
String mg = "";
String v = newCode.toLowerCase();
if (!v.equals(c)) {
mg = "codeFiled";
} else {
UsernamePasswordToken token = new UsernamePasswordToken(stu.getAccount(), stu.getPassword(), stu.getVerification());
Subject currentUser = SecurityUtils.getSubject();
try {
currentUser.login(token);
mg = "ok";
userLogMapper.insert(new UserLog(UUIDUtil.generate(), stu.getAccount(), IPUtil.getIpAddr(request), new Date()));
log.info("《账号:" + stu.getAccount() + "》登录成功");
} catch (UnknownAccountException | IncorrectCredentialsException uae) {
log.debug("登录失败:" + uae);
} catch (LockedAccountException lae) {
log.info("LockedAccountException");
} catch (ExcessiveAttemptsException eae) {
log.info("ExcessiveAttemptsException");
} catch (AuthenticationException ae) {
log.info("AuthenticationException");
}
throw new ApiException(ApiException.E_LOGIN_UNKNOWN_CODE);
}
return mg;
}
/**
* 注册账号
*/
public String register(RegisterDTO registerDTO) {
public void register(RegisterDTO registerDTO) {
List<StuUser> stuUsers = stuMapper.selectByExample(new Example.Builder(StuUser.class).where(WeekendSqls.<StuUser>custom().andEqualTo(StuUser::getAccount, registerDTO.getAccount())).build());
if (stuUsers.isEmpty()) {
String password = new Sha1Hash(registerDTO.getPassword(), "salt", 5).toBase64();
int insert = stuMapper.insert(new StuUser(UUIDUtil.generate(), registerDTO.getName(), registerDTO.getAccount(), registerDTO.getClassName(), password, registerDTO.getPhone(), registerDTO.getIdCard(), Jurisdiction.STUDENT));
return insert == 1 ? "ok" : "注册失败,联系管理员!";
if (insert != 1) {
throw new ApiException(ApiException.E_REGISTER_CREATE_FAILURE);
}
} else {
throw new ApiException(ApiException.E_REGISTER_EXISTENCE_ACCOUNT);
}
return "注册账号已存在!";
}
@Autowired
......
package com.stu.stusystem.util;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.codec.Hex;
import org.apache.shiro.crypto.hash.*;
import org.junit.Test;
/**
* @author cxt
* @date 2020/9/14
*/
public class Demo {
//加密内容
private String pass = "123";
//盐
private String salt = "salt";
//加密次数
private int hashIterations = 5;
/**
* base64
*/
public void test1() {
String encodeToString = Base64.encodeToString(pass.getBytes());
System.out.println(encodeToString);
}
/**
* md5加密
*/
public void test2() {
//MD5普通加密
String encodeToString = new Md5Hash(pass).toString();
System.out.println(encodeToString);
//md5加密转base64位编码或者16进制编码
String md5Base64 = new Md5Hash(pass).toBase64();
String md5Hex = new Md5Hash(pass).toHex();
System.out.println(md5Base64);
System.out.println(md5Hex);
//md5加密,加密内容source,带盐加密salt,还可以指定加密次数:hashIterations
md5Base64 = new Md5Hash("asdf", "123", 5).toBase64();
System.out.println(md5Base64);
}
/**
* sha加密
* SHA1,SHA256,SHA512
*/
public void test3() {
String sha1hash = new Sha1Hash(pass, salt, hashIterations).toBase64();
String sha256hash = new Sha256Hash(pass, salt, hashIterations).toBase64();
String sha512hash = new Sha512Hash(pass, salt, hashIterations).toBase64();
System.out.println(sha1hash);
System.out.println(sha256hash);
System.out.println(sha512hash);
}
/**
* 通用加密:SimpleHash,将算法名称添加到方法即可
*/
public void testSimleHash() {
// algorithmName 算法名称 第一个参数
String encryptStr = new SimpleHash("md5", pass, salt, hashIterations).toBase64();
String sha256 = new SimpleHash("sha-256", pass, salt, hashIterations).toBase64();
String sha512= new SimpleHash("sha-512", pass, salt, hashIterations).toBase64();
System.out.println(encryptStr);
System.out.println(sha256);
System.out.println(sha512);
}
//hex十六进制编码
public void testHex() {
String encodeToString = Hex.encodeToString(pass.getBytes());
String decodeToString = new String(Hex.decode(encodeToString));
System.out.println("加密:"+encodeToString);
System.out.println("解密:"+decodeToString);
}
@Test
public void tes(){
test1();
System.out.println("-----------------\n");
test2();
System.out.println("-----------------\n");
test3();
System.out.println("-----------------\n");
testSimleHash();
System.out.println("-----------------\n");
testHex();
System.out.println("-----------------\n");
}
}
......@@ -8,7 +8,11 @@ class StusystemApplicationTests {
@Test
void contextLoads() {
int temp = 2;
for (int i = 0; i <= temp; i++) {
int i1 = i + 16 * i;
System.out.println(i1);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册