【问题标题】:antMatchers() is not working , and gives forbidden errorantMatchers() 不工作,并给出禁止错误
【发布时间】:2021-12-16 17:57:56
【问题描述】:

我有一个名为 authenticate 的端点,该端点被提供给 antMatchers("/authenticate") 以跳过对该端点的授权,但它仍会检查身份验证。

代码:

   @Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    // We don't need CSRF for this example
    httpSecurity.csrf().disable()
            // dont authenticate this particular request
            .authorizeRequests().antMatchers("/authenticate").permitAll()
            // all other requests need to be authenticated
            .and().authorizeRequests()
            .anyRequest().authenticated()
            .and()
            // make sure we use stateless session; session won't be used to
            // store user's state.
            .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
            .and().sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

    // Add a filter to validate the tokens with every request
    httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
}

【问题讨论】:

  • permitAll 并不意味着没有安全性。仍然需要一个身份验证对象,但它可以是匿名的。你还确定模式是正确的,并且 url 是 /authenticate 而不是别的吗?
  • 模式是正确的,而不是permitAll(),我们还能用什么来停止在端点检查授权?
  • 那个端点是做什么的?您的 JWT 过滤器不应该已经解码令牌并设置身份验证吗?为什么你需要一个端点?

标签: java spring-boot jwt


【解决方案1】:

尝试添加此方法以确保忽略此端点。

@Override
    public void configure(final WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/authenticate");

    }

【讨论】:

  • 谢谢,这不是 antMatchers() 的问题,而是弃用函数的另一个问题。
【解决方案2】:

如果无法评估,只需通过您的身份验证过滤器。

    @Override
    protected void doFilterInternal(final HttpServletRequest req,
        final HttpServletResponse res,
        final FilterChain chain) throws IOException, ServletException {
        final String header = req.getHeader("Authorization");

        if (header == null || !header.startsWith("Bearer ")) {
            // if cannot be evaluated
            chain.doFilter(req, res);
            return;
        }

        // do authentication

        // SecurityContextHolder.getContext().setAuthentication() if authenticated normally
        // throw AuthenticationException if received illegal credentials

        chain.doFilter(req, res);
    }

另见AbstractAuthenticationProcessingFilter#attemptAuthentication()javadoc:

实现应该执行以下操作之一:

  1. 为经过身份验证的用户返回填充的身份验证令牌,表示身份验证成功
  2. 返回null,表示认证过程仍在进行中。在返回之前,实施应执行完成流程所需的任何额外工作。
  3. 如果身份验证过程失败,则抛出 AuthenticationException

【讨论】:

    【解决方案3】:

    我有关于这个问题的更新。

    在我的情况下,我遇到了一个已弃用的函数 singWith() 的问题,/authenticate 的请求正在通过 antMatchers() 过滤器但没有能够生成令牌。

    经过研究,我发现还有其他类型的相同功能,效果很好。

    旧版令牌生成代码。

    SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64.decode("newworldorder"));
    

    密钥属于 SecretKey 类型

    Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(date)
                .setExpiration(new Date(validity)
                .signWith(key).compact();
    

    这是新版本的令牌生成代码。

    private String key = "newworldorder";
    

    键是字符串类型

    Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(date)
                .setExpiration(validity)
                .signWith(SignatureAlgorithm.HS512, key).compact();
    

    【讨论】:

      猜你喜欢
      • 2017-08-31
      • 1970-01-01
      • 2013-08-29
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多