| | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.java110.core.context.IPageData; |
| | | import com.java110.core.context.PageData; |
| | | import com.java110.core.context.SecureInvocation; |
| | | import com.java110.core.log.LoggerFactory; |
| | | import com.java110.utils.constant.CommonConstant; |
| | | import com.java110.utils.exception.FilterException; |
| | |
| | | String componentMethod = ""; |
| | | if (url.contains("callComponent")) { //组件处理 |
| | | String[] urls = url.split("/"); |
| | | |
| | | if (urls.length == 6) { |
| | | componentCode = urls[4]; |
| | | componentMethod = urls[5]; |
| | |
| | | String headerName = (String) reqHeaderEnum.nextElement(); |
| | | headers.put(headerName.toLowerCase(), request.getHeader(headerName)); |
| | | } |
| | | pd = PageData.newInstance().builder(userId, userName, this.getToken(request), reqData, componentCode, componentMethod, url, sessionId, appId, headers); |
| | | //pd = PageData.newInstance().builder(userId, userName, this.getToken(request), reqData, componentCode, componentMethod, url, sessionId, appId, headers); |
| | | headers.put(CommonConstant.COOKIE_AUTH_TOKEN,this.getToken(request)); |
| | | pd = PageData.newInstance().builder(userId, userName, "", reqData, componentCode, componentMethod, url, sessionId, appId, headers); |
| | | pd.setMethod(request.getMethod().equals("GET") ? HttpMethod.GET : HttpMethod.POST); |
| | | |
| | | logger.debug("切面 获取到的pd=" + JSONObject.toJSONString(pd)); |
| | | request.setAttribute(CommonConstant.CONTEXT_PAGE_DATA, pd); |
| | | //调用链 |
| | | //Java110TraceFactory.createTrace(componentCode + "/" + componentMethod, headers); |
| | | } |
| | | |
| | | |
| | | @AfterReturning(returning = "ret", pointcut = "dataProcess()") |
| | | public void doAfterReturning(Object ret) throws Throwable { |
| | |
| | | public void after(JoinPoint jp) throws IOException { |
| | | // 接收到请求,记录请求内容 |
| | | ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| | | |
| | | HttpServletRequest request = attributes.getRequest(); |
| | | //记录调用链 |
| | | //Java110TraceFactory.putAnnotations(TraceAnnotationsDto.VALUE_CLIENT_RECEIVE); |
| | | |
| | | PageData pd = request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA) != null ? (PageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA) : null; |
| | | //保存日志处理 |
| | | if (pd == null) { |
| | | return; |
| | | } |
| | | |
| | | //写cookies信息 |
| | | writeCookieInfo(pd, attributes); |
| | | |
| | |
| | | return o; |
| | | } catch (Throwable e) { |
| | | logger.error("执行方法异常", e); |
| | | return new ResponseEntity("内部异常" + e.getLocalizedMessage(), HttpStatus.BAD_REQUEST); |
| | | return new ResponseEntity(e.getLocalizedMessage(), HttpStatus.BAD_REQUEST); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | private String getToken(HttpServletRequest request) throws FilterException { |
| | | String token = ""; |
| | | if (request.getCookies() == null || request.getCookies().length == 0) { |
| | | return token; |
| | | } |
| | | for (Cookie cookie : request.getCookies()) { |
| | | if (CommonConstant.COOKIE_AUTH_TOKEN.equals(cookie.getName()) ) { |
| | | token = cookie.getValue(); |
| | | if (request.getCookies() != null && request.getCookies().length > 0) { |
| | | for (Cookie cookie : request.getCookies()) { |
| | | if (CommonConstant.COOKIE_AUTH_TOKEN.equals(cookie.getName())) { |
| | | token = cookie.getValue(); |
| | | } |
| | | } |
| | | } |
| | | String authorization = request.getHeader("Authorization"); |
| | | |
| | | if(StringUtil.isEmpty(token) && !StringUtil.isEmpty(authorization)){ |
| | | token = authorization.substring("Bearer ".length()); |
| | | } |
| | | return token; |
| | | } |
| | |
| | | * @throws IOException |
| | | */ |
| | | private void writeCookieInfo(IPageData pd, ServletRequestAttributes attributes) throws IOException { |
| | | // 这里目前只写到组件级别,如果需要 写成方法级别 |
| | | if (!StringUtil.isNullOrNone(pd.getToken()) && "login".equals(pd.getComponentCode())) { |
| | | HttpServletResponse response = attributes.getResponse(); |
| | | Cookie cookie = new Cookie(CommonConstant.COOKIE_AUTH_TOKEN, pd.getToken()); |
| | | cookie.setHttpOnly(true); |
| | | cookie.setPath("/"); |
| | | response.addCookie(cookie); |
| | | response.flushBuffer(); |
| | | // 这里目前只写到组件级别,如果需要 写成方法级别 && "login".equals(pd.getComponentCode()) |
| | | //todo 未包含token 不做处理 |
| | | if (StringUtil.isNullOrNone(pd.getToken())) { |
| | | return; |
| | | } |
| | | HttpServletResponse response = attributes.getResponse(); |
| | | |
| | | //讲token写入到cookies 中 |
| | | Cookie cookie = new Cookie(CommonConstant.COOKIE_AUTH_TOKEN, pd.getToken()); |
| | | cookie.setHttpOnly(true); |
| | | cookie.setPath("/"); |
| | | |
| | | response.addCookie(cookie); |
| | | //response.addHeader("Set-Cookie","SameSite=None"); |
| | | |
| | | response.flushBuffer(); |
| | | |
| | | |
| | | } |
| | | } |