|
|
@@ -23,7 +23,6 @@ import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
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;
|
|
|
@@ -52,7 +51,6 @@ public class LoginController extends BaseController {
|
|
|
private UserAccountService userAccountService;
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.POST)
|
|
|
- @ResponseBody
|
|
|
public ModelMap login() {
|
|
|
// 获取登录信息
|
|
|
WafRequestWrapper wr = new WafRequestWrapper(request);
|
|
|
@@ -108,7 +106,7 @@ public class LoginController extends BaseController {
|
|
|
// 校验密码
|
|
|
String encryPwd = userService.getEncryPassword(Const.ENCRY_FORMAT, password, user.getSalt());
|
|
|
if (!encryPwd.equals(user.getPassword())) {
|
|
|
- inputErrorPwd(user);
|
|
|
+ inputErrorPwd(user.getUserRecord());
|
|
|
return error("您输入的账号或密码有误");
|
|
|
}
|
|
|
|
|
|
@@ -128,7 +126,7 @@ public class LoginController extends BaseController {
|
|
|
|
|
|
// 应用允许个人账号,并且账号未绑定企业,或者只绑定了一个企业,直接登录
|
|
|
if (userAccounts.size() == 1) {
|
|
|
- return loginByUser(userAccounts.get(0), returnUrl);
|
|
|
+ return loginByUser(userAccounts.get(0), returnUrl, user.getUserRecord());
|
|
|
}
|
|
|
|
|
|
// 返回企业id和名称
|
|
|
@@ -136,22 +134,37 @@ public class LoginController extends BaseController {
|
|
|
} else if (spaceUU.equals(Const.SPACEUU_PERSONAL)) {
|
|
|
// 使用个人账号登录
|
|
|
UserAccount userAccount = getUserAccountByUserName(controlApp.getUid(), username, null);
|
|
|
- return loginByUser(userAccount, returnUrl);
|
|
|
+ return loginByUser(userAccount, returnUrl, user.getUserRecord());
|
|
|
} else {
|
|
|
// 带企业登录
|
|
|
UserAccount userAccount = getUserAccountByUserName(controlApp.getUid(), username, spaceUU);
|
|
|
- return loginByUser(userAccount, returnUrl);
|
|
|
+ return loginByUser(userAccount, returnUrl, user.getUserRecord());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void inputErrorPwd(User user) {
|
|
|
- UserRecord userRecord = user.getUserRecord();
|
|
|
+ /**
|
|
|
+ * 密码输错处理
|
|
|
+ *
|
|
|
+ * @param userRecord 用户登录记录
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private int inputErrorPwd(UserRecord userRecord) {
|
|
|
+ // 密码输错次数+1
|
|
|
int pwdErrorCount = userRecord.getPwdErrorCount();
|
|
|
userRecord.setPwdErrorCount(++pwdErrorCount);
|
|
|
userService.save(userRecord);
|
|
|
- }
|
|
|
|
|
|
+ // 设置返回值
|
|
|
+ return pwdErrorCount;
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据用户名获得用户账号信息
|
|
|
+ *
|
|
|
+ * @param appId 应用
|
|
|
+ * @param username 用户名
|
|
|
+ * @return
|
|
|
+ */
|
|
|
private List<UserAccount> getUserAccountByUserName(String appId, String username) {
|
|
|
String type = AccountTypeUtils.getAccountType(username);
|
|
|
if (AccountTypeUtils.MOBILE.equals(type)) {
|
|
|
@@ -171,6 +184,14 @@ public class LoginController extends BaseController {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据用户名和企业uu找到用户信息
|
|
|
+ *
|
|
|
+ * @param appId 应用
|
|
|
+ * @param username 用户名
|
|
|
+ * @param spaceUU 企业uu号
|
|
|
+ * @return
|
|
|
+ */
|
|
|
private UserAccount getUserAccountByUserName(String appId, String username, String spaceUU) {
|
|
|
String type = AccountTypeUtils.getAccountType(username);
|
|
|
if (AccountTypeUtils.MOBILE.equals(type)) {
|
|
|
@@ -195,34 +216,31 @@ public class LoginController extends BaseController {
|
|
|
*
|
|
|
* @param userAccount 用户账号信息
|
|
|
* @param returnUrl 跳转url
|
|
|
+ * @param userRecord 用户登录记录,便于记录登录时间,不能直接new出来
|
|
|
* @return
|
|
|
*/
|
|
|
- private ModelMap loginByUser(UserAccount userAccount, String returnUrl) {
|
|
|
- if (StringUtils.isEmpty((userAccount.getPassword()))) {
|
|
|
- // 使用错误码100来判断
|
|
|
- return error("100", "未设置密码");
|
|
|
- } else {
|
|
|
- // 登录
|
|
|
- /*
|
|
|
- * 设置登录 Cookie 最后一个参数 true 时添加 cookie 同时销毁当前 JSESSIONID
|
|
|
- * 创建信任的 JSESSIONID
|
|
|
- */
|
|
|
- SSOToken st = new SSOToken(request, userAccount.getMobile());
|
|
|
- st.setData(JSON.toJSONString(userAccount));
|
|
|
- SSOHelper.setSSOCookie(request, response, st, true);
|
|
|
-
|
|
|
- // 通知各个应用用户已经登录
|
|
|
- ModelMap data = new ModelMap();
|
|
|
- data = addOtherAppRequestData(userAccount, data, request.getSession().getAttribute("baseUrl"), true);
|
|
|
- data.put("returnUrl", HttpUtil.decodeURL(returnUrl));
|
|
|
- return success(data);
|
|
|
- }
|
|
|
+ private ModelMap loginByUser(UserAccount userAccount, String returnUrl, UserRecord userRecord) {
|
|
|
+ /*
|
|
|
+ * 设置登录 Cookie 最后一个参数 true 时添加 cookie 同时销毁当前 JSESSIONID
|
|
|
+ * 创建信任的 JSESSIONID
|
|
|
+ */
|
|
|
+ SSOToken st = new SSOToken(request, userAccount.getMobile());
|
|
|
+ st.setData(JSON.toJSONString(userAccount));
|
|
|
+ SSOHelper.setSSOCookie(request, response, st, true);
|
|
|
+
|
|
|
+ // 设置登录时间
|
|
|
+ userRecord.setLastLoginTime(System.currentTimeMillis());
|
|
|
+ userService.save(userRecord);
|
|
|
+
|
|
|
+ // 设置返回值,通知各个应用用户已经登录
|
|
|
+ ModelMap data = new ModelMap();
|
|
|
+ data = addOtherAppRequestData(userAccount, data, request.getSession().getAttribute("baseUrl"), true);
|
|
|
+ data.put("returnUrl", HttpUtil.decodeURL(returnUrl));
|
|
|
+ return success(data);
|
|
|
}
|
|
|
|
|
|
private ModelMap addOtherAppRequestData(UserAccount userAccount, ModelMap data, Object loginUrl,
|
|
|
boolean isLoginAll) {
|
|
|
- // 需要通知的应用数量
|
|
|
- int count = 0;
|
|
|
List<App> apps = appService.findAll();
|
|
|
List<String> loginUrls = new ArrayList<>();
|
|
|
boolean hasLoginUrl = false;
|
|
|
@@ -235,17 +253,14 @@ public class LoginController extends BaseController {
|
|
|
hasLoginUrl = true;
|
|
|
}
|
|
|
loginUrls.add(app.getLoginUrl());
|
|
|
- count++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 添加baseUrl
|
|
|
if (!hasLoginUrl && !StringUtils.isEmpty(loginUrl)) {
|
|
|
loginUrls.add(loginUrl.toString());
|
|
|
- count++;
|
|
|
}
|
|
|
|
|
|
- data.put("count", count);
|
|
|
data.put("loginUrls", loginUrls);
|
|
|
|
|
|
// 添加传递数据
|
|
|
@@ -282,8 +297,7 @@ public class LoginController extends BaseController {
|
|
|
return new ModelMap("spaces", spaces);
|
|
|
}
|
|
|
|
|
|
- @RequestMapping(value = "/checkcode", method = RequestMethod.GET)
|
|
|
- @ResponseBody
|
|
|
+ @RequestMapping(value = "/checkCode", method = RequestMethod.GET)
|
|
|
public void checkCode() {
|
|
|
try {
|
|
|
CaptchaUtil.outputCaptcha(request, response);
|
|
|
@@ -293,4 +307,18 @@ public class LoginController extends BaseController {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得密码输错次数
|
|
|
+ *
|
|
|
+ * @param username 用户名
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/getPwdErrorCount", method = RequestMethod.GET)
|
|
|
+ public ModelMap getPwdErrorCount(String username) {
|
|
|
+ if (StringUtils.isEmpty(username)) {
|
|
|
+ return error("用户名不能为空");
|
|
|
+ }
|
|
|
+ return success(userService.getPwdErrorCount(username));
|
|
|
+ }
|
|
|
}
|