Java Spring安全性基本HTML身份验证

Java Spring安全性基本HTML身份验证,java,json,spring,spring-mvc,spring-security,Java,Json,Spring,Spring Mvc,Spring Security,我正在尝试为我参与的一个项目开发一个RESTful Web服务,这对我来说是非常新鲜的 我正在尝试向Web服务器添加一个基本的身份验证。我的想法是,使用该服务的每个应用程序都具有凭据。至少在这一点上,不会有任何web界面来访问它。 我一直在遵循Spring的教程,试图改变一些事情,主要是因为我试图使用Spring的最新版本 我被一个看起来很简单的部分困住了,那就是,只允许用户使用有效的用户名和密码向系统插入一个新值,但是由于某种原因,当我运行特定的测试时,我一直收到错误403 以下是我的配置:

我正在尝试为我参与的一个项目开发一个RESTful Web服务,这对我来说是非常新鲜的

我正在尝试向Web服务器添加一个基本的身份验证。我的想法是,使用该服务的每个应用程序都具有凭据。至少在这一点上,不会有任何web界面来访问它。 我一直在遵循Spring的教程,试图改变一些事情,主要是因为我试图使用Spring的最新版本

我被一个看起来很简单的部分困住了,那就是,只允许用户使用有效的用户名和密码向系统插入一个新值,但是由于某种原因,当我运行特定的测试时,我一直收到错误403

以下是我的配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

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

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/aggregators/**").authorizeRequests()
        .anyRequest().hasRole("USER")
        .and().httpBasic();
    }

}
在测试类中,错误来自buildindividual构建JSON的类

private ResponseEntity<Individual> buildIndividual() {

     HttpEntity<String> requestEntity = new HttpEntity<String>(
             RestDataFixture.standardIndividualJSON2(),
             getHeaders("user:password"));
     RestTemplate template = new RestTemplate();


     ResponseEntity<Individual> entity = template.postForEntity(
             "http://localhost:8080/aggregators/individuals",
             requestEntity, Individual.class);

     return entity;
 }

 static HttpHeaders getHeaders(String auth) {
     HttpHeaders headers = new HttpHeaders();
     headers.setContentType(MediaType.APPLICATION_JSON);
     headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

     byte[] codedString = Base64.encode(auth.getBytes());
     headers.add("Authorization", "Basic " + new String(codedString));

     return headers;
 }

通过禁用CSRF解决:

@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().
antMatcher("/aggregators/**").authorizeRequests()
.anyRequest().hasRole("USER")
.and().httpBasic();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
   http.csrf().disable().
   antMatcher("/aggregators/**").authorizeRequests()
   .anyRequest().hasRole("USER")
   .and().httpBasic();
}

也许localhost/8080应该是localhost:8080?我注意到了这个输入错误,但没有解决任何问题。我尝试添加密码不正确的新个人的测试也返回403。如果您找到解决方案,请将其作为答案发布并接受。不要把它附加在问题后面;
@Override
protected void configure(HttpSecurity http) throws Exception {
   http.csrf().disable().
   antMatcher("/aggregators/**").authorizeRequests()
   .anyRequest().hasRole("USER")
   .and().httpBasic();
}