【问题标题】:Spring Boot Security wont ignore certain paths that dont need to be securedSpring Boot Security 不会忽略某些不需要保护的路径
【发布时间】:2020-11-04 03:06:27
【问题描述】:

我之前看到有人发布过有关此问题的帖子。问题是我希望 /api/v1/auth/ 控制器中的所有内容都不会通过 JWT 过滤器链。

这就是我的安全配置的样子

@Configuration
@EnableWebSecurity
class SecurityConfig() : WebSecurityConfigurerAdapter() {

    @Autowired
    lateinit var tokenService: TokenService

    override fun configure(web: WebSecurity) {
        web.ignoring().antMatchers(
                "/v2/api-docs",
                "/configuration/ui",
                "/swagger-resources/**",
                "/configuration/security",
                "/swagger-ui.html",
                "/webjars/**",
                "/api/v1/auth/**",
                "/api/v1/auth/request",
                "/api/v1/auth/verify",
                "/api/v1/auth/verify_hack",
                "/api/v1/auth/refresh_token",
                "/messages",
                "/index.html"
        )
    }

    override fun configure(http: HttpSecurity) {
        http.cors().and().csrf()
                .disable()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers("/api/v1/auth/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .addFilterBefore(JwtFilter(tokenService), UsernamePasswordAuthenticationFilter::class.java)


    }
}

如果我在 /api/v1/auth 下点击一个端点并且 DONT 添加一个授权标头,它似乎绕过了 jwt 过滤器类,如果我确实添加了一个授权标头对于它总是进入 JWT 过滤器类的任何请求,我需要它完全忽略它,这是 configure 方法应该做的。

您可能会问为什么不直接发送 Auth 标头,我特别需要它用于 api/v1/auth/refresh_token 端点

这是最新最好的 Spring Boot 2.3.0。

TLDR 我到底如何让安全配置真正忽略路径

JWT 过滤器

class JwtFilter(private val tokenService: TokenService) : GenericFilterBean() {

    override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {

        val token = TokenUtil.extractToken(request as HttpServletRequest)

        if (token != null && token.isNotEmpty()) {
            try {
                tokenService.getClaims(token)
            } catch (e: SignatureException) {
                throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Invalid JWT Signature")
            } catch (e: MalformedJwtException) {
                throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Invalid JWT token")
            } catch (e: ExpiredJwtException) {
                throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Expired JWT token")
            } catch (e: UnsupportedJwtException) {
                throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Unsupported JWT exception")
            } catch (e: IllegalArgumentException) {
                throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Jwt claims string is empty")
            }
        } else {
            throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Missing auth token")
        }
        chain.doFilter(request, response)
    }

}

【问题讨论】:

    标签: spring spring-boot spring-security


    【解决方案1】:

    您可以将此方法添加到您的JwtFilter

    class JwtFilter(private val tokenService: TokenService) : GenericFilterBean() {
    
        override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
            if (request.getRequestURI().startsWith("/api/v1/auth/")) {
               filterChain.doFilter(request, response);
               return;
            }
    
            val token = TokenUtil.extractToken(request as HttpServletRequest)
            ....
    

    【讨论】:

    • 我没有为此设置覆盖方法,JWT 过滤器实现了GenericFilterBean
    • 然后改为这样做。
    • 这行得通,但感觉像一个黑客,如果没有其他工作,将标记为正确
    【解决方案2】:

    您可以在过滤器上添加验证以排除您不想过滤的网址。

    将此代码添加到您的过滤器中

    String path = request.getRequestURI();
    if ("/urltoexclude".equals(path)) {
        filterChain.doFilter(request, response);
        return;
    }
    

    这里是有关如何从过滤器中排除 url 的更多信息。 https://www.baeldung.com/spring-exclude-filter

    【讨论】:

      猜你喜欢
      • 2016-09-10
      • 1970-01-01
      • 2022-01-25
      • 2018-03-24
      • 2021-07-08
      • 2018-06-21
      • 1970-01-01
      • 2021-04-13
      • 2011-08-06
      相关资源
      最近更新 更多