Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用Spring Boot REST API设置基本身份验证_Spring_Spring Boot_Spring Security - Fatal编程技术网

无法使用Spring Boot REST API设置基本身份验证

无法使用Spring Boot REST API设置基本身份验证,spring,spring-boot,spring-security,Spring,Spring Boot,Spring Security,我正在尝试使用Spring Boot设置RESTful API,并尝试启用基本身份验证。为什么我总是遇到403拒绝访问错误?我在邮递员中以邮件头的形式发送我的凭证(见附图)。如果我删除.anyRequest.authenticated(),它可以正常工作。但我不想删除它,因为我希望每个端点都有基本的身份验证。我做错了什么 Application.java SecurityConfiguration.java Controller.java 在翻阅了Spring文档之后,我似乎理解了每个链式方法调

我正在尝试使用Spring Boot设置RESTful API,并尝试启用基本身份验证。为什么我总是遇到403拒绝访问错误?我在邮递员中以邮件头的形式发送我的凭证(见附图)。如果我删除
.anyRequest.authenticated()
,它可以正常工作。但我不想删除它,因为我希望每个端点都有基本的身份验证。我做错了什么

Application.java
SecurityConfiguration.java
Controller.java

在翻阅了Spring文档之后,我似乎理解了每个链式方法调用的用途

无论如何,简单的答案是我需要
和().httpBasic()
通过RESTAPI启用基本HTTP身份验证

.anyRequest().authenticated()
只是要求对每个请求进行身份验证,但没有指定方法。添加基本身份验证意味着我们可以使用基本身份验证对用户进行身份验证


在翻阅了Spring文档之后,我似乎理解了每个链式方法调用的用途

无论如何,简单的答案是我需要
和().httpBasic()
通过RESTAPI启用基本HTTP身份验证

.anyRequest().authenticated()
只是要求对每个请求进行身份验证,但没有指定方法。添加基本身份验证意味着我们可以使用基本身份验证对用户进行身份验证

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/h2-console/*").permitAll()
                .anyRequest().authenticated();

        http.csrf().disable();
        http.headers().frameOptions().disable();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
}
@RestController
public class Controller {

    @RequestMapping("/test")
    public String index() {
        return "Greetings from Spring Boot!";
    }
}