Commit 93686c7b60ae896ce7235532a111ac7539f2346f
1 parent
ee454ace
fix: 积木报表 API 数据集解析时 token 未正确解析的问题
Showing
3 changed files
with
29 additions
and
144 deletions
yudao-module-visualization/yudao-module-visualization-biz/src/main/java/cn/iocoder/yudao/module/visualization/framework/jmreport/config/JmReportConfiguration.java
| 1 | package cn.iocoder.yudao.module.visualization.framework.jmreport.config; | 1 | package cn.iocoder.yudao.module.visualization.framework.jmreport.config; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; | ||
| 4 | import cn.iocoder.yudao.module.system.api.oauth2.OAuth2TokenApi; | 3 | import cn.iocoder.yudao.module.system.api.oauth2.OAuth2TokenApi; |
| 5 | import cn.iocoder.yudao.module.visualization.framework.jmreport.core.service.JmReportTokenServiceImpl; | 4 | import cn.iocoder.yudao.module.visualization.framework.jmreport.core.service.JmReportTokenServiceImpl; |
| 6 | -import cn.iocoder.yudao.module.visualization.framework.jmreport.core.web.JmReportTokenFilter; | ||
| 7 | import org.jeecg.modules.jmreport.api.JmReportTokenServiceI; | 5 | import org.jeecg.modules.jmreport.api.JmReportTokenServiceI; |
| 8 | -import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
| 9 | import org.springframework.context.annotation.Bean; | 6 | import org.springframework.context.annotation.Bean; |
| 10 | import org.springframework.context.annotation.ComponentScan; | 7 | import org.springframework.context.annotation.ComponentScan; |
| 11 | import org.springframework.context.annotation.Configuration; | 8 | import org.springframework.context.annotation.Configuration; |
| @@ -25,12 +22,4 @@ public class JmReportConfiguration { | @@ -25,12 +22,4 @@ public class JmReportConfiguration { | ||
| 25 | return new JmReportTokenServiceImpl(oAuth2TokenApi); | 22 | return new JmReportTokenServiceImpl(oAuth2TokenApi); |
| 26 | } | 23 | } |
| 27 | 24 | ||
| 28 | - @Bean | ||
| 29 | - @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") | ||
| 30 | - public FilterRegistrationBean<JmReportTokenFilter> registerMyAnotherFilter(OAuth2TokenApi oAuth2TokenApi){ | ||
| 31 | - FilterRegistrationBean<JmReportTokenFilter> bean = new FilterRegistrationBean<>(); | ||
| 32 | - bean.setOrder(WebFilterOrderEnum.JM_TOKEN_FILTER); | ||
| 33 | - bean.setFilter(new JmReportTokenFilter(oAuth2TokenApi)); | ||
| 34 | - return bean; | ||
| 35 | - } | ||
| 36 | } | 25 | } |
yudao-module-visualization/yudao-module-visualization-biz/src/main/java/cn/iocoder/yudao/module/visualization/framework/jmreport/core/service/JmReportTokenServiceImpl.java
| @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.visualization.framework.jmreport.core.service; | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.visualization.framework.jmreport.core.service; | ||
| 2 | 2 | ||
| 3 | import cn.hutool.core.util.StrUtil; | 3 | import cn.hutool.core.util.StrUtil; |
| 4 | import cn.iocoder.yudao.framework.common.exception.ServiceException; | 4 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 5 | +import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; | ||
| 5 | import cn.iocoder.yudao.framework.security.core.LoginUser; | 6 | import cn.iocoder.yudao.framework.security.core.LoginUser; |
| 6 | import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; | 7 | import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; |
| 7 | import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; | 8 | import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; |
| @@ -10,6 +11,9 @@ import cn.iocoder.yudao.module.system.api.oauth2.OAuth2TokenApi; | @@ -10,6 +11,9 @@ import cn.iocoder.yudao.module.system.api.oauth2.OAuth2TokenApi; | ||
| 10 | import cn.iocoder.yudao.module.system.api.oauth2.dto.OAuth2AccessTokenCheckRespDTO; | 11 | import cn.iocoder.yudao.module.system.api.oauth2.dto.OAuth2AccessTokenCheckRespDTO; |
| 11 | import lombok.RequiredArgsConstructor; | 12 | import lombok.RequiredArgsConstructor; |
| 12 | import org.jeecg.modules.jmreport.api.JmReportTokenServiceI; | 13 | import org.jeecg.modules.jmreport.api.JmReportTokenServiceI; |
| 14 | +import org.springframework.http.HttpHeaders; | ||
| 15 | + | ||
| 16 | +import javax.servlet.http.HttpServletRequest; | ||
| 13 | 17 | ||
| 14 | /** | 18 | /** |
| 15 | * {@link JmReportTokenServiceI} 实现类,提供积木报表的 Token 校验、用户信息的查询等功能 | 19 | * {@link JmReportTokenServiceI} 实现类,提供积木报表的 Token 校验、用户信息的查询等功能 |
| @@ -18,10 +22,34 @@ import org.jeecg.modules.jmreport.api.JmReportTokenServiceI; | @@ -18,10 +22,34 @@ import org.jeecg.modules.jmreport.api.JmReportTokenServiceI; | ||
| 18 | */ | 22 | */ |
| 19 | @RequiredArgsConstructor | 23 | @RequiredArgsConstructor |
| 20 | public class JmReportTokenServiceImpl implements JmReportTokenServiceI { | 24 | public class JmReportTokenServiceImpl implements JmReportTokenServiceI { |
| 25 | + private static final String JM_TOKEN_HEADER = "X-Access-Token"; | ||
| 26 | + /** | ||
| 27 | + * 系统内置请求头 | ||
| 28 | + */ | ||
| 29 | + private static final String TOKEN_HEADER = "Authorization"; | ||
| 30 | + /** | ||
| 31 | + * auth 相关格式 | ||
| 32 | + */ | ||
| 33 | + private static final String AUTHORIZATION_FORMAT = "Bearer %s"; | ||
| 21 | 34 | ||
| 22 | private final OAuth2TokenApi oauth2TokenApi; | 35 | private final OAuth2TokenApi oauth2TokenApi; |
| 23 | 36 | ||
| 24 | /** | 37 | /** |
| 38 | + * 修改请求的 head | ||
| 39 | + * | ||
| 40 | + * @return 新 head | ||
| 41 | + */ | ||
| 42 | + @Override | ||
| 43 | + public HttpHeaders customApiHeader() { | ||
| 44 | + HttpHeaders header = new HttpHeaders(); | ||
| 45 | + HttpServletRequest request = ServletUtils.getRequest(); | ||
| 46 | + String token = request.getHeader(JM_TOKEN_HEADER); | ||
| 47 | + | ||
| 48 | + header.add(TOKEN_HEADER, String.format(AUTHORIZATION_FORMAT, token)); | ||
| 49 | + return header; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + /** | ||
| 25 | * 校验 Token 是否有效,即验证通过 | 53 | * 校验 Token 是否有效,即验证通过 |
| 26 | * | 54 | * |
| 27 | * @param token JmReport 前端传递的 token | 55 | * @param token JmReport 前端传递的 token |
| @@ -62,7 +90,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI { | @@ -62,7 +90,7 @@ public class JmReportTokenServiceImpl implements JmReportTokenServiceI { | ||
| 62 | 90 | ||
| 63 | /** | 91 | /** |
| 64 | * 获得用户编号 | 92 | * 获得用户编号 |
| 65 | - * | 93 | + * <p> |
| 66 | * 虽然方法名获得的是 username,实际对应到项目中是用户编号 | 94 | * 虽然方法名获得的是 username,实际对应到项目中是用户编号 |
| 67 | * | 95 | * |
| 68 | * @param token JmReport 前端传递的 token | 96 | * @param token JmReport 前端传递的 token |
yudao-module-visualization/yudao-module-visualization-biz/src/main/java/cn/iocoder/yudao/module/visualization/framework/jmreport/core/web/JmReportTokenFilter.java deleted
| 1 | -package cn.iocoder.yudao.module.visualization.framework.jmreport.core.web; | ||
| 2 | - | ||
| 3 | -import cn.hutool.core.util.StrUtil; | ||
| 4 | -import cn.iocoder.yudao.framework.security.core.LoginUser; | ||
| 5 | -import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; | ||
| 6 | -import cn.iocoder.yudao.module.system.api.oauth2.OAuth2TokenApi; | ||
| 7 | -import cn.iocoder.yudao.module.system.api.oauth2.dto.OAuth2AccessTokenCheckRespDTO; | ||
| 8 | -import lombok.RequiredArgsConstructor; | ||
| 9 | - | ||
| 10 | -import javax.servlet.Filter; | ||
| 11 | -import javax.servlet.FilterChain; | ||
| 12 | -import javax.servlet.ServletException; | ||
| 13 | -import javax.servlet.ServletRequest; | ||
| 14 | -import javax.servlet.ServletResponse; | ||
| 15 | -import javax.servlet.http.HttpServletRequest; | ||
| 16 | -import javax.servlet.http.HttpServletRequestWrapper; | ||
| 17 | -import java.io.IOException; | ||
| 18 | -import java.util.Collections; | ||
| 19 | -import java.util.Enumeration; | ||
| 20 | -import java.util.HashMap; | ||
| 21 | -import java.util.List; | ||
| 22 | -import java.util.Map; | ||
| 23 | -import java.util.Optional; | ||
| 24 | - | ||
| 25 | -/** | ||
| 26 | - * 积木报表 token 处理,将积木报表请求头中的 token 转换成 spring security 的 auth head | ||
| 27 | - */ | ||
| 28 | -@RequiredArgsConstructor | ||
| 29 | -public class JmReportTokenFilter implements Filter { | ||
| 30 | - /** | ||
| 31 | - * 积木 token 请求头 | ||
| 32 | - */ | ||
| 33 | - private static final String JM_TOKEN_HEADER = "X-Access-Token"; | ||
| 34 | - /** | ||
| 35 | - * 系统内置请求头 | ||
| 36 | - */ | ||
| 37 | - private static final String TOKEN_HEADER = "Authorization"; | ||
| 38 | - /** | ||
| 39 | - * auth 相关格式 | ||
| 40 | - */ | ||
| 41 | - private static final String AUTHORIZATION_FORMAT = "Bearer %s"; | ||
| 42 | - | ||
| 43 | - private final OAuth2TokenApi oauth2TokenApi; | ||
| 44 | - | ||
| 45 | - @Override | ||
| 46 | - public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { | ||
| 47 | - // 积木请求头 | ||
| 48 | - HttpServletRequest req = (HttpServletRequest) servletRequest; | ||
| 49 | - String token = req.getHeader(JM_TOKEN_HEADER); | ||
| 50 | - if (StrUtil.isNotEmpty(token)) { | ||
| 51 | - // 1. 增加请求头 | ||
| 52 | - HeaderMapRequestWrapper requestWrapper = new HeaderMapRequestWrapper(req); | ||
| 53 | - requestWrapper.addHeader(TOKEN_HEADER, String.format(AUTHORIZATION_FORMAT, token)); | ||
| 54 | - | ||
| 55 | - OAuth2AccessTokenCheckRespDTO resp = oauth2TokenApi.checkAccessToken(token); | ||
| 56 | - Optional<LoginUser> optUser = Optional.ofNullable(resp) | ||
| 57 | - .map( | ||
| 58 | - t -> new LoginUser().setId(t.getUserId()) | ||
| 59 | - .setUserType(t.getUserType()) | ||
| 60 | - .setTenantId(t.getTenantId()) | ||
| 61 | - .setScopes(t.getScopes()) | ||
| 62 | - ); | ||
| 63 | - if (optUser.isPresent()) { | ||
| 64 | - // 2. 设置登录用户类型 | ||
| 65 | - WebFrameworkUtils.setLoginUserType(servletRequest, optUser.get().getUserType()); | ||
| 66 | - filterChain.doFilter(requestWrapper, servletResponse); | ||
| 67 | - return; | ||
| 68 | - } | ||
| 69 | - } | ||
| 70 | - filterChain.doFilter(servletRequest, servletResponse); | ||
| 71 | - } | ||
| 72 | - | ||
| 73 | - /** | ||
| 74 | - * request 包装类,用于修改 head | ||
| 75 | - * | ||
| 76 | - * <a href="https://stackoverflow.com/questions/2811769/adding-an-http-header-to-the-request-in-a-servlet-filter">add request head</a> | ||
| 77 | - */ | ||
| 78 | - public class HeaderMapRequestWrapper extends HttpServletRequestWrapper { | ||
| 79 | - /** | ||
| 80 | - * construct a wrapper for this request | ||
| 81 | - * | ||
| 82 | - * @param request | ||
| 83 | - */ | ||
| 84 | - public HeaderMapRequestWrapper(HttpServletRequest request) { | ||
| 85 | - super(request); | ||
| 86 | - } | ||
| 87 | - | ||
| 88 | - private Map<String, String> headerMap = new HashMap<String, String>(); | ||
| 89 | - | ||
| 90 | - /** | ||
| 91 | - * add a header with given name and value | ||
| 92 | - * | ||
| 93 | - * @param name | ||
| 94 | - * @param value | ||
| 95 | - */ | ||
| 96 | - public void addHeader(String name, String value) { | ||
| 97 | - headerMap.put(name, value); | ||
| 98 | - } | ||
| 99 | - | ||
| 100 | - @Override | ||
| 101 | - public String getHeader(String name) { | ||
| 102 | - String headerValue = super.getHeader(name); | ||
| 103 | - if (headerMap.containsKey(name)) { | ||
| 104 | - headerValue = headerMap.get(name); | ||
| 105 | - } | ||
| 106 | - return headerValue; | ||
| 107 | - } | ||
| 108 | - | ||
| 109 | - /** | ||
| 110 | - * get the Header names | ||
| 111 | - */ | ||
| 112 | - @Override | ||
| 113 | - public Enumeration<String> getHeaderNames() { | ||
| 114 | - List<String> names = Collections.list(super.getHeaderNames()); | ||
| 115 | - for (String name : headerMap.keySet()) { | ||
| 116 | - names.add(name); | ||
| 117 | - } | ||
| 118 | - return Collections.enumeration(names); | ||
| 119 | - } | ||
| 120 | - | ||
| 121 | - @Override | ||
| 122 | - public Enumeration<String> getHeaders(String name) { | ||
| 123 | - List<String> values = Collections.list(super.getHeaders(name)); | ||
| 124 | - if (headerMap.containsKey(name)) { | ||
| 125 | - values.add(headerMap.get(name)); | ||
| 126 | - } | ||
| 127 | - return Collections.enumeration(values); | ||
| 128 | - } | ||
| 129 | - | ||
| 130 | - } | ||
| 131 | - | ||
| 132 | -} |