Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

内部服务外网隔离,子服务添加过滤器后报错 #134

Closed
warthur1214 opened this issue Aug 25, 2021 · 9 comments
Closed

内部服务外网隔离,子服务添加过滤器后报错 #134

warthur1214 opened this issue Aug 25, 2021 · 9 comments

Comments

@warthur1214
Copy link

warthur1214 commented Aug 25, 2021

参考文档

过滤器配置

@Component
public class SaTokenConfigure implements WebMvcConfigurer {

    @Value("${spring.profiles.active:dev}")
    private String profile;

    /**
     * 注册 Sa-Token 全局过滤器
     */
    @Bean
    public SaServletFilter getSaServletFilter() {
        return new SaServletFilter().addInclude("/**").addExclude("/favicon.ico").setAuth(r -> {

            // 校验 Id-Token 身份凭证
            if ("prod".equals(profile)) {
                SaIdUtil.checkCurrentRequestToken();
            }
        }).setError(e -> SaResult.error(e.getMessage()));
    }
}

异常信息:

{"code": 500, "msg": "非Web上下文无法获取Request", "data": "null"}

异常位置:

cn.dev33.satoken.spring.SpringMVCUtil

/**
 * 获取当前会话的 request 
 * @return request
 */
public static HttpServletRequest getRequest() {
	ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
	if(servletRequestAttributes == null) {
		throw new SaTokenException("非Web上下文无法获取Request");
	}
	return servletRequestAttributes.getRequest();
}
@click33
Copy link
Collaborator

click33 commented Aug 25, 2021

你用法有误,
网关处是提交token的地方,网关里无需验证token

@warthur1214
Copy link
Author

@click33 我标题没描述清楚,过滤器是加到子服务里的

@warthur1214 warthur1214 changed the title 网关转发鉴权添加过滤器后报错 内部服务外网隔离,子服务添加过滤器后报错 Aug 25, 2021
@click33
Copy link
Collaborator

click33 commented Aug 25, 2021

这是走的什么调用抱的错,正常转发,还是feign 还是dubbo

@warthur1214
Copy link
Author

@click33 正常转发,用postman调controller里的接口报这个错,把过滤器去掉就正常了

@click33
Copy link
Collaborator

click33 commented Aug 26, 2021

看不出来什么问题了,有时间的话弄个复现demo吧,这边本地调试一下

@warthur1214
Copy link
Author

@click33 抱歉占用你的时间了。我通过排除法定位到 除了SaTokenConfigure实现WebMvcConfigurer接口,我还有个WebMvcConfig配置类并且@EnableWebMvc。

@click33
Copy link
Collaborator

click33 commented Aug 26, 2021

很多问题排查到最后发现都不是框架的问题,而是被一些其它的代码影响了……

没有疑问的话就关闭 issues 吧

@warthur1214
Copy link
Author

@click33 疑问到是有一个😁,为什么SaServletFilter和EnableWebMvc不能共用呢,不知道我的问题描述的准确不

@click33
Copy link
Collaborator

click33 commented Aug 26, 2021

这个和 SpringBoot 配置初始化机制有关系,我也没有很仔细的研究过,找到解决方案了就OK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants