package com.changhong.epc.zuul.rest;
|
|
import com.changhong.epc.constter.base.BaseConst;
|
import com.changhong.epc.constter.system.PrivateRestConst;
|
import com.changhong.epc.constter.system.UserInfo;
|
import com.changhong.epc.constter.system.UserInfo.LogInInfo;
|
import com.changhong.epc.constter.system.prop.ChangHongProperties;
|
import com.changhong.epc.rely.api.service.UserService;
|
import com.changhong.epc.rely.api.service.impl.UserServiceImpl;
|
import com.changhong.epc.zuul.filter.Des1;
|
import com.changhong.epc.zuul.service.LoginService;
|
import com.iemsoft.framework.cloud.core.thread.ThreadData;
|
import com.iemsoft.framework.cloud.core.tools.JSONTool;
|
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
|
import com.iemsoft.framework.cloud.redis.service.CacheUtils;
|
import lombok.AllArgsConstructor;
|
import lombok.Data;
|
import lombok.NoArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.http.client.utils.URIBuilder;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.util.WebUtils;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.Cookie;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
import java.net.URLDecoder;
|
import java.net.URLEncoder;
|
import java.util.Objects;
|
|
@Slf4j
|
@RestController
|
public class LoginRest implements PrivateRestConst{
|
|
@Autowired
|
private ChangHongProperties changHongProperties;
|
|
@Resource
|
private UserServiceImpl.UserInfoServiceImpl userInfoService;
|
|
@Resource
|
private LoginService loginService;
|
|
@GetMapping("/login")
|
public void login(@RequestParam String permType, HttpServletRequest request, HttpServletResponse response) throws Exception{
|
if(WebUtils.getCookie(request, TOKEN) == null){
|
// System.err.println("LoginRest kongde ");
|
return;
|
}
|
/*
|
* 获得登录信息
|
*/
|
UserInfo.LogInInfo loginInfo=new LogInInfo();
|
loginInfo.setToken(WebUtils.getCookie(request, TOKEN).getValue());
|
loginInfo.setOpenId(WebUtils.getCookie(request, OPEN_ID).getValue());
|
// System.out.println("进入:"+JSONTool.toJson(loginInfo));
|
UserInfo user = loginService.getUserInfo(loginInfo);
|
log.debug("当前用户:"+JSONTool.toJson(user));
|
/*
|
* 跳转默认页
|
*/
|
response.sendRedirect(
|
new URIBuilder(changHongProperties.getLoginCallBack())
|
.setParameter("permType", permType)
|
.toString()
|
);
|
|
}
|
|
@PostMapping("/token/validate")
|
public ResultReport tokenValidate(UserInfo.LogInInfo logInInfo){
|
String key = String.format("token_%s"
|
,logInInfo.getOpenId());
|
if(!Objects.equals(CacheUtils.get(key, String.class), logInInfo.getToken())){
|
return new ResultReport();
|
}
|
return new ResultReport(1, true);
|
}
|
|
@Resource
|
private UserService userService;
|
|
@PostMapping("/getUserInfo")
|
public UserInfo getUserInfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
|
/*
|
* 获得登录信息
|
*/
|
UserInfo.LogInInfo loginInfo=new LogInInfo();
|
String token = URLDecoder.decode(WebUtils.getCookie(request, TOKEN).getValue(),"UTF-8").replaceAll(" ","+");
|
loginInfo.setToken(Des1.desDecrypt(token,Des1.SECRETKEY));
|
String openId = URLDecoder.decode(WebUtils.getCookie(request, OPEN_ID).getValue(),"UTF-8").replaceAll(" ","+");
|
loginInfo.setOpenId(Des1.desDecrypt(openId, Des1.SECRETKEY));
|
|
ThreadData.set(BaseConst.TOKEN, token);
|
ThreadData.set(BaseConst.OPEN_ID, openId);
|
|
// System.out.println("进入:"+JSONTool.toJson(loginInfo));
|
log.debug("开始添加cookie缓存,有效期1800秒");
|
String key = String.format("token_%s"
|
,loginInfo.getOpenId());
|
log.debug("当前缓存中登陆token:"+key);
|
String time = SpringUtil.getBean(ChangHongProperties.class).getTokenTimeOut();
|
log.debug("当前超时时间:"+time);
|
CacheUtils.set(key, loginInfo.getToken(), Long.parseLong(time));
|
// System.out.println("刚存得cookie:"+CacheUtils.get(key,String.class));
|
//加密cookie
|
Cookie tokenC = new Cookie(TOKEN, URLEncoder.encode(Des1.desEncryp(loginInfo.getToken(), Des1.SECRETKEY), "UTF-8"));
|
log.debug("加密后token:"+Des1.desEncryp(loginInfo.getToken(), Des1.SECRETKEY));
|
tokenC.setPath("/");
|
tokenC.setMaxAge(-1);
|
Cookie openIdC = new Cookie(OPEN_ID, URLEncoder.encode(Des1.desEncryp(loginInfo.getOpenId(), Des1.SECRETKEY), "UTF-8"));
|
log.debug("加密后openId:"+Des1.desEncryp(loginInfo.getOpenId(), Des1.SECRETKEY));
|
openIdC.setPath("/");
|
openIdC.setMaxAge(-1);
|
response.addCookie(tokenC);
|
response.addCookie(openIdC);
|
return loginService.getUserInfo(loginInfo);
|
|
}
|
|
|
@GetMapping("/")
|
public void locationLogin(HttpServletResponse httpServletResponse)throws IOException {
|
httpServletResponse.sendRedirect(changHongProperties.getHomeUrl());
|
}
|
|
|
@Data
|
@AllArgsConstructor
|
@NoArgsConstructor
|
public static class ResultReport{
|
|
private Integer userId;
|
|
private Boolean display;
|
|
}
|
|
}
|