|
@@ -1,14 +1,22 @@
|
|
|
package com.uas.sso.controller;
|
|
package com.uas.sso.controller;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.uas.message.sms.service.SmsService;
|
|
|
|
|
+import com.uas.sso.core.Const;
|
|
|
import com.uas.sso.core.PasswordStrength;
|
|
import com.uas.sso.core.PasswordStrength;
|
|
|
|
|
+import com.uas.sso.entity.Setting;
|
|
|
|
|
+import com.uas.sso.entity.Token;
|
|
|
import com.uas.sso.exception.VisibleError;
|
|
import com.uas.sso.exception.VisibleError;
|
|
|
|
|
+import com.uas.sso.service.SettingService;
|
|
|
|
|
+import com.uas.sso.service.TokenService;
|
|
|
|
|
+import com.uas.sso.util.StringUtil;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.ui.ModelMap;
|
|
import org.springframework.ui.ModelMap;
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
@@ -31,6 +39,15 @@ public class BaseController {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
protected HttpServletResponse response;
|
|
protected HttpServletResponse response;
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ protected TokenService tokenService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ protected SmsService smsService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ protected SettingService settingService;
|
|
|
|
|
+
|
|
|
protected static boolean isSuccess(ModelMap map) {
|
|
protected static boolean isSuccess(ModelMap map) {
|
|
|
return Boolean.TRUE.equals(map.get("success"));
|
|
return Boolean.TRUE.equals(map.get("success"));
|
|
|
}
|
|
}
|
|
@@ -105,6 +122,13 @@ public class BaseController {
|
|
|
return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.CREATED);
|
|
return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.CREATED);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 校验密码强度
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param password 密码
|
|
|
|
|
+ * @return PasswordStrength枚举
|
|
|
|
|
+ * @throws VisibleError 用户可见异常
|
|
|
|
|
+ */
|
|
|
protected PasswordStrength checkPasswordLevel(String password) throws VisibleError {
|
|
protected PasswordStrength checkPasswordLevel(String password) throws VisibleError {
|
|
|
String strongRegex = "^(?=.{8,20})(((?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]))|((?=.*[0-9])((?=.*[a-zA-Z]))(?=.*[^a-zA-Z0-9]))).*$";
|
|
String strongRegex = "^(?=.{8,20})(((?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]))|((?=.*[0-9])((?=.*[a-zA-Z]))(?=.*[^a-zA-Z0-9]))).*$";
|
|
|
String mediumRegex = "^(?=.{8,20})(((?=.*[0-9])(?=.*[a-z]))|((?=.*[0-9])(?=.*[A-Z]))).*$";
|
|
String mediumRegex = "^(?=.{8,20})(((?=.*[0-9])(?=.*[a-z]))|((?=.*[0-9])(?=.*[A-Z]))).*$";
|
|
@@ -119,4 +143,93 @@ public class BaseController {
|
|
|
return PasswordStrength.WEAK;
|
|
return PasswordStrength.WEAK;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取手机号验证码
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return tokenId
|
|
|
|
|
+ */
|
|
|
|
|
+ protected String getMobileCode(String mobile) {
|
|
|
|
|
+ // 随机获得验证码
|
|
|
|
|
+ String code = StringUtil.getRandomNumber(6);
|
|
|
|
|
+ Token token = new Token(code, 10*60);
|
|
|
|
|
+
|
|
|
|
|
+ // 设置绑定手机,防止获取验证码之后修改手机号
|
|
|
|
|
+ token.setMobile(mobile);
|
|
|
|
|
+
|
|
|
|
|
+ // 将token存到Redis服务器上
|
|
|
|
|
+ tokenService.save(token);
|
|
|
|
|
+
|
|
|
|
|
+ // 将验证码发送到手机上
|
|
|
|
|
+ ModelMap data = new ModelMap();
|
|
|
|
|
+ data.put("checkcode", code);
|
|
|
|
|
+ // 手机短信
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (!StringUtils.isEmpty(mobile)) {
|
|
|
|
|
+ Setting smsTplId = settingService.findOne("templateForSendSmsWhenRegister");
|
|
|
|
|
+ if (!StringUtils.isEmpty(smsTplId)) {
|
|
|
|
|
+ smsService.send(smsTplId.getValue(), mobile, new Object[]{code});
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 返回tokenId
|
|
|
|
|
+ return token.getId();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 校验手机号
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param token 验证码tokenID
|
|
|
|
|
+ * @param mobile 手机号
|
|
|
|
|
+ * @param code 验证码
|
|
|
|
|
+ * @return
|
|
|
|
|
+ * @throws VisibleError 校验失败则抛异常
|
|
|
|
|
+ * 当参数异常,token过期或者token绑定的手机号不对时抛出此异常
|
|
|
|
|
+ */
|
|
|
|
|
+ protected void checkMobileCode(String token, String mobile, String code) {
|
|
|
|
|
+ // 校验参数
|
|
|
|
|
+ if (StringUtils.isEmpty(token) || StringUtils.isEmpty(code)) {
|
|
|
|
|
+ throw new VisibleError("参数错误");
|
|
|
|
|
+ }
|
|
|
|
|
+ Token existToken = tokenService.findOne(token);
|
|
|
|
|
+ if (existToken == null || existToken.isExpired()) {
|
|
|
|
|
+ throw new VisibleError("验证码已经失效,请重新获取");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isEmpty(mobile) || !mobile.equals(existToken.getMobile())) {
|
|
|
|
|
+ throw new VisibleError("手机号被修改,请重新获取验证码");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 校验验证码
|
|
|
|
|
+ String existCode = existToken.getBind().toString();
|
|
|
|
|
+ if (!code.equals(existCode)) {
|
|
|
|
|
+ throw new VisibleError("验证码错误");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 校验手机号格式
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param mobile 手机号
|
|
|
|
|
+ * @param mobileArea 手机号所属区域
|
|
|
|
|
+ */
|
|
|
|
|
+ protected void checkMobile(String mobile, String mobileArea) {
|
|
|
|
|
+ // 由于现在不考虑手机号所属区域,默认为中国大陆
|
|
|
|
|
+ mobileArea = mobileArea == null ? Const.CONTINENT : mobileArea;
|
|
|
|
|
+
|
|
|
|
|
+ // 校验手机号
|
|
|
|
|
+ if (Const.CONTINENT.equals(mobileArea)) {
|
|
|
|
|
+ if (!mobile.matches(Const.REGEXP_MOBILE_CONTINENT)) {
|
|
|
|
|
+ throw new VisibleError("请输入正确的手机号格式");
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (Const.HONGKONG.equals(mobileArea)) {
|
|
|
|
|
+ if (!mobile.matches(Const.REGEXP_MOBILE_HONGKONG)) {
|
|
|
|
|
+ throw new VisibleError("请输入正确的手机号格式");
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ throw new VisibleError("未找到所选地区");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|