【问题标题】:Http failure response for http url: 401 Unauthorizedhttp url 的 Http 失败响应:401 Unauthorized
【发布时间】:2020-06-01 08:00:25
【问题描述】:

每当我使用以下方法时,我都可以从角度调用我的服务

   @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.csrf().disable().authorizeRequests().antMatchers("/").permitAll();
    }

但是当我如下更改配置方法时,我收到 401 错误。

@Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.csrf().disable()
                // dont authenticate this particular request
                .authorizeRequests().antMatchers("/authenticate", "/register","/basicauth").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);
    }

如何解决这个问题。

我的错误如下

【问题讨论】:

    标签: java angular authentication spring-security http-status-code-401


    【解决方案1】:

    您需要在antMatchers() 中指定整个端点。据我所知,您要访问的网址是 .../api/v1/basicauth => 将 /basicauth 字符串更改为 /api/v1/basicauth。。 p>

    【讨论】:

    • 另一种解决方案可能是使用 /*/basicauth 并且它更通用(basicauth 之前的映射可以是任何东西)。但出于身份验证的目的,我认为最好使用完整的端点。
    猜你喜欢
    • 2011-03-18
    • 2016-12-14
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    相关资源
    最近更新 更多