ÎļþÃû´Ó src/main/java/com/moon/server/interceptor/ProxyFilter.java ÐÞ¸Ä |
| | |
| | | package com.moon.server.interceptor; |
| | | package com.moon.server.service.sys; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.moon.server.entity.all.HttpStatus; |
| | |
| | | import com.moon.server.helper.HttpHelper; |
| | | import com.moon.server.helper.StringHelper; |
| | | 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.SysService; |
| | | import com.moon.server.service.sys.ResLogService; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.*; |
| | | import javax.servlet.annotation.WebFilter; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.List; |
| | | import java.util.TimerTask; |
| | | |
| | | /** |
| | | * 代çè¿æ»¤å¨ |
| | | * 代çæå¡ç±» |
| | | * @author WWW |
| | | * @date 2023-07-04 |
| | | * @date 2023-07-11 |
| | | */ |
| | | @WebFilter(urlPatterns = {"/proxy/*"}) |
| | | public class ProxyFilter implements Filter { |
| | | @Service |
| | | public class ProxyService { |
| | | |
| | | @Resource |
| | | private SysService sysService; |
| | | |
| | |
| | | @Resource |
| | | private ResLogService resLogService; |
| | | |
| | | private final static int LEN = "/proxy/".length(); |
| | | |
| | | private static final Log log = LogFactory.getLog(ProxyFilter.class); |
| | | |
| | | public static final String ILLEGAL_RESOURCE = JSON.toJSONString(new ResponseMsg<String>(HttpStatus.UNAUTHORIZED, "没æèµæºè®¿é®æé")); |
| | | |
| | | @Override |
| | | public void init(FilterConfig filterConfig) { |
| | | } |
| | | |
| | | @Override |
| | | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { |
| | | try { |
| | | HttpServletRequest req = (HttpServletRequest) request; |
| | | HttpServletResponse res = (HttpServletResponse) response; |
| | | |
| | | // 2.è·å令ç |
| | | String token = getToken(req); |
| | | |
| | | // 3.è·åç¨æ· |
| | | UserEntity ue = getUser(res, token); |
| | | if (null == ue) { |
| | | return; |
| | | } |
| | | |
| | | int resId = getResId(req.getRequestURI(), LEN + token.length() + 1); |
| | | if (!check(req, res, ue, token)) { |
| | | return; |
| | | } |
| | | |
| | | // 9.è·åèµæºå®ä½ |
| | | ResEntity entity = getResEntity(ue, resId); |
| | | if (null == entity) { |
| | | WebHelper.writeStr2Page(res, ILLEGAL_RESOURCE); |
| | | return; |
| | | } |
| | | |
| | | insertLog(req, ue, resId); |
| | | String url = getSourceUrl(req, entity); |
| | | proxy(req, res, url); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void destroy() { |
| | | } |
| | | |
| | | /** |
| | | * è·å令ç |
| | | * URL代ç |
| | | */ |
| | | private String getToken(HttpServletRequest req) { |
| | | return req.getRequestURI().substring(LEN, req.getRequestURI().indexOf("/", LEN)); |
| | | public void proxyUrl(String token, int resId, HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | // 3.è·åç¨æ· |
| | | UserEntity ue = getUser(res, token); |
| | | if (null == ue) { |
| | | return; |
| | | } |
| | | |
| | | // 9.è·åèµæºå®ä½ |
| | | ResEntity entity = getResEntity(ue, resId); |
| | | if (null == entity) { |
| | | WebHelper.writeStr2Page(res, ILLEGAL_RESOURCE); |
| | | return; |
| | | } |
| | | |
| | | insertLog(req, ue, resId); |
| | | |
| | | String url = getSourceUrl(req, entity, token); |
| | | forward(req, res, url); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | return ue; |
| | | } |
| | | |
| | | /** |
| | | * è·åèµæºID |
| | | */ |
| | | private int getResId(String uri, int start) { |
| | | int end = uri.indexOf("/", start); |
| | | String str = -1 == end ? uri.substring(start) : uri.substring(start, end); |
| | | |
| | | return Integer.parseInt(str); |
| | | } |
| | | |
| | | /** |
| | |
| | | return null; |
| | | } |
| | | |
| | | // List<ResEntity> list = rs.stream().filter(resEntity -> resEntity.getId() == resId).collect(Collectors.toList()); |
| | | // List<ResEntity> list = rs.stream().filter(resEntity -> resEntity.getId() == resId).collect(Collectors.toList()) |
| | | for (ResEntity entity : rs) { |
| | | if (resId == entity.getId()) { |
| | | return entity; |
| | |
| | | /** |
| | | * è·ååå§Url |
| | | */ |
| | | private String getSourceUrl(HttpServletRequest req, ResEntity entity) { |
| | | private String getSourceUrl(HttpServletRequest req, ResEntity entity, String token) { |
| | | String proxyUrl = entity.getProxy().replace("{token}", token); |
| | | int end = req.getRequestURL().indexOf(proxyUrl) + proxyUrl.length(); |
| | | |
| | | return ""; |
| | | return entity.getUrl() + req.getRequestURL().substring(end); |
| | | } |
| | | |
| | | /** |
| | | * 代çæå¡ |
| | | * 转åè¯·æ± |
| | | */ |
| | | private void proxy(HttpServletRequest request, HttpServletResponse response, String url) throws Exception { |
| | | private void forward(HttpServletRequest request, HttpServletResponse response, String url) throws Exception { |
| | | HttpHelper httpHelper = new HttpHelper(); |
| | | httpHelper.service(request, response, url, null); |
| | | } |