package com.terra.proxy.controller;
|
|
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONUtil;
|
import com.terra.proxy.bean.CustomerToken;
|
import com.terra.proxy.bean.ServerRgBean;
|
import com.terra.proxy.bean.TokenRecord;
|
import com.terra.proxy.mapper.LogMapper;
|
import com.terra.proxy.properties.TerraProperties;
|
import com.terra.proxy.service.Impl.ServerRegisterServiceImpl;
|
import com.terra.proxy.util.JedisUtils;
|
import com.terra.proxy.util.JwtUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.http.ResponseEntity;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.util.StringUtils;
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
import redis.clients.jedis.Jedis;
|
import springfox.documentation.annotations.ApiIgnore;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.io.UnsupportedEncodingException;
|
import java.net.URI;
|
import java.net.URISyntaxException;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
@Controller
|
@RequestMapping("/serverRegister")
|
@CrossOrigin
|
public class ServiceRegisterController {
|
|
@Autowired
|
JedisUtils jedisUtils;
|
|
@Autowired
|
JwtUtils JwtUtils;
|
|
@Autowired
|
ServerRegisterServiceImpl serverRegisterService;
|
|
@Autowired
|
LogMapper logMapper;
|
|
@Value("${sys.allowedips}")
|
private String allowedips;
|
@Value("${sys.jwt.expire}")
|
private Long expireSeconds;
|
|
@Value("${sys.visitname}")
|
private String visitname;
|
@Value("${sys.visitpwd}")
|
private String visitpwd;
|
|
private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
|
|
private String salt = "abcde";
|
|
private String pass = "HYFW";
|
@Autowired
|
private TerraProperties properties;
|
|
/**
|
* Arcgis Server 服务代理注册
|
* @param request
|
* @param zyid
|
* @param serverUrl
|
* @param contextPath
|
* @param urlid
|
* @param isPublic
|
* @param suffix
|
* @param fromsys
|
* @param servername
|
* @param enable
|
* @return
|
*/
|
@RequestMapping(value = "/addArcGisServer", method = RequestMethod.GET)
|
@ResponseBody
|
public String addArcgisServer(HttpServletRequest request, Integer zyid, String serverUrl,
|
String contextPath, Integer urlid, Integer isPublic, Integer suffix, String fromsys, String servername, Integer enable) {
|
ServerRgBean serverRgBean = new ServerRgBean(serverUrl, zyid, isPublic, urlid, contextPath, suffix, fromsys, servername, enable != null ? enable : 1);
|
String servertype = judgeServerType(serverUrl);
|
serverRegisterService.addServer(serverRgBean);
|
JSONObject json = new JSONObject();
|
json.put("id", serverRgBean.getId());
|
json.put("result", 0);
|
String proxyurl = "";
|
if (!StringUtils.isEmpty(suffix)) {
|
proxyurl = new StringBuilder(properties.getProxy().getHost()).append("/arcgis/rest/services/").append(zyid).append("/").append(suffix.toString()).append("/").append(servertype).toString();
|
} else {
|
proxyurl = new StringBuilder(properties.getProxy().getHost()).append("/arcgis/rest/services/").append(zyid).append("/").append(servertype).toString();
|
}
|
json.put("proxyurl", proxyurl);
|
return json.toJSONString(0);
|
}
|
|
public static String judgeServerType(String serverUrl) {
|
//获取服务的path,按斜杠切割成数组
|
String type = null;
|
try {
|
URI uri = new URI(serverUrl);
|
String path = uri.getPath();
|
String[] t = path.split("/");
|
if (isdigit(t[t.length - 1])) {
|
type = t[t.length - 2];
|
} else {
|
type = t[t.length - 1];
|
}
|
System.out.print(path);
|
} catch (URISyntaxException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
return type;
|
}
|
|
public static boolean isdigit(String str) {
|
|
return str.matches("[0-9]+");
|
}
|
|
public static void main(String[] args) {
|
String serverUrl = "http://18.1.1.55:6080/arcgis/rest/services/BaseMap_Remote_WGS1984_Mercator2016/ImageServer";
|
judgeServerType(serverUrl);
|
}
|
|
|
/**
|
* Arcgis Server 服务代理更新
|
* @param request
|
* @param zyid
|
* @param serverUrl
|
* @param contextPath
|
* @param urlid
|
* @param isPublic
|
* @param suffix
|
* @param id
|
* @param fromsys
|
* @param serverName
|
* @param enable
|
* @return
|
*/
|
@RequestMapping(value = "/updateArcGisServer", method = RequestMethod.GET)
|
@ApiIgnore
|
@ResponseBody
|
public String updateArcgisServer(HttpServletRequest request, Integer zyid, String serverUrl,
|
String contextPath, Integer urlid, Integer isPublic, Integer suffix, Integer id, String fromsys, String serverName, Integer enable) {
|
Map<String, Object> params = new HashMap<>();
|
// 获取源服务地址,上下文服务地址 ,资源id,将其持久化入库,并返回服务id
|
params.put("serverurl", serverUrl);
|
params.put("servername", serverName);
|
params.put("resourceid", zyid);
|
params.put("suffix", suffix);
|
params.put("ispublic", isPublic);
|
params.put("urlid", urlid);
|
params.put("enable", enable);
|
params.put("id", id);
|
int result = serverRegisterService.updateServer(params);
|
if (result > 0) {
|
JSONObject json = new JSONObject();
|
json.put("id", id);
|
json.put("result", 0);
|
return json.toJSONString(0);
|
}
|
return "error";
|
}
|
|
|
/**
|
* ArcgisServer服务删除
|
* @param id
|
* @return
|
*/
|
@RequestMapping(value = "/delArcGisServer", method = RequestMethod.GET)
|
@ApiIgnore
|
public ResponseEntity<String> delArcGisServer(Integer id) {
|
|
int result = serverRegisterService.delServer(id);
|
if (result > 0) {
|
return new ResponseEntity<>("删除服务成功", HttpStatus.OK);
|
}
|
return new ResponseEntity<>("删除服务失败", HttpStatus.FAILED_DEPENDENCY);
|
}
|
|
|
@RequestMapping(value = "/genToken", method = RequestMethod.GET)
|
@ResponseBody
|
public String genToken(String userid, String password, String resourceid, HttpServletRequest req, Long expiration, String identyinfo, String appUrl, Boolean isPubzy, String appId, String applyuserid) {
|
if (expiration == null || expiration == 0) expiration = this.expireSeconds;
|
JSONObject json = new JSONObject();
|
|
if (userid == null || userid.isEmpty() || password == null || password.isEmpty()) {
|
json.put("code", 403);
|
json.put("data", null);
|
json.put("message", "用户名不能为空");
|
return json.toJSONString(0);
|
}
|
if (!userid.equals(visitname) || !password.equals(visitpwd)) {
|
json.put("code", 401);
|
json.put("data", null);
|
json.put("message", "用户名密码不正确");
|
return json.toJSONString(0);
|
}
|
if (!resourceid.matches("[0-9]+")) {
|
json.put("code", 401);
|
json.put("data", null);
|
json.put("message", "资源id格式不正确");
|
return json.toJSONString(0);
|
}
|
|
Jedis jedis = jedisUtils.getJedis();
|
try {
|
CustomerToken cutToken = new CustomerToken();
|
cutToken.setResourceId(resourceid);
|
cutToken.setUserid(applyuserid == null ? "0" : applyuserid);
|
cutToken.setAppUrl(appUrl == null ? "" : appUrl);
|
cutToken.setAppId(appId == null ? "0" : appUrl);
|
cutToken.setClientIp(req.getRemoteHost());
|
cutToken.setRemark(identyinfo == null ? "" : identyinfo);
|
String token = expiration != null ? JwtUtils.generateToken(cutToken.toString(), expiration) : JwtUtils.generateToken(cutToken.toString(), expireSeconds);
|
TokenRecord record = new TokenRecord();
|
record.setApplytime(new Date());
|
record.setExpiration(expiration != null ? expiration.intValue() : expireSeconds.intValue());
|
record.setIdentyinfo(identyinfo);
|
record.setTokeninfo(cutToken.toString());
|
if (!StringUtils.isEmpty(resourceid)) {
|
record.setResourceid(Integer.valueOf(resourceid));
|
}
|
record.setToken(token);
|
record.setTokenapplyer(req.getRemoteHost());
|
logMapper.saveTokenRecord(record);
|
jedis.setex("zytoken:" + token, expiration != null ? expiration.intValue() : expireSeconds.intValue(), "1");
|
json.put("code", 200);
|
json.put("data", token);
|
json.put("message", "获取成功");
|
return json.toJSONString(0);
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
} finally {
|
jedis.close();
|
}
|
return "error";
|
}
|
|
|
/**
|
* 获取token记录
|
* @param token
|
* @param identyinfo
|
* @return
|
*/
|
@RequestMapping(value = "/getTokenRecord", method = RequestMethod.GET)
|
@ResponseBody
|
public String getTokenRecord(String token, String identyinfo) {
|
Map<String, Object> map = new HashMap<>();
|
map.put("token", token);
|
map.put("identyinfo", identyinfo);
|
List<Map> list = logMapper.selectTokenRecord(map);
|
return JSONUtil.toJsonStr(list);
|
|
}
|
|
|
/**
|
* 应用系统申请token,时间传空,则使用各自默认的token时效值
|
*
|
* @param appId
|
* @param req
|
* @param expiration
|
* @return
|
*/
|
@RequestMapping(value = "/genAppToken", method = RequestMethod.GET)
|
@ResponseBody
|
public String genAppToken(Integer appId, HttpServletRequest req, Long expiration) {
|
JSONObject json = new JSONObject();
|
if (appId == null || appId == 0) {
|
json.put("code", 403);
|
json.put("data", null);
|
json.put("message", "应用系统id不能为空");
|
return json.toJSONString(0);
|
}
|
Jedis jedis = jedisUtils.getJedis();
|
try {
|
|
String token = JwtUtils.generateToken(expiration, appId);
|
jedis.set(token, token);
|
jedis.expire(token, expiration != null ? expiration.intValue() : expireSeconds.intValue());
|
json.put("code", 200);
|
json.put("data", token);
|
json.put("message", "获取成功");
|
return json.toJSONString(0);
|
} catch (Exception e) {
|
e.printStackTrace();
|
|
} finally {
|
jedis.close();
|
}
|
|
return "error";
|
}
|
|
|
@RequestMapping(value = "/genTokenByUserid", method = RequestMethod.GET)
|
@ResponseBody
|
public String genTokenForJGPT(Integer userid, HttpServletRequest req, Long expiration) {
|
System.out.println(allowedips);
|
JSONObject json = new JSONObject();
|
|
Jedis jedis = jedisUtils.getJedis();
|
try {
|
String token = expiration != null ? JwtUtils.generateToken(userid, expiration) : JwtUtils.generateToken(userid, expireSeconds);
|
jedis.set(token, token);
|
jedis.expire(token, expiration != null ? expiration.intValue() : expireSeconds.intValue());
|
return token;
|
} catch (Exception e) {
|
e.printStackTrace();
|
|
} finally {
|
jedis.close();
|
}
|
return "error";
|
}
|
|
@RequestMapping(value = "/queryServer")
|
@ApiIgnore
|
@ResponseBody
|
public String queryServer(HttpServletRequest request, Integer zyid, String serverUrl,
|
String contextPath, Integer urlid, Integer isPublic, Integer suffix, String fromsys, Integer page, Integer limit, String servername) {
|
Map<String, Object> params = new HashMap<>();
|
Map<String, Object> resultmap = new HashMap<>();
|
// 获取源服务地址,上下文服务地址 ,资源id,将其持久化入库,并返回服务id
|
params.put("serverurl", serverUrl);
|
params.put("resourceid", zyid);
|
params.put("contextPath", contextPath);
|
params.put("serverUrl", serverUrl);
|
params.put("suffix", suffix);
|
params.put("ispublic", isPublic);
|
params.put("urlid", urlid);
|
params.put("fromsys", fromsys);
|
params.put("offSet", (page - 1) * limit);
|
params.put("limit", limit);
|
params.put("servername", servername);
|
List<Map> list = serverRegisterService.queryServer(params);
|
int total = serverRegisterService.queryTotalServer(params);
|
resultmap.put("data", list);
|
resultmap.put("totalpage", Math.ceil((double) total / (double) limit));
|
resultmap.put("total", total);
|
String str = JSONUtil.toJsonStr(resultmap);
|
return str;
|
}
|
|
@RequestMapping(value = "/queryServer2", method = RequestMethod.POST)
|
@ApiIgnore
|
@ResponseBody
|
public String queryServer2(HttpServletRequest request, Integer zyid, String serverUrl,
|
String contextPath, Integer urlid, Integer isPublic, String fromsys, Integer enable) {
|
Map<String, Object> params = new HashMap<>();
|
// 获取源服务地址,上下文服务地址 ,资源id,将其持久化入库,并返回服务id
|
params.put("serverurl", serverUrl);
|
params.put("resourceid", zyid);
|
params.put("contextPath", contextPath);
|
params.put("serverUrl", serverUrl);
|
params.put("ispublic", isPublic);
|
params.put("urlid", urlid);
|
params.put("enable", enable);
|
params.put("fromsys", fromsys);
|
List<Map> list = serverRegisterService.queryServer2(params);
|
return list.toString();
|
}
|
|
@RequestMapping(value = "/getProxyUrl", method = RequestMethod.POST)
|
@ApiIgnore
|
@ResponseBody
|
public String getProxyUrl(HttpServletRequest request, String url) {
|
Map<String, Object> params = new HashMap<>();
|
// 获取源服务地址,上下文服务地址 ,资源id,将其持久化入库,并返回服务id
|
URI uri = null;
|
try {
|
uri = new URI(url);
|
} catch (URISyntaxException e1) {
|
// TODO Auto-generated catch block
|
e1.printStackTrace();
|
}
|
String path = uri.getPath();
|
String param = path;
|
path = path.replace("arcgis/rest/services/", "");
|
param = param.replace("arcgis/rest/services/", "");
|
path = path.substring(0, path.indexOf("MapServer"));
|
param = param.substring(param.indexOf("/MapServer"));
|
String result = "";
|
try {
|
result = byteArrayToHexString(path.getBytes("utf-8"));
|
} catch (UnsupportedEncodingException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
|
StringBuilder serverurl = new StringBuilder(properties.getProxy().getHost());
|
serverurl.append("/server/").append(result).append(param);
|
return serverurl.toString();
|
}
|
|
private String mergePathAndSalt(String path) {
|
|
if (path == null) {
|
path = "";
|
} else {
|
path = path + salt;
|
}
|
|
return path;
|
|
}
|
|
private static String byteArrayToHexString(byte[] b) {
|
StringBuffer resultsb = new StringBuffer();
|
for (int i = 0; i < b.length; i++) {
|
resultsb.append(byteToHexString(b[i]));
|
}
|
return resultsb.toString();
|
}
|
|
private static String byteToHexString(byte b) {
|
String hex = Integer.toHexString(b & 0xff);
|
if (hex.length() < 2) {
|
hex = "0" + hex;
|
}
|
return hex;
|
}
|
|
|
}
|