| | |
| | | import com.moon.server.entity.all.*; |
| | | import com.moon.server.entity.sys.ResEntity; |
| | | import com.moon.server.entity.sys.ResLogEntity; |
| | | import com.moon.server.entity.sys.TokenEntity; |
| | | import com.moon.server.entity.sys.UserEntity; |
| | | import com.moon.server.helper.AsyncHelper; |
| | | import com.moon.server.helper.HttpHelper; |
| | |
| | | import java.util.TimerTask; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * 代理服务类 |
| | | * @author WWW |
| | | * @date 2023-07-11 |
| | | */ |
| | | @Service |
| | | @SuppressWarnings("ALL") |
| | | public class ProxyService { |
| | | @Resource |
| | | RedisService redisService; |
| | |
| | | |
| | | public static final String ILLEGAL_RESOURCE = JSON.toJSONString(new ResponseMsg<String>(HttpStatus.UNAUTHORIZED, "没有资源访问权限")); |
| | | |
| | | /** |
| | | * URL代理 |
| | | */ |
| | | public void proxyUrl(String token, int resId, boolean isRest, HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | // 3.获取用户 |
| | | UserEntity ue = getUser(req, res, token); |
| | | if (null == ue) { |
| | | return; |
| | | } |
| | | // 9.获取资源实体,status:0-禁用,1-启用原始地址,2-启用代理地址 |
| | | |
| | | ResEntity entity = getResEntity(ue, resId); |
| | | if (null == entity || StaticData.I2 != entity.getStatus() || StringHelper.isNull(entity.getProxy()) || StringHelper.isNull(entity.getUrl())) { |
| | | if (null == entity || StaticData.I1 > entity.getStatus() || StringHelper.isNull(entity.getProxy()) || StringHelper.isNull(entity.getUrl())) { |
| | | WebHelper.writeStr2Page(res, ILLEGAL_RESOURCE); |
| | | return; |
| | | } |
| | |
| | | forward(req, res, entity, url); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户 |
| | | */ |
| | | private UserEntity getUser(HttpServletRequest req, HttpServletResponse res, String token) { |
| | | String key = RedisCacheKey.permsProxy(token); |
| | | Object obj = redisService.get(key); |
| | |
| | | return ue; |
| | | } |
| | | |
| | | /** |
| | | * 检查 |
| | | */ |
| | | private boolean check(HttpServletRequest req, HttpServletResponse res, UserEntity ue, String token) { |
| | | // 4.获取IP |
| | | String ip = WebHelper.getIpAddress(req); |
| | | if (StringHelper.isEmpty(ip)) { |
| | | return WebHelper.writeStr2Page(res, AuthInterceptor.IP_NULL); |
| | | } |
| | | // 5.检查黑名单 |
| | | if (!checkBlackList(ip, req)) { |
| | | return WebHelper.writeStr2Page(res, AuthInterceptor.BLACK_LIST); |
| | | } |
| | | // 6.admin跳过权限检测 |
| | | if (StaticData.ADMIN.equals(ue.getUid())) { |
| | | return true; |
| | | } |
| | | // 7.检查白名单和IP一致性 |
| | | if (!checkWhiteList(ip, req)) { |
| | | if (!ip.equals(sysService.tokenService.getEntityByToken(token).getIp())) { |
| | | if (!checkIpSource(ip, token)) { |
| | | return WebHelper.writeStr2Page(res, AuthInterceptor.ILLEGAL_TOKEN); |
| | | } |
| | | } |
| | | // 8.检查用户ID是否禁用 |
| | | if (sysService.tokenService.isUidDisable(ue)) { |
| | | return WebHelper.writeStr2Page(res, AuthInterceptor.USER_LOCK); |
| | | } |
| | |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 检查黑名单 |
| | | */ |
| | | private boolean checkBlackList(String ip, HttpServletRequest request) { |
| | | List<String> blackList = sysService.blacklistService.selectIpList(1); |
| | | if (blackList == null || blackList.isEmpty()) { |
| | |
| | | return !blackList.contains(ip); |
| | | } |
| | | |
| | | /** |
| | | * 检查白名单 |
| | | */ |
| | | private boolean checkWhiteList(String ip, HttpServletRequest request) { |
| | | List<String> whiteList = sysService.blacklistService.selectIpList(2); |
| | | if (whiteList == null || whiteList.isEmpty()) { |
| | |
| | | return whiteList.contains(ip); |
| | | } |
| | | |
| | | /** |
| | | * 检查资源权限 |
| | | */ |
| | | private boolean checkIpSource(String ip, String token) { |
| | | TokenEntity te = sysService.tokenService.getEntityByToken(token); |
| | | |
| | | return StaticData.I1 == te.getType() || te.getIp().equals(ip); |
| | | } |
| | | |
| | | private ResEntity getResEntity(UserEntity ue, int resId) { |
| | | List<ResEntity> rs = StaticData.ADMIN.equals(ue.getUid()) ? permsService.selectAllRes() : permsService.selectRes(ue.getUid()); |
| | | if (null == rs || rs.isEmpty()) { |
| | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 插入日志 |
| | | */ |
| | | private void insertLog(HttpServletRequest req, UserEntity ue, int resId) { |
| | | String ip = WebHelper.getIpAddress(req); |
| | | |
| | |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 获取请求类别 |
| | | */ |
| | | private int getRequestType(String method) { |
| | | // 请求类:1-GET,2-POST,3-PUT,4-DELETE,5-TRACE,6-HEAD,7-OPTIONS,8-CONNECT'; |
| | | switch (method) { |
| | | case "GET": |
| | | return 1; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取Url |
| | | */ |
| | | private String getUrl(HttpServletRequest req, UserEntity ue, ResEntity entity, String token, boolean isRest) { |
| | | String proxyUrl = entity.getProxy().replace("{token}", token); |
| | | int end = req.getRequestURL().indexOf(proxyUrl) + proxyUrl.length(); |
| | |
| | | return url; |
| | | } |
| | | |
| | | /** |
| | | * 获取GeoServer地址 |
| | | */ |
| | | private String getGeoServerUrl(HttpServletRequest req, UserEntity ue, ResEntity entity, String url) { |
| | | if (null == req.getQueryString()) { |
| | | return url; |
| | |
| | | |
| | | String str = req.getQueryString(); |
| | | boolean isLower = str.contains(StaticData.SERVICE); |
| | | String layers = req.getParameter(isLower ? StaticData.LAYERS : StaticData.LAYERS.toUpperCase()); |
| | | String layersKey = isLower ? StaticData.LAYERS : StaticData.LAYERS.toUpperCase(); |
| | | String layers = req.getParameter(layersKey); |
| | | String request = req.getParameter(isLower ? StaticData.REQUEST : StaticData.REQUEST.toUpperCase()); |
| | | if (!StaticData.GET_CAPABILITIES.equals(request) && null != layers) { |
| | | int start = str.indexOf("layers"); |
| | | if (!StaticData.GET_CAPABILITIES.equals(request)) { |
| | | int start = str.indexOf(layersKey); |
| | | int end = str.indexOf("&", start); |
| | | layers = filterGeoLayers(ue, layers); |
| | | |
| | | str = str.replace(str.substring(start, end > -1 ? end : str.length()), "layers=" + layers); |
| | | str = str.replace(str.substring(start, end > -1 ? end : str.length()), layersKey + "=" + layers); |
| | | } else { |
| | | List<String> tabs = StaticData.ADMIN.equals(ue.getUid()) ? permsService.selectAllTabs() : permsService.selectTabs(ue.getUid()); |
| | | entity.setTab(null == tabs ? "" : StringHelper.join(tabs, ",")); |
| | |
| | | return url + (url.contains("?") ? "&" : "?") + str; |
| | | } |
| | | |
| | | /** |
| | | * 过滤GeoServer图层 |
| | | */ |
| | | private String filterGeoLayers(UserEntity ue, String layers) { |
| | | List<String> tabs = StaticData.ADMIN.equals(ue.getUid()) ? permsService.selectAllTabs() : permsService.selectTabs(ue.getUid()); |
| | | if (null == tabs || tabs.isEmpty()) { |
| | | if (null == tabs || tabs.isEmpty() || StringHelper.isEmpty(layers)) { |
| | | return ""; |
| | | } |
| | | |
| | |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 转发请求 |
| | | */ |
| | | private void forward(HttpServletRequest request, HttpServletResponse response, ResEntity entity, String url) throws Exception { |
| | | HttpHelper httpHelper = new HttpHelper(); |
| | | httpHelper.service(request, response, entity, url); |