Spring Boot 返回 401- 未经授权,即使我允许它

时间:2020-12-21 19:46:19

标签: java spring spring-boot authentication postman

我正在尝试通过 Postman 访问我的 API。在配置中,我已将 /authenticate 设置为允许,但我一直收到 401 未授权。

相关函数:

D:\softwares\tor\Tor Browser\Browser\TorBrowser\Tor>tor.exe --hash-password <my password> | more

我的依赖

@Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                // dont authenticate this particular request
                .authorizeRequests().antMatchers("/authenticate").permitAll().
                // all other requests need to be authenticated
                        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);
    }

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

这里的问题是,即使您将 Spring Security 配置为允许每个用户,您的请求也会通过与其他请求相同的安全链。我猜您对身份验证端点的调用在授权标头中不包含任何内容,或者换句话说,您的令牌为空,因此您收到 401。在这种情况下,一种解决方案可以是对 spring 说用于身份验证的 URL 的安全性,请求不会通过安全链。您可以使用 Spring Security 配置中的以下代码实现这一点。

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

希望这能解决您的问题。

相关问题