| | |
| | | package com.moon.server.service.sys; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.moon.server.entity.all.HttpStatus; |
| | | import com.moon.server.entity.all.ResponseMsg; |
| | | import com.moon.server.entity.all.StaticData; |
| | | 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.helper.WebHelper; |
| | | import com.moon.server.interceptor.AuthInterceptor; |
| | | import com.moon.server.service.all.PermsService; |
| | | import com.moon.server.service.all.RedisService; |
| | | import com.moon.server.service.all.SysService; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.List; |
| | | import java.util.TimerTask; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * 代理服务类 |
| | |
| | | */ |
| | | @Service |
| | | public class ProxyService { |
| | | @Resource |
| | | RedisService redisService; |
| | | |
| | | @Resource |
| | | private SysService sysService; |
| | |
| | | /** |
| | | * URL代理 |
| | | */ |
| | | public void proxyUrl(String token, int resId, HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | public void proxyUrl(String token, int resId, boolean isRest, HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | // 3.获取用户 |
| | | UserEntity ue = getUser(res, token); |
| | | UserEntity ue = getUser(req, res, token); |
| | | if (null == ue) { |
| | | return; |
| | | } |
| | | if (!check(req, res, ue, token)) { |
| | | return; |
| | | } |
| | | |
| | | // 9.获取资源实体 |
| | | ResEntity entity = getResEntity(ue, resId); |
| | | if (null == entity || entity.getType() != 3|| StringHelper.isNull(entity.getProxy())||StringHelper.isNull(entity.getUrl())) { |
| | | if (null == entity || StaticData.THREE != entity.getType() || StringHelper.isNull(entity.getProxy()) || StringHelper.isNull(entity.getUrl())) { |
| | | WebHelper.writeStr2Page(res, ILLEGAL_RESOURCE); |
| | | return; |
| | | } |
| | | |
| | | insertLog(req, ue, resId); |
| | | |
| | | String url = getSourceUrl(req, entity, token); |
| | | String url = getSourceUrl(req, entity, token, isRest); |
| | | res.setHeader("token", token); |
| | | forward(req, res, entity, url); |
| | | } |
| | |
| | | /** |
| | | * 获取用户 |
| | | */ |
| | | private UserEntity getUser(HttpServletResponse res, String token) { |
| | | private UserEntity getUser(HttpServletRequest req, HttpServletResponse res, String token) { |
| | | String key = RedisCacheKey.permsProxy(token); |
| | | Object obj = redisService.get(key); |
| | | if (obj instanceof UserEntity) { |
| | | return (UserEntity) obj; |
| | | } |
| | | |
| | | UserEntity ue = sysService.tokenService.getUserByToken(token); |
| | | if (ue == null) { |
| | | if (null == ue) { |
| | | WebHelper.writeStr2Page(res, AuthInterceptor.NO_LOGIN); |
| | | return null; |
| | | } |
| | | if (!check(req, res, ue, token)) { |
| | | return null; |
| | | } |
| | | |
| | | redisService.put(key, ue, SettingData.CACHE_EXPIRE, TimeUnit.MINUTES); |
| | | |
| | | return ue; |
| | | } |
| | |
| | | /** |
| | | * 获取原始Url |
| | | */ |
| | | private String getSourceUrl(HttpServletRequest req, ResEntity entity, String token) { |
| | | private String getSourceUrl(HttpServletRequest req, ResEntity entity, String token, boolean isRest) { |
| | | String proxyUrl = entity.getProxy().replace("{token}", token); |
| | | int end = req.getRequestURL().indexOf(proxyUrl) + proxyUrl.length(); |
| | | |
| | | String url = entity.getUrl() + req.getRequestURL().substring(end); |
| | | if (isRest) { |
| | | url = url.replace("/v6/wmts/", "/v6/rest/"); |
| | | } |
| | | if (null != req.getQueryString()) { |
| | | url = url + (entity.getUrl().contains("?") ? "&" : "?") + req.getQueryString(); |
| | | url = url + (url.contains("?") ? "&" : "?") + req.getQueryString(); |
| | | } |
| | | if (!StringHelper.isNull(entity.getArgs())) { |
| | | url = url + (url.contains("?") ? "&" : "?") + entity.getArgs(); |
| | | } |
| | | |
| | | // System.out.println(url) |
| | | return url; |
| | | } |
| | | |