【发布时间】:2018-06-20 12:17:38
【问题描述】:
以下场景:我的 Spring Boot 2.0 REST 服务通过与 Keycloak 对话的 API 网关获取 Angular 6 客户端发出的请求。所以请求是由已经通过身份验证的用户发出的(由 API 网关完成)。有关用户及其角色的信息被打包在一个 JWT 令牌中,该令牌是请求的一部分(在带有 Bearer 令牌的授权标头中)。
如何在服务端处理令牌?
我构建了一个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 的文档,应该是这样), - 如果是,那么如何避免重定向?
- 如果没有,如何以其他正确的方式做到这一点?
【问题讨论】:
标签: spring spring-boot spring-security spring-security-oauth2 angular2-jwt