以下场景:我的 Spring Boot 2.0 REST 服务通过与 Keycloak 对话的 API 网关获取 Angular 6 客户端发出的请求。所以请求是由已经通过身份验证的用户发出的(由 API 网关完成)。有关用户及其角色的信息被打包在作为请求一部分的 JWT 令牌中(在带有承载令牌的授权标头中)。
如何在服务端处理令牌?
我构建了一个TokenPreAuthenticatedProcessingFilter
(基于AbstractPreAuthenticatedProcessingFilter
)一个在我的配置它WebSecurityConfigurerAdapter
如下:
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**")
.cors()
.and()
.authorizeRequests()
.mvcMatchers(HttpMethod.GET, "/health", "/info").anonymous()
.anyRequest().authenticated()
.and()
.addFilterBefore(tokenPreAuthenticatedFilter(), RequestHeaderAuthenticationFilter.class)
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.NEVER)
.and()
.csrf().disable();
到目前为止一切顺利,但是在控制器中到达(并运行)端点后,请求被重定向并且客户端获得 HTTP 状态代码 302 作为响应而不是数据。
问题:
- 这种情况下的方法
AbstractPreAuthenticatedProcessingFilter
是否正确?(我已经阅读了http://springcert.sourceforge.net/sec-3/preauth.html的文档,它应该是), - 如果是,那么如何避免重定向?
- 如果没有,如何以其他正确的方式做到这一点?