|
|
@@ -8,11 +8,14 @@ import com.uas.sso.SSOToken;
|
|
|
import com.uas.sso.common.util.HttpUtil;
|
|
|
import com.uas.sso.core.Const;
|
|
|
import com.uas.sso.entity.App;
|
|
|
+import com.uas.sso.entity.User;
|
|
|
import com.uas.sso.entity.UserAccount;
|
|
|
+import com.uas.sso.entity.UserRecord;
|
|
|
import com.uas.sso.service.AppService;
|
|
|
import com.uas.sso.service.UserService;
|
|
|
import com.uas.sso.service.impl.UserAccountService;
|
|
|
import com.uas.sso.util.AccountTypeUtils;
|
|
|
+import com.uas.sso.util.CaptchaUtil;
|
|
|
import com.uas.sso.web.waf.request.WafRequestWrapper;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.ui.ModelMap;
|
|
|
@@ -23,6 +26,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
+import javax.servlet.ServletException;
|
|
|
+import java.io.IOException;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
@@ -35,6 +40,8 @@ import java.util.*;
|
|
|
@RestController
|
|
|
public class LoginController extends BaseController {
|
|
|
|
|
|
+ private static final int PWD_ERROR_FIVE_TIME = 5;
|
|
|
+ private static final int PWD_ERROR_THREE_TIME = 3;
|
|
|
@Autowired
|
|
|
private AppService appService;
|
|
|
|
|
|
@@ -53,6 +60,7 @@ public class LoginController extends BaseController {
|
|
|
String spaceUU = wr.getParameter("spaceUU");
|
|
|
String username = StringUtils.trimAllWhitespace(wr.getParameter("username"));
|
|
|
String password = wr.getParameter("password");
|
|
|
+ String captcha = wr.getParameter("captcha");
|
|
|
String returnUrl = wr.getParameter("returnUrl");
|
|
|
String baseUrl = wr.getParameter("baseUrl");
|
|
|
|
|
|
@@ -76,6 +84,34 @@ public class LoginController extends BaseController {
|
|
|
return error("应用id不存在");
|
|
|
}
|
|
|
|
|
|
+ // 获取用户基本信息
|
|
|
+ User user = userService.findByUsername(username);
|
|
|
+ if (user == null) {
|
|
|
+ return error("用户名或密码错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验账户密码输错次数
|
|
|
+ int pwdErrorCount = user.getUserRecord() == null ? 0 : user.getUserRecord().getPwdErrorCount();
|
|
|
+ Object randomString = request.getSession().getAttribute("randomString");
|
|
|
+ String checkCode = randomString == null ? "" : randomString.toString();
|
|
|
+ if (pwdErrorCount >= PWD_ERROR_FIVE_TIME) {
|
|
|
+ return error("403", "密码错误次数已达上限,今日无法登陆");
|
|
|
+ }
|
|
|
+ // 校验账号是否被锁定,5次输错密码
|
|
|
+ if (pwdErrorCount >= PWD_ERROR_THREE_TIME && StringUtils.isEmpty(captcha)) {
|
|
|
+ return error("404", "验证码不能为空");
|
|
|
+ }
|
|
|
+ if (pwdErrorCount >= PWD_ERROR_THREE_TIME && !captcha.equalsIgnoreCase(checkCode)) {
|
|
|
+ return error("验证码错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验密码
|
|
|
+ String encryPwd = userService.getEncryPassword(Const.ENCRY_FORMAT, password, user.getSalt());
|
|
|
+ if (!encryPwd.equals(user.getPassword())) {
|
|
|
+ inputErrorPwd(user);
|
|
|
+ return error("您输入的账号或密码有误");
|
|
|
+ }
|
|
|
+
|
|
|
// 登录
|
|
|
App controlApp = StringUtils.isEmpty(app.getUserControl()) ? app : appService.findOne(app.getUserControl());
|
|
|
boolean personalEnable = Const.YES == controlApp.getPersonalEnable();
|
|
|
@@ -83,26 +119,16 @@ public class LoginController extends BaseController {
|
|
|
if (StringUtils.isEmpty(spaceUU)) {
|
|
|
/*企业uu号为空,让用户选择企业*/
|
|
|
// 找到用户账号信息
|
|
|
- List<UserAccount> userAccounts = getUserAccountByUserName(controlApp.getUid(), username);
|
|
|
+ List<UserAccount> userAccounts = userAccountService.findByUserUU(appId, user.getUserUU());
|
|
|
|
|
|
- // 应用不允许个人账号,并且账号未绑定企业
|
|
|
- if (!personalEnable && CollectionUtils.isEmpty(userAccounts)) {
|
|
|
- return error("用户名或密码错误");
|
|
|
+ // 没有记录
|
|
|
+ if (CollectionUtils.isEmpty(userAccounts)) {
|
|
|
+ return error("您的账号为绑定企业并且当前应用不支持个人账号");
|
|
|
}
|
|
|
|
|
|
// 应用允许个人账号,并且账号未绑定企业,或者只绑定了一个企业,直接登录
|
|
|
if (userAccounts.size() == 1) {
|
|
|
- return loginByUser(userAccounts.get(0), password, returnUrl);
|
|
|
- }
|
|
|
-
|
|
|
- // 由于老账户存在多个账号绑定一个邮箱的情况,把密码不符合的企业去除
|
|
|
- Iterator<UserAccount> iterator = userAccounts.iterator();
|
|
|
- while (iterator.hasNext()) {
|
|
|
- UserAccount userAccount = iterator.next();
|
|
|
- String encryPwd = userService.getEncryPassword(Const.ENCRY_FORMAT, password, userAccount.getSalt());
|
|
|
- if (!encryPwd.equals(userAccount.getPassword())) {
|
|
|
- iterator.remove();
|
|
|
- }
|
|
|
+ return loginByUser(userAccounts.get(0), returnUrl);
|
|
|
}
|
|
|
|
|
|
// 返回企业id和名称
|
|
|
@@ -110,14 +136,22 @@ public class LoginController extends BaseController {
|
|
|
} else if (spaceUU.equals(Const.SPACEUU_PERSONAL)) {
|
|
|
// 使用个人账号登录
|
|
|
UserAccount userAccount = getUserAccountByUserName(controlApp.getUid(), username, null);
|
|
|
- return loginByUser(userAccount, password, returnUrl);
|
|
|
+ return loginByUser(userAccount, returnUrl);
|
|
|
} else {
|
|
|
// 带企业登录
|
|
|
UserAccount userAccount = getUserAccountByUserName(controlApp.getUid(), username, spaceUU);
|
|
|
- return loginByUser(userAccount, password, returnUrl);
|
|
|
+ return loginByUser(userAccount, returnUrl);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void inputErrorPwd(User user) {
|
|
|
+ UserRecord userRecord = user.getUserRecord();
|
|
|
+ int pwdErrorCount = userRecord.getPwdErrorCount();
|
|
|
+ userRecord.setPwdErrorCount(++pwdErrorCount);
|
|
|
+ userService.save(userRecord);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
private List<UserAccount> getUserAccountByUserName(String appId, String username) {
|
|
|
String type = AccountTypeUtils.getAccountType(username);
|
|
|
if (AccountTypeUtils.MOBILE.equals(type)) {
|
|
|
@@ -130,7 +164,7 @@ public class LoginController extends BaseController {
|
|
|
}
|
|
|
if (AccountTypeUtils.UU_NUMBER.equals(type)) {
|
|
|
// uu号登录
|
|
|
- return userAccountService.findOneByUserUU(appId, Long.valueOf(username));
|
|
|
+ return userAccountService.findByUserUU(appId, Long.valueOf(username));
|
|
|
}
|
|
|
|
|
|
// 其余情况
|
|
|
@@ -156,17 +190,18 @@ public class LoginController extends BaseController {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- private ModelMap loginByUser(UserAccount userAccount, String password, String returnUrl) {
|
|
|
+ /**
|
|
|
+ * 用户信息没问题,直接登录
|
|
|
+ *
|
|
|
+ * @param userAccount 用户账号信息
|
|
|
+ * @param returnUrl 跳转url
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private ModelMap loginByUser(UserAccount userAccount, String returnUrl) {
|
|
|
if (StringUtils.isEmpty((userAccount.getPassword()))) {
|
|
|
// 使用错误码100来判断
|
|
|
return error("100", "未设置密码");
|
|
|
} else {
|
|
|
- // 校验密码
|
|
|
- String encryPwd = userService.getEncryPassword(Const.ENCRY_FORMAT, password, userAccount.getSalt());
|
|
|
- if (!encryPwd.equals(userAccount.getPassword())) {
|
|
|
- return error("您输入的账号或密码有误");
|
|
|
- }
|
|
|
-
|
|
|
// 登录
|
|
|
/*
|
|
|
* 设置登录 Cookie 最后一个参数 true 时添加 cookie 同时销毁当前 JSESSIONID
|
|
|
@@ -247,4 +282,15 @@ public class LoginController extends BaseController {
|
|
|
return new ModelMap("spaces", spaces);
|
|
|
}
|
|
|
|
|
|
+ @RequestMapping(value = "/checkcode", method = RequestMethod.GET)
|
|
|
+ @ResponseBody
|
|
|
+ public void checkCode() {
|
|
|
+ try {
|
|
|
+ CaptchaUtil.outputCaptcha(request, response);
|
|
|
+ } catch (ServletException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|