【问题标题】:how to create exceptions to JWT filter based on URLs如何基于 URL 为 JWT 过滤器创建例外
【发布时间】:2018-05-20 05:16:18
【问题描述】:

我正在处理 JWT 身份验证。我想绕过(不允许通过 jwt 过滤器)提供的 URL。下面是代码sn-p。

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.cors().and().csrf().disable().authorizeRequests().antMatchers(bypassURLs)
                    .permitAll().anyRequest().authenticated().and().addFilter(new JWTAuthenticationFilter(authenticationManager(), jwtConfigProperties))
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);}

在上面的代码中,我希望系统不要过滤bypassURLs。 如果我传递“/sayHello”,则不应将 JWTAuthenticationFilter 应用于此,而“/sayHello”以外的 URL 必须通过 JWTAuthenticationFilter。

我尝试了http.csrf().ignoringAntMatchers("/sayHello"); 和一些正则表达式,但没有成功。请帮忙。

【问题讨论】:

  • 使用.authorizeRequests().antMatchers("/sayHello").permitAll()
  • 我需要过滤除“/sayHello”之外的所有请求。

标签: java http jwt jjwt


【解决方案1】:

当使用permitAll 时,它表示每个经过身份验证的用户,但是您禁用了匿名访问,这样就不起作用了。

您想要忽略某些 URL 以覆盖 configure 方法,该方法采用 WebSecurity 对象和 ignore 模式。

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/api/v1/signup");
}

并从HttpSecurity 部分中删除该行。这将告诉 Spring Security 忽略此 URL,并且不对它们应用任何过滤器。

【讨论】:

  • 这不会过滤指定的 url 但我将如何允许其他人通过过滤器。 “/sayHello”以外的网址
  • 不要做任何事情...过滤器将自动应用于除web.ignoring().antMatchers("/api/v1/signup")之外的其他网址
  • 您只需覆盖方法protected void configure(HttpSecurity httpSecurity) 并添加过滤器...您已经添加
  • 只需检查其他 URL 是否通过过滤器..让我知道它是否有效..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-04
  • 1970-01-01
相关资源
最近更新 更多