添加用户名密码
parent
be6b633b08
commit
adb489a8e4
|
@ -23,6 +23,7 @@ import com.four.system.api.model.LoginUser;
|
|||
* @author ruoyi
|
||||
*/
|
||||
@RestController
|
||||
@SuppressWarnings("all")
|
||||
public class TokenController
|
||||
{
|
||||
@Autowired
|
||||
|
@ -32,14 +33,22 @@ public class TokenController
|
|||
private SysLoginService sysLoginService;
|
||||
|
||||
@PostMapping("login")
|
||||
public R<?> login(@RequestBody LoginBody login)
|
||||
public R<?> login(@RequestBody LoginBody form)
|
||||
{
|
||||
// 用户登录
|
||||
LoginUser userInfo = sysLoginService.login(login.getEmail(), login.getPassword());
|
||||
LoginUser userInfo = sysLoginService.login(form.getEmail(), form.getPassword());
|
||||
// 获取登录token
|
||||
return R.ok(tokenService.createToken(userInfo));
|
||||
}
|
||||
|
||||
@PostMapping("userLogin")
|
||||
public R<?> userLogin(@RequestBody LoginBody form){
|
||||
|
||||
LoginUser loginUser = sysLoginService.userLogin(form.getUsername(),form.getPassword());
|
||||
|
||||
return R.ok(tokenService.createToken(loginUser));
|
||||
}
|
||||
|
||||
@DeleteMapping("logout")
|
||||
public R<?> logout(HttpServletRequest request)
|
||||
{
|
||||
|
|
|
@ -10,7 +10,7 @@ public class LoginBody
|
|||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String username;
|
||||
private String userName;
|
||||
|
||||
private String email;
|
||||
/**
|
||||
|
@ -20,7 +20,7 @@ public class LoginBody
|
|||
|
||||
public String getUsername()
|
||||
{
|
||||
return username;
|
||||
return userName;
|
||||
}
|
||||
|
||||
|
||||
|
@ -34,7 +34,7 @@ public class LoginBody
|
|||
|
||||
public void setUsername(String username)
|
||||
{
|
||||
this.username = username;
|
||||
this.userName = username;
|
||||
}
|
||||
|
||||
public String getPassword()
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.four.auth.service;
|
||||
|
||||
import org.apache.catalina.Contained;
|
||||
import org.apache.catalina.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import com.four.common.core.constant.CacheConstants;
|
||||
|
@ -18,12 +20,15 @@ import com.four.system.api.RemoteUserService;
|
|||
import com.four.system.api.domain.SysUser;
|
||||
import com.four.system.api.model.LoginUser;
|
||||
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
/**
|
||||
* 登录校验方法
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Component
|
||||
@SuppressWarnings("all")
|
||||
public class SysLoginService
|
||||
{
|
||||
@Autowired
|
||||
|
@ -46,36 +51,36 @@ public class SysLoginService
|
|||
// 用户名或密码为空 错误
|
||||
if (StringUtils.isAnyBlank(email, password))
|
||||
{
|
||||
recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "用户/密码必须填写");
|
||||
recordLogService.recordLogininfors(email, Constants.LOGIN_FAIL, "用户/密码必须填写");
|
||||
throw new ServiceException("用户/密码必须填写");
|
||||
}
|
||||
// 密码如果不在指定范围内 错误
|
||||
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|
||||
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH)
|
||||
{
|
||||
recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "用户密码不在指定范围");
|
||||
recordLogService.recordLogininfors(email, Constants.LOGIN_FAIL, "用户密码不在指定范围");
|
||||
throw new ServiceException("用户密码不在指定范围");
|
||||
}
|
||||
// 用户名不在指定范围内 错误
|
||||
if (email.length() < UserConstants.USERNAME_MIN_LENGTH
|
||||
|| email.length() > UserConstants.USERNAME_MAX_LENGTH)
|
||||
{
|
||||
recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "用户名不在指定范围");
|
||||
recordLogService.recordLogininfors(email, Constants.LOGIN_FAIL, "用户名不在指定范围");
|
||||
throw new ServiceException("用户名不在指定范围");
|
||||
}
|
||||
// IP黑名单校验
|
||||
String blackStr = Convert.toStr(redisService.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST));
|
||||
if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr()))
|
||||
{
|
||||
recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "很遗憾,访问IP已被列入系统黑名单");
|
||||
recordLogService.recordLogininfors(email, Constants.LOGIN_FAIL, "很遗憾,访问IP已被列入系统黑名单");
|
||||
throw new ServiceException("很遗憾,访问IP已被列入系统黑名单");
|
||||
}
|
||||
// 查询用户信息
|
||||
R<LoginUser> userResult = remoteUserService.getUserInfo(email, SecurityConstants.INNER);
|
||||
R<LoginUser> userResult = remoteUserService.getUserInfoByEmail(email, SecurityConstants.INNER);
|
||||
|
||||
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
|
||||
{
|
||||
recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "登录用户不存在");
|
||||
recordLogService.recordLogininfors(email, Constants.LOGIN_FAIL, "登录用户不存在");
|
||||
throw new ServiceException("登录用户:" + email + " 不存在");
|
||||
}
|
||||
|
||||
|
@ -88,16 +93,16 @@ public class SysLoginService
|
|||
SysUser user = userResult.getData().getSysUser();
|
||||
if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
|
||||
{
|
||||
recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
|
||||
recordLogService.recordLogininfors(email, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
|
||||
throw new ServiceException("对不起,您的账号:" + email + " 已被删除");
|
||||
}
|
||||
if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
|
||||
{
|
||||
recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
|
||||
recordLogService.recordLogininfors(email, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
|
||||
throw new ServiceException("对不起,您的账号:" + email + " 已停用");
|
||||
}
|
||||
passwordService.validate(user, password);
|
||||
recordLogService.recordLogininfor(email, Constants.LOGIN_SUCCESS, "登录成功");
|
||||
recordLogService.recordLogininfors(email, Constants.LOGIN_SUCCESS, "登录成功");
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
|
@ -109,15 +114,15 @@ public class SysLoginService
|
|||
/**
|
||||
* 注册
|
||||
*/
|
||||
public void register(String username, String password)
|
||||
public void register(String userName, String password)
|
||||
{
|
||||
// 用户名或密码为空 错误
|
||||
if (StringUtils.isAnyBlank(username, password))
|
||||
if (StringUtils.isAnyBlank(userName, password))
|
||||
{
|
||||
throw new ServiceException("用户/密码必须填写");
|
||||
}
|
||||
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|
||||
|| username.length() > UserConstants.USERNAME_MAX_LENGTH)
|
||||
if (userName.length() < UserConstants.USERNAME_MIN_LENGTH
|
||||
|| userName.length() > UserConstants.USERNAME_MAX_LENGTH)
|
||||
{
|
||||
throw new ServiceException("账户长度必须在2到20个字符之间");
|
||||
}
|
||||
|
@ -129,8 +134,8 @@ public class SysLoginService
|
|||
|
||||
// 注册用户信息
|
||||
SysUser sysUser = new SysUser();
|
||||
sysUser.setUserName(username);
|
||||
sysUser.setNickName(username);
|
||||
sysUser.setUserName(userName);
|
||||
sysUser.setNickName(userName);
|
||||
sysUser.setPassword(SecurityUtils.encryptPassword(password));
|
||||
R<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
|
||||
|
||||
|
@ -138,6 +143,59 @@ public class SysLoginService
|
|||
{
|
||||
throw new ServiceException(registerResult.getMsg());
|
||||
}
|
||||
recordLogService.recordLogininfor(username, Constants.REGISTER, "注册成功");
|
||||
recordLogService.recordLogininfor(userName, Constants.REGISTER, "注册成功");
|
||||
}
|
||||
|
||||
public LoginUser userLogin(String userName, String password) {
|
||||
|
||||
if(StringUtils.isAllBlank(userName,password)){
|
||||
recordLogService.recordLogininfor(userName,Constants.LOGIN_FAIL,"用户、密码必须填写");
|
||||
throw new ServiceException("用户/密码必须填写");
|
||||
}
|
||||
|
||||
if(password.length() < UserConstants.PASSWORD_MIN_LENGTH
|
||||
||password.length()> UserConstants.PASSWORD_MAX_LENGTH){
|
||||
recordLogService.recordLogininfor(userName,Constants.LOGIN_FAIL,"用户密码不在指定范围");
|
||||
throw new ServiceException("用户密码不在指定范围");
|
||||
}
|
||||
|
||||
if(userName .length() < UserConstants.USERNAME_MIN_LENGTH || userName.length() > UserConstants.PASSWORD_MAX_LENGTH){
|
||||
recordLogService.recordLogininfor(userName, Constants.LOGIN_FAIL,"用户不在指定范围");
|
||||
throw new ServiceException("用户不在指定范围");
|
||||
}
|
||||
|
||||
String blackStr = Convert.toStr(redisService.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST));
|
||||
if(IpUtils.isMatchedIp(blackStr,IpUtils.getIpAddr())){
|
||||
recordLogService.recordLogininfor(blackStr,Constants.LOGIN_FAIL,"很遗憾,访问IP已被列入系统黑名单");
|
||||
throw new ServiceException("很遗憾,访问IP已被列入系统黑名单");
|
||||
}
|
||||
|
||||
|
||||
R<LoginUser> userResult = remoteUserService.getUserInfo(userName, SecurityConstants.INNER);
|
||||
|
||||
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
|
||||
{
|
||||
recordLogService.recordLogininfor(userName, Constants.LOGIN_FAIL, "登录用户不存在");
|
||||
throw new ServiceException("登录用户:" + userName + " 不存在");
|
||||
}
|
||||
|
||||
if(R.FAIL == userResult.getCode()){
|
||||
throw new ServiceException(userResult.getMsg());
|
||||
}
|
||||
|
||||
LoginUser dataInfo = userResult.getData();
|
||||
|
||||
SysUser sysUser = userResult.getData().getSysUser();
|
||||
if(UserStatus.DISABLE.getCode().equals(sysUser.getDelFlag())){
|
||||
recordLogService.recordLogininfor(userName,Constants.LOGIN_FAIL,"对不起,您的账号已被删除");
|
||||
throw new ServiceException("对不起,你的账号:"+userName + "已被删除");
|
||||
}
|
||||
if(UserStatus.DISABLE.getCode().equals(sysUser.getStatus())){
|
||||
recordLogService.recordLogininfor(userName,Constants.LOGIN_FAIL,"用户已停用,请联系管理员");
|
||||
throw new ServiceException("对不起 您的账号:"+userName + "已停用");
|
||||
}
|
||||
passwordService.validate(sysUser,password);
|
||||
recordLogService.recordLogininfor(userName,Constants.LOGIN_SUCCESS,"登录成功");
|
||||
return dataInfo;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,11 @@ import com.four.system.api.domain.SysUser;
|
|||
* @author ruoyi
|
||||
*/
|
||||
@Component
|
||||
public class SysPasswordService
|
||||
public class
|
||||
|
||||
|
||||
|
||||
SysPasswordService
|
||||
{
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
@ -31,12 +35,12 @@ public class SysPasswordService
|
|||
/**
|
||||
* 登录账户密码错误次数缓存键名
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param userName 用户名
|
||||
* @return 缓存键key
|
||||
*/
|
||||
private String getCacheKey(String username)
|
||||
private String getCacheKey(String userName)
|
||||
{
|
||||
return CacheConstants.PWD_ERR_CNT_KEY + username;
|
||||
return CacheConstants.PWD_ERR_CNT_KEY + userName;
|
||||
}
|
||||
|
||||
public void validate(SysUser user, String password)
|
||||
|
@ -50,6 +54,7 @@ public class SysPasswordService
|
|||
retryCount = 0;
|
||||
}
|
||||
|
||||
|
||||
if (retryCount >= Integer.valueOf(maxRetryCount).intValue())
|
||||
{
|
||||
String errMsg = String.format("密码输入错误%s次,帐户锁定%s分钟", maxRetryCount, lockTime);
|
||||
|
@ -57,6 +62,8 @@ public class SysPasswordService
|
|||
throw new ServiceException(errMsg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (!matches(user, password))
|
||||
{
|
||||
retryCount = retryCount + 1;
|
||||
|
@ -64,6 +71,7 @@ public class SysPasswordService
|
|||
redisService.setCacheObject(getCacheKey(userEmail), retryCount, lockTime, TimeUnit.MINUTES);
|
||||
throw new ServiceException("用户不存在/密码错误");
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
clearLoginRecordCache(userEmail);
|
||||
|
|
|
@ -15,6 +15,7 @@ import com.four.system.api.domain.SysLogininfor;
|
|||
* @author ruoyi
|
||||
*/
|
||||
@Component
|
||||
@SuppressWarnings("all")
|
||||
public class SysRecordLogService
|
||||
{
|
||||
@Autowired
|
||||
|
@ -23,26 +24,37 @@ public class SysRecordLogService
|
|||
/**
|
||||
* 记录登录信息
|
||||
*
|
||||
* @param email 用户名
|
||||
* @param username 用户名
|
||||
* @param status 状态
|
||||
* @param message 消息内容
|
||||
* @return
|
||||
*/
|
||||
public void recordLogininfor(String email, String status, String message)
|
||||
{
|
||||
public void recordLogininfor(String userName, String status, String message) {
|
||||
SysLogininfor logininfor = new SysLogininfor();
|
||||
logininfor.setUserName(userName);
|
||||
logininfor.setIpaddr(IpUtils.getIpAddr());
|
||||
logininfor.setMsg(message);
|
||||
// 日志状态
|
||||
if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) {
|
||||
logininfor.setStatus(Constants.LOGIN_SUCCESS_STATUS);
|
||||
} else if (Constants.LOGIN_FAIL.equals(status)) {
|
||||
logininfor.setStatus(Constants.LOGIN_FAIL_STATUS);
|
||||
}
|
||||
remoteLogService.saveLogininfor(logininfor, SecurityConstants.INNER);
|
||||
}
|
||||
|
||||
public void recordLogininfors(String email, String status, String message) {
|
||||
SysLogininfor logininfor = new SysLogininfor();
|
||||
logininfor.setUserName(email);
|
||||
logininfor.setIpaddr(IpUtils.getIpAddr());
|
||||
logininfor.setMsg(message);
|
||||
// 日志状态
|
||||
if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER))
|
||||
{
|
||||
if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) {
|
||||
logininfor.setStatus(Constants.LOGIN_SUCCESS_STATUS);
|
||||
}
|
||||
else if (Constants.LOGIN_FAIL.equals(status))
|
||||
{
|
||||
} else if (Constants.LOGIN_FAIL.equals(status)) {
|
||||
logininfor.setStatus(Constants.LOGIN_FAIL_STATUS);
|
||||
}
|
||||
remoteLogService.saveLogininfor(logininfor, SecurityConstants.INNER);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 9200
|
||||
port: 9203
|
||||
|
||||
# Spring
|
||||
spring:
|
||||
|
|
Loading…
Reference in New Issue