package com.vci.web.controller;
|
|
import com.vci.bo.LoginResultBO;
|
import com.vci.corba.common.PLException;
|
import com.vci.dto.LoginUserDTO;
|
import com.vci.starter.web.annotation.controller.VciUnCheckRight;
|
import com.vci.starter.web.annotation.log.VciBusinessLog;
|
import com.vci.starter.web.constant.TokenKeyConstant;
|
import com.vci.starter.web.pagemodel.BaseResult;
|
import com.vci.starter.web.pagemodel.RequestClientInfo;
|
import com.vci.starter.web.pagemodel.SessionInfo;
|
import com.vci.starter.web.util.MessageUtils;
|
import com.vci.starter.web.util.VciBaseUtil;
|
import com.vci.starter.web.util.WebThreadLocalUtil;
|
import com.vci.web.service.OsLoginServiceI;
|
import com.zeroc.IceInternal.Ex;
|
import eu.bitwalker.useragentutils.*;
|
import org.apache.commons.lang3.StringUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.util.Locale;
|
import java.util.Map;
|
|
/**
|
* 登录控制器
|
* @author weidy
|
* @date 2021-1-28
|
*/
|
@Controller
|
@RequestMapping("/framework/loginController")
|
@VciBusinessLog(modelName="登录服务")
|
public class OsLoginController {
|
|
/**
|
* 登录服务
|
*/
|
@Autowired
|
private OsLoginServiceI loginService;
|
|
/**
|
* 日志
|
*/
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
/**
|
* 登录,这个地方主要是为了登录后单独的业务
|
* @param userDTO 用户的对象
|
* @param request 请求对象
|
* @param clientInfo 客户端的信息
|
* @return 执行结果
|
*/
|
@VciBusinessLog(operateName="登录")
|
@PostMapping(value = "/login")
|
@ResponseBody
|
@VciUnCheckRight()
|
public BaseResult login(LoginUserDTO userDTO, HttpServletRequest request, RequestClientInfo clientInfo){
|
VciBaseUtil.alertNotNull(userDTO,"用户对象",clientInfo,"请求客户端信息");
|
try {
|
wrapperBrowserInfo(clientInfo,request);
|
LoginResultBO loginResultBO = loginService.login(userDTO,clientInfo);
|
if(loginResultBO.isSuccess()){
|
return BaseResult.success(loginResultBO);
|
}else{
|
BaseResult result = BaseResult.fail(loginResultBO.getFailMsg());
|
result.setCode(200);//状态码不是200前端无法获取到obj中的信息
|
if(StringUtils.isBlank(loginResultBO.getFailMsg())){
|
result.setMsg(MessageUtils.get(loginResultBO.getFailCode(),loginResultBO.getFailMsgArray()));
|
}
|
result.setObj(loginResultBO);
|
return result;
|
}
|
}catch (Exception e){
|
e.printStackTrace();
|
String msg = "调用登录方法时出现错误,原因:"+VciBaseUtil.getExceptionMessage(e);
|
logger.error(msg);
|
return BaseResult.fail(msg);
|
}
|
}
|
|
/**
|
* 获取客户端请求信息,为了隔绝在server层使用request
|
* @param request 请求对象
|
* @param clientInfo 客户端信息
|
*/
|
private void wrapperBrowserInfo(RequestClientInfo clientInfo,HttpServletRequest request) {
|
UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
|
if(StringUtils.isBlank(clientInfo.getIpaddress())){
|
//找IP地址
|
clientInfo.setIpaddress(getIpAddressFromRequest(request));
|
}
|
if(userAgent !=null) {
|
Browser browser = userAgent.getBrowser();
|
OperatingSystem os = userAgent.getOperatingSystem();
|
|
clientInfo.setOsversion(os != null ? os.getName() : "");
|
clientInfo.setBrowser(browser != null ? browser.getName() : "IE");
|
String version = "";
|
if (browser != null) {
|
Version version1 = browser.getVersion(request.getHeader("User-Agent"));
|
if (version1 != null) {
|
version = version1.getVersion();
|
}
|
}
|
clientInfo.setBrowserversion(version);
|
if (os != null) {
|
clientInfo.setRequestType(os.getDeviceType().getName());
|
if (DeviceType.COMPUTER.getName().equals(clientInfo.getRequestType())) {
|
clientInfo.setRequestType("browser");
|
}
|
}
|
|
Locale loc = Locale.getDefault();
|
clientInfo.setCountry(loc.getCountry());
|
clientInfo.setLanguage(loc.toLanguageTag());
|
|
Map<String,String> map = System.getenv();
|
clientInfo.setMachine(map.get("COMPUTERNAME"));
|
clientInfo.setOsUser(map.get("USERNAME"));
|
}
|
}
|
|
/**
|
* 从请求中获取ip地址,为了隔绝在server层使用request
|
* @param request 请求对象
|
* @return ip地址,没有找到默认为127.0.0.1
|
*/
|
private String getIpAddressFromRequest(HttpServletRequest request){
|
String ip = request.getHeader("X-Forwarded-For");
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("Proxy-Client-IP");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("WL-Proxy-Client-IP");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_CLIENT_IP");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getRemoteAddr();
|
}
|
if (ip == null || ip.length() == 0 || ip.indexOf("0:0:0:0:0:0:0:1") >-1) {
|
//0:0:0:0:0:0:0:1是本机在访问
|
ip = "127.0.0.1";
|
}
|
return ip;
|
}
|
|
/**
|
* 获取用户的会话信息
|
* @return success为true表示获取成功,否则msg是错误信息,obj属性是获取的会话对象信息
|
*/
|
@VciUnCheckRight
|
@PostMapping("/getSessionInfo")
|
@ResponseBody
|
public BaseResult getSessionInfo(){
|
BaseResult<SessionInfo> json = new BaseResult<>();
|
SessionInfo sessionInfo = WebThreadLocalUtil.getCurrentUserSessionInfoInThread();
|
if(sessionInfo != null){
|
json = json.success(sessionInfo);
|
}
|
return json;
|
}
|
|
/**
|
* 执行退出
|
* @param request 请求对象
|
* @return success为true表示退出成功,前端不需要判断结果
|
*/
|
@VciUnCheckRight
|
@PostMapping("/logout")
|
@ResponseBody
|
public BaseResult logout(HttpServletRequest request){
|
String userToken = request.getHeader(TokenKeyConstant.USER_TOKEN_KEY);
|
try {
|
loginService.logout(userToken);
|
return BaseResult.success("退出成功");
|
} catch (Exception e) {
|
e.printStackTrace();
|
String errorLog = "登出时出现异常,原因:"+ VciBaseUtil.getExceptionMessage(e);
|
logger.error(errorLog);
|
return BaseResult.fail(errorLog);
|
}
|
}
|
|
}
|