5 changed files with 119 additions and 9 deletions
@ -0,0 +1,112 @@ |
|||||
|
package com.dreamchaser.depository_manage.controller; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import com.dreamchaser.depository_manage.config.PublicConfig; |
||||
|
import com.dreamchaser.depository_manage.entity.UserByPort; |
||||
|
import com.dreamchaser.depository_manage.security.pool.AuthenticationTokenPool; |
||||
|
import com.dreamchaser.depository_manage.security.pool.RedisPool; |
||||
|
import com.dreamchaser.depository_manage.service.UserService; |
||||
|
import com.dreamchaser.depository_manage.utils.EncryptionAlgorithmUtil; |
||||
|
import com.dreamchaser.depository_manage.utils.HttpUtils; |
||||
|
import lombok.extern.java.Log; |
||||
|
import lombok.extern.log4j.Log4j; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.http.protocol.HTTP; |
||||
|
import org.apache.ibatis.annotations.Param; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.web.bind.annotation.GetMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
import org.springframework.web.servlet.ModelAndView; |
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import javax.servlet.http.HttpServletResponse; |
||||
|
import java.io.IOException; |
||||
|
import java.time.Instant; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
|
||||
|
@Slf4j |
||||
|
@RestController |
||||
|
public class IdentityVerifyController { |
||||
|
|
||||
|
|
||||
|
@Autowired |
||||
|
private RedisPool redisPool; |
||||
|
|
||||
|
@Autowired |
||||
|
private UserService userService; |
||||
|
|
||||
|
/** |
||||
|
* 用于验证用户身份 |
||||
|
*/ |
||||
|
@GetMapping("/identityVerify") |
||||
|
public ModelAndView identityVerify(@RequestParam Map<String,String> map, HttpServletRequest request, HttpServletResponse response){ |
||||
|
|
||||
|
ModelAndView mv = new ModelAndView(); |
||||
|
mv.addObject("userWxId", ""); |
||||
|
mv.setViewName("pages/user/login"); |
||||
|
|
||||
|
String url = PublicConfig.external_url_extranet+"/signcode/signcode"; |
||||
|
|
||||
|
String userkey = map.get("userkey"); |
||||
|
String usertoken = map.get("usertoken"); |
||||
|
String code = map.get("code"); |
||||
|
EncryptionAlgorithmUtil encryptionAlgorithmUtil = new EncryptionAlgorithmUtil(code); |
||||
|
// 获取加密后的字符串
|
||||
|
String algorithm = encryptionAlgorithmUtil.Algorithm(); |
||||
|
Map<String,String> paramForPost = new HashMap<>(); |
||||
|
paramForPost.put("code",algorithm); |
||||
|
String jsonString = JSONObject.toJSONString(paramForPost); |
||||
|
JSONObject paramObject = JSONObject.parseObject(jsonString); |
||||
|
try { |
||||
|
String send = HttpUtils.send(url, paramObject, HTTP.UTF_8, userkey, usertoken); |
||||
|
JSONObject resultObj = JSONObject.parseObject(send); |
||||
|
int resultCode = resultObj.getInteger("code"); |
||||
|
if(resultCode == 0){ |
||||
|
// 如果验证成功
|
||||
|
String redisKey = PublicConfig.LoginRedisPrefix + userkey; |
||||
|
Object usernumber = redisPool.getRedisTemplateByDb(5).opsForHash().get(redisKey, "usernumber"); |
||||
|
if(usernumber != null){ |
||||
|
UserByPort userByNumber = userService.findUserByNumber(String.valueOf(usernumber)); |
||||
|
|
||||
|
// 设置放入时间
|
||||
|
userByNumber.setInstant(Instant.now()); |
||||
|
// 将登录用户存储到池中保存
|
||||
|
AuthenticationTokenPool.addUserToken(usertoken,userByNumber); |
||||
|
|
||||
|
// 将key与token设置到session中
|
||||
|
request.getSession().setAttribute("userKey", userkey); |
||||
|
request.getSession().setAttribute("userToken", usertoken); |
||||
|
request.getSession().setMaxInactiveInterval(3 * 60 * 60); |
||||
|
|
||||
|
|
||||
|
try { |
||||
|
response.sendRedirect("http://localhost:11111"); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
}else{ |
||||
|
JSONObject captcha = PublicConfig.Captcha(request); |
||||
|
String picPath = (String) captcha.get("picPath"); |
||||
|
String captchaid = (String) captcha.get("captchaid"); |
||||
|
mv.addObject("picPath", picPath); |
||||
|
mv.addObject("captchaid", captchaid); |
||||
|
} |
||||
|
}else{ |
||||
|
// 如果验证失败
|
||||
|
JSONObject captcha = PublicConfig.Captcha(request); |
||||
|
String picPath = (String) captcha.get("picPath"); |
||||
|
String captchaid = (String) captcha.get("captchaid"); |
||||
|
mv.addObject("picPath", picPath); |
||||
|
mv.addObject("captchaid", captchaid); |
||||
|
} |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return mv; |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue