|
@@ -1,9 +1,12 @@
|
|
|
package com.jpsoft.employment.modules.common.controller;
|
|
package com.jpsoft.employment.modules.common.controller;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+import com.github.pagehelper.util.StringUtil;
|
|
|
import com.jpsoft.employment.modules.common.dto.MessageResult;
|
|
import com.jpsoft.employment.modules.common.dto.MessageResult;
|
|
|
|
|
+import com.jpsoft.employment.modules.common.service.SmsService;
|
|
|
import com.jpsoft.employment.modules.common.utils.DES3;
|
|
import com.jpsoft.employment.modules.common.utils.DES3;
|
|
|
import com.jpsoft.employment.modules.common.utils.JwtUtil;
|
|
import com.jpsoft.employment.modules.common.utils.JwtUtil;
|
|
|
|
|
+import com.jpsoft.employment.modules.common.utils.SMSUtil;
|
|
|
import com.jpsoft.employment.modules.sys.entity.User;
|
|
import com.jpsoft.employment.modules.sys.entity.User;
|
|
|
import com.jpsoft.employment.modules.sys.service.UserService;
|
|
import com.jpsoft.employment.modules.sys.service.UserService;
|
|
|
import io.swagger.annotations.ApiImplicitParam;
|
|
import io.swagger.annotations.ApiImplicitParam;
|
|
@@ -15,11 +18,13 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
import org.joda.time.DateTime;
|
|
import org.joda.time.DateTime;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.data.redis.core.ValueOperations;
|
|
import org.springframework.data.redis.core.ValueOperations;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import springfox.documentation.annotations.ApiIgnore;
|
|
import springfox.documentation.annotations.ApiIgnore;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpSession;
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@@ -32,38 +37,46 @@ public class JwtsUserController {
|
|
|
private UserService userService;
|
|
private UserService userService;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
- private ValueOperations<String,Object> valueOperations;
|
|
|
|
|
|
|
+ private SmsService smsService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private RedisTemplate<String, String> redisTemplate;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private ValueOperations<String, Object> valueOperations;
|
|
|
|
|
+
|
|
|
|
|
+ private HashMap<String, Long> captchaStore = new HashMap<>();
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 接收扫码登录回调
|
|
* 接收扫码登录回调
|
|
|
|
|
+ *
|
|
|
* @param eventKey
|
|
* @param eventKey
|
|
|
* @param openId
|
|
* @param openId
|
|
|
* @return 返回值会在微信中显示
|
|
* @return 返回值会在微信中显示
|
|
|
*/
|
|
*/
|
|
|
@ApiOperation(value = "接收扫码登录回调")
|
|
@ApiOperation(value = "接收扫码登录回调")
|
|
|
- @PostMapping(value="/qrcode/scanLogin")
|
|
|
|
|
|
|
+ @PostMapping(value = "/qrcode/scanLogin")
|
|
|
@ResponseBody
|
|
@ResponseBody
|
|
|
- public MessageResult<String> scanLogin(String eventKey, String openId){
|
|
|
|
|
|
|
+ public MessageResult<String> scanLogin(String eventKey, String openId) {
|
|
|
MessageResult<String> messageResult = new MessageResult<>();
|
|
MessageResult<String> messageResult = new MessageResult<>();
|
|
|
log.warn(openId + "请求登录!");
|
|
log.warn(openId + "请求登录!");
|
|
|
String result;
|
|
String result;
|
|
|
|
|
|
|
|
User user = userService.findByOpenId(openId);
|
|
User user = userService.findByOpenId(openId);
|
|
|
|
|
|
|
|
- if(user!=null) {
|
|
|
|
|
|
|
+ if (user != null) {
|
|
|
String[] arr = eventKey.split(",");
|
|
String[] arr = eventKey.split(",");
|
|
|
String randNum = arr[1];
|
|
String randNum = arr[1];
|
|
|
|
|
|
|
|
long expireSeconds = 3000; //5分钟
|
|
long expireSeconds = 3000; //5分钟
|
|
|
|
|
|
|
|
//生成token
|
|
//生成token
|
|
|
- String token = JwtUtil.createToken(jwtSecret,user.getId(), DateTime.now().plusHours(6).toDate());
|
|
|
|
|
|
|
+ String token = JwtUtil.createToken(jwtSecret, user.getId(), DateTime.now().plusHours(6).toDate());
|
|
|
|
|
|
|
|
valueOperations.set("scan_qrcode_login_" + randNum, token, expireSeconds, TimeUnit.SECONDS);
|
|
valueOperations.set("scan_qrcode_login_" + randNum, token, expireSeconds, TimeUnit.SECONDS);
|
|
|
messageResult.setMessage("扫码登录成功!");
|
|
messageResult.setMessage("扫码登录成功!");
|
|
|
messageResult.setResult(true);
|
|
messageResult.setResult(true);
|
|
|
- }
|
|
|
|
|
- else{
|
|
|
|
|
|
|
+ } else {
|
|
|
messageResult.setMessage("当前用户未绑定微信!");
|
|
messageResult.setMessage("当前用户未绑定微信!");
|
|
|
messageResult.setResult(false);
|
|
messageResult.setResult(false);
|
|
|
}
|
|
}
|
|
@@ -72,24 +85,22 @@ public class JwtsUserController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/qrcode/queryScanResult")
|
|
@PostMapping("/qrcode/queryScanResult")
|
|
|
- @ApiOperation(value="查询扫码结果")
|
|
|
|
|
- public MessageResult<String> queryScanResult(String rnd){
|
|
|
|
|
|
|
+ @ApiOperation(value = "查询扫码结果")
|
|
|
|
|
+ public MessageResult<String> queryScanResult(String rnd) {
|
|
|
MessageResult<String> messageResult = new MessageResult<>();
|
|
MessageResult<String> messageResult = new MessageResult<>();
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- String data = (String)valueOperations.get("scan_qrcode_login_" + rnd);
|
|
|
|
|
|
|
+ String data = (String) valueOperations.get("scan_qrcode_login_" + rnd);
|
|
|
|
|
|
|
|
- if(StringUtils.isNotEmpty(data)) {
|
|
|
|
|
|
|
+ if (StringUtils.isNotEmpty(data)) {
|
|
|
messageResult.setData(data);
|
|
messageResult.setData(data);
|
|
|
messageResult.setResult(true);
|
|
messageResult.setResult(true);
|
|
|
- }
|
|
|
|
|
- else{
|
|
|
|
|
|
|
+ } else {
|
|
|
messageResult.setResult(false);
|
|
messageResult.setResult(false);
|
|
|
messageResult.setMessage("暂无扫码结果");
|
|
messageResult.setMessage("暂无扫码结果");
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- catch (Exception ex){
|
|
|
|
|
- log.error(ex.getMessage(),ex);
|
|
|
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
|
+ log.error(ex.getMessage(), ex);
|
|
|
|
|
|
|
|
messageResult.setResult(false);
|
|
messageResult.setResult(false);
|
|
|
messageResult.setMessage(ex.getMessage());
|
|
messageResult.setMessage(ex.getMessage());
|
|
@@ -98,39 +109,53 @@ public class JwtsUserController {
|
|
|
return messageResult;
|
|
return messageResult;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
@PostMapping("/login")
|
|
@PostMapping("/login")
|
|
|
- @ApiOperation(value="登录获取token,在swagger ui中获取token时将写入session,调用其它接口时不用再设置header")
|
|
|
|
|
|
|
+ @ApiOperation(value = "登录获取token,在swagger ui中获取token时将写入session,调用其它接口时不用再设置header")
|
|
|
@ApiImplicitParams({
|
|
@ApiImplicitParams({
|
|
|
- @ApiImplicitParam(name="userName", paramType="query", required=true, value="用户名"),
|
|
|
|
|
- @ApiImplicitParam(name="password", paramType="query", required=true, value="密码")
|
|
|
|
|
|
|
+ @ApiImplicitParam(name = "userName", paramType = "query", required = true, value = "用户名"),
|
|
|
|
|
+ @ApiImplicitParam(name = "password", paramType = "query", required = true, value = "密码"),
|
|
|
|
|
+ @ApiImplicitParam(name = "captCha", paramType = "query", required = true, value = "验证码")
|
|
|
})
|
|
})
|
|
|
- public MessageResult<String> login(String userName, String password, @ApiIgnore HttpSession session){
|
|
|
|
|
|
|
+ public MessageResult<String> login(String userName, String password, String captCha, @ApiIgnore HttpSession session) {
|
|
|
MessageResult<String> messageResult = new MessageResult<>();
|
|
MessageResult<String> messageResult = new MessageResult<>();
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
|
|
+
|
|
|
User user = userService.findByUserName(userName);
|
|
User user = userService.findByUserName(userName);
|
|
|
|
|
|
|
|
DES3 des3 = new DES3();
|
|
DES3 des3 = new DES3();
|
|
|
|
|
|
|
|
- String passwordEnc = des3.encrypt(jwtSecret,password);
|
|
|
|
|
|
|
+ String passwordEnc = des3.encrypt(jwtSecret, password);
|
|
|
|
|
|
|
|
- if(user!=null && passwordEnc.equals(user.getPassword())){
|
|
|
|
|
|
|
+ //验证码
|
|
|
|
|
+ Boolean isCodePass = isCaptchaValid(captCha);
|
|
|
|
|
+
|
|
|
|
|
+ if (!isCodePass) {
|
|
|
|
|
+ messageResult.setResult(false);
|
|
|
|
|
+ messageResult.setMessage("验证码不正确或已过期!");
|
|
|
|
|
+ return messageResult;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (user != null && passwordEnc.equals(user.getPassword())) {
|
|
|
|
|
|
|
|
//生成token
|
|
//生成token
|
|
|
- String token = JwtUtil.createToken(jwtSecret,user.getId(),DateTime.now().plusHours(6).toDate());
|
|
|
|
|
|
|
+ String token = JwtUtil.createToken(jwtSecret, user.getId(), DateTime.now().plusHours(6).toDate());
|
|
|
|
|
|
|
|
- session.setAttribute("token",token);
|
|
|
|
|
|
|
+ session.setAttribute("token", token);
|
|
|
|
|
|
|
|
messageResult.setResult(true);
|
|
messageResult.setResult(true);
|
|
|
messageResult.setData(token);
|
|
messageResult.setData(token);
|
|
|
- }
|
|
|
|
|
- else{
|
|
|
|
|
|
|
+
|
|
|
|
|
+ //登录成功,清理使用过的验证码
|
|
|
|
|
+ captchaStore.remove(captCha);
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
messageResult.setResult(false);
|
|
messageResult.setResult(false);
|
|
|
messageResult.setMessage("用户不存在或密码错误!");
|
|
messageResult.setMessage("用户不存在或密码错误!");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- }
|
|
|
|
|
- catch(Exception ex){
|
|
|
|
|
|
|
+ } catch (Exception ex) {
|
|
|
messageResult.setResult(false);
|
|
messageResult.setResult(false);
|
|
|
messageResult.setMessage(ex.getMessage());
|
|
messageResult.setMessage(ex.getMessage());
|
|
|
}
|
|
}
|
|
@@ -139,7 +164,7 @@ public class JwtsUserController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/userInfo")
|
|
@GetMapping("/userInfo")
|
|
|
- public MessageResult<User> userInfo(@RequestAttribute String subject){
|
|
|
|
|
|
|
+ public MessageResult<User> userInfo(@RequestAttribute String subject) {
|
|
|
MessageResult<User> messageResult = new MessageResult<>();
|
|
MessageResult<User> messageResult = new MessageResult<>();
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
@@ -147,12 +172,61 @@ public class JwtsUserController {
|
|
|
|
|
|
|
|
messageResult.setResult(true);
|
|
messageResult.setResult(true);
|
|
|
messageResult.setData(user);
|
|
messageResult.setData(user);
|
|
|
- }
|
|
|
|
|
- catch(Exception ex){
|
|
|
|
|
|
|
+ } catch (Exception ex) {
|
|
|
messageResult.setResult(false);
|
|
messageResult.setResult(false);
|
|
|
messageResult.setMessage(ex.getMessage());
|
|
messageResult.setMessage(ex.getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return messageResult;
|
|
return messageResult;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @PostMapping("/generateCaptCha")
|
|
|
|
|
+ @ApiOperation(value = "生成验证码")
|
|
|
|
|
+ public MessageResult<String> generateCaptCha(String phoneNumber) {
|
|
|
|
|
+ MessageResult<String> messageResult = new MessageResult<>();
|
|
|
|
|
+
|
|
|
|
|
+ String captCha = SMSUtil.getCaptcha();
|
|
|
|
|
+
|
|
|
|
|
+ User user = userService.findByUserName(phoneNumber);
|
|
|
|
|
+
|
|
|
|
|
+ if (user == null) {
|
|
|
|
|
+ messageResult.setResult(false);
|
|
|
|
|
+ messageResult.setMessage("用户不存在!");
|
|
|
|
|
+ }
|
|
|
|
|
+ else{
|
|
|
|
|
+ String phone = user.getPhone();
|
|
|
|
|
+ if(StringUtil.isNotEmpty(phone)){
|
|
|
|
|
+ // 将验证码发送给用户
|
|
|
|
|
+ //boolean success = smsService.sendVerificationCode(phone, captCha);
|
|
|
|
|
+ captchaStore.put(captCha, System.currentTimeMillis());
|
|
|
|
|
+ messageResult.setResult(true);
|
|
|
|
|
+ messageResult.setData(captCha);
|
|
|
|
|
+ }
|
|
|
|
|
+ else{
|
|
|
|
|
+ messageResult.setResult(false);
|
|
|
|
|
+ messageResult.setMessage("此用户名绑定的手机号码不正确或未绑定手机号码!");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return messageResult;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean isCaptchaValid(String captCha) {
|
|
|
|
|
+ Long generatedTime = captchaStore.get(captCha);
|
|
|
|
|
+ long currentTime = System.currentTimeMillis();
|
|
|
|
|
+
|
|
|
|
|
+ if (generatedTime != null && (currentTime - generatedTime) <= TimeUnit.MINUTES.toMillis(5)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void verifyCaptcha(String captCha) {
|
|
|
|
|
+ if (isCaptchaValid(captCha)) {
|
|
|
|
|
+ System.out.print("验证码通过");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ System.out.print("验证码无效或已过期");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|