Spring Cloud Gateway 与 spring-boot-starter-web

pro*_*r20 5 java spring-security jwt spring-boot spring-cloud-gateway

我正在使用 Spring Cloud Gateway 为 Spring Boot 微服务创建网关。Gateway 还负责使用 Spring Security 进行 JWT 授权。

public class JwtAuthorizationFilter extends BasicAuthenticationFilter {

    ...

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        String header = request.getHeader(JwtProperties.HEADER_STRING);

        if (header == null || !header.startsWith(JwtProperties.TOKEN_PREFIX)) {
            chain.doFilter(request, response);
            return;
        }

        Authentication authentication = getUsernamePasswordAuthentication(request);
        SecurityContextHolder.getContext().setAuthentication(authentication);

        chain.doFilter(request, response);
    }

    private Authentication getUsernamePasswordAuthentication(HttpServletRequest request) {
        String token = request.getHeader(JwtProperties.HEADER_STRING).replace(JwtProperties.TOKEN_PREFIX, "");

        DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC512(JwtProperties.SECRET.getBytes())).build().verify(token);
        String username = decodedJWT.getSubject();

        if (username != null) {
            UserPrincipal principal = (UserPrincipal) userPrincipalService.loadUserByUsername(username);
            Authentication auth = new UsernamePasswordAuthenticationToken(username, null, principal.getAuthorities());
            return auth;
        }
        return null;
    }

}
Run Code Online (Sandbox Code Playgroud)

该过滤器在配置方法中注册,如下所示:

@Configuration
@EnableWebSecurity
public class ApplicationSecurityConfig extends WebSecurityConfigurerAdapter {
    
    ...
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .cors()
            .and()
            .csrf().disable()
            .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .addFilter(new JwtAuthorizationFilter(authenticationManager(), userPrincipalService))
            .authorizeRequests()
            .antMatchers(HttpMethod.POST, "/login").permitAll()
            ...
            .anyRequest().authenticated();
        
    }
...
}
Run Code Online (Sandbox Code Playgroud)

如您所见,Spring Security 使用属于 spring-boot-starter-web 的 HttpServletRequest、HttpServletResponse、FilterChain 接口。但这是主要问题,因为它与 Spring Cloud Gateway 不兼容。

Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.
Run Code Online (Sandbox Code Playgroud)

有没有办法避免此错误或在网关上实施 jwt 授权过滤器有任何不同的解决方案?谢谢!

Mar*_*nik 3

Spring Cloud Gateway的文档中明确指出该产品运行在Netty之上并且需要webflux,因此它与Spring MVC不兼容。

您使用的过滤器 ( JwtAuthorizationFilter) 属于非反应式世界,因此您可能应该使用 spring security 来重写它以用于 Web Flux 构建块。

免责声明,我不是 Spring Web Flux/Spring-Security 专家,但请考虑检查 此应用程序- 它展示了如何使用 Spring Security 的反应式版本定义 JWT 安全应用程序。

因此,最重要的是,您应该选择是想要反应式应用程序还是传统应用程序,并使用相关技术,但不能真正混合它们。