|
|
@@ -12,6 +12,7 @@ import com.uas.sso.entity.login.*;
|
|
|
import com.uas.sso.entity.Token;
|
|
|
import com.uas.sso.entity.User;
|
|
|
import com.uas.sso.entity.UserAccount;
|
|
|
+import com.uas.sso.exception.PasswordErrorException;
|
|
|
import com.uas.sso.exception.VisibleError;
|
|
|
import com.uas.sso.foreign.entity.ForeignInfo;
|
|
|
import com.uas.sso.foreign.factory.ForeignFactory;
|
|
|
@@ -32,6 +33,7 @@ import org.springframework.util.StringUtils;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Calendar;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
@@ -76,7 +78,7 @@ public class LoginServiceImpl implements LoginService {
|
|
|
private static final int PWD_ERROR_THREE_TIME = 3;
|
|
|
|
|
|
@Override
|
|
|
- public ModelMap loginByPassword(PasswordLogin loginParam) {
|
|
|
+ public ModelMap loginByPassword(PasswordLogin loginParam) throws PasswordErrorException {
|
|
|
// 校验参数
|
|
|
if (loginParam == null) {
|
|
|
throw new VisibleError("参数错误");
|
|
|
@@ -95,7 +97,7 @@ public class LoginServiceImpl implements LoginService {
|
|
|
}
|
|
|
|
|
|
// 获取密码错误次数
|
|
|
- Integer pwdErrorCount = getPwdErrorCount(user);
|
|
|
+ Integer pwdErrorCount = getPwdErrorCount(user.getUserUU());
|
|
|
|
|
|
// 校验验证码
|
|
|
if (pwdErrorCount >= PWD_ERROR_FIVE_TIME) {
|
|
|
@@ -110,7 +112,11 @@ public class LoginServiceImpl implements LoginService {
|
|
|
}
|
|
|
|
|
|
// 校验密码
|
|
|
- userService.checkPassword(user.getUserUU(), loginParam.getPassword(), false);
|
|
|
+ String encryPassword = userService.getEncryPassword(Const.ENCRY_FORMAT, loginParam.getPassword(), user.getSalt());
|
|
|
+ if (!encryPassword.equals(user.getPassword())) {
|
|
|
+ int count = addPwdCount(user.getUserUU());
|
|
|
+ throw new PasswordErrorException("您输入的账号或密码有误", count);
|
|
|
+ }
|
|
|
|
|
|
// 校验密码强度,如果和存储的不同,则保存
|
|
|
int strength = PasswordLevelUtils.checkPasswordLevel(loginParam.getPassword()).getValue();
|
|
|
@@ -206,6 +212,15 @@ public class LoginServiceImpl implements LoginService {
|
|
|
return login(user.getUserUU(), spaceUU, loginParam).addAttribute("hasRegister", true);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public int getPwdErrorCount(String username) {
|
|
|
+ User user = userService.findByUsername(username);
|
|
|
+ if (user == null) {
|
|
|
+ throw new VisibleError("用户名不存在");
|
|
|
+ }
|
|
|
+ return getPwdErrorCount(user.getUserUU());
|
|
|
+ }
|
|
|
+
|
|
|
private ModelMap login(Long userUU, Long spaceUU, BaseLogin loginParam) {
|
|
|
App app = appService.findOne(loginParam.getAppId());
|
|
|
if (app == null) {
|
|
|
@@ -286,10 +301,6 @@ public class LoginServiceImpl implements LoginService {
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
- private void resetPwdCount(Long userUU) {
|
|
|
- tokenService.delete("login_count_" + userUU);
|
|
|
- }
|
|
|
-
|
|
|
private ModelMap addOtherAppRequestData(UserAccount userAccount, ModelMap data, BaseLogin loginParam) {
|
|
|
List<String> loginUrls = appService.findAllLoginUrl();
|
|
|
boolean loginAll = loginParam.isLoginAll();
|
|
|
@@ -311,8 +322,50 @@ public class LoginServiceImpl implements LoginService {
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
- private Integer getPwdErrorCount(User user) {
|
|
|
- Token token = tokenService.findOne("login_count_" + user.getUserUU());
|
|
|
+ /**
|
|
|
+ * 获取密码错误次数
|
|
|
+ * @param userUU 用户uu号
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Integer getPwdErrorCount(Long userUU) {
|
|
|
+ Token token = tokenService.findOne("login_count_" + userUU);
|
|
|
return (Integer) Optional.ofNullable(token).map(Token::getBind).orElse(0);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加密码错误次数
|
|
|
+ * @param userUU 用户uu号
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private int addPwdCount(Long userUU) {
|
|
|
+ String tokenId = "login_count_" + userUU;
|
|
|
+ Token token = tokenService.findOne(tokenId);
|
|
|
+ token = token == null ? new Token(0, getSecondsNextEarlyMorning().intValue()) : token;
|
|
|
+ int count = (Integer) Optional.ofNullable(token).map(Token::getBind).orElse(0) + 1;
|
|
|
+ token.setId(tokenId);
|
|
|
+ token.setBind(count);
|
|
|
+ tokenService.save(token);
|
|
|
+ return count;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long getSecondsNextEarlyMorning() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
+ // 改成这样就好了
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.MILLISECOND, 0);
|
|
|
+ Long seconds = (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
|
|
|
+ return seconds.longValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重置密码错误次数
|
|
|
+ * @param userUU 用户uu号
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private void resetPwdCount(Long userUU) {
|
|
|
+ tokenService.delete("login_count_" + userUU);
|
|
|
+ }
|
|
|
}
|