Spring boot HttpSecurity Spring引导配置

Spring boot HttpSecurity Spring引导配置,spring-boot,spring-security,Spring Boot,Spring Security,我需要配置3个端点,2个具有身份验证,1个不具有身份验证。问题是我得到的所有端点都有错误 /users无需身份验证 /users/1需要身份验证 /details/1需要身份验证 我正在使用依赖项: org.springframework.boot 弹簧启动安全 我实现了这个类: @配置 公共类SecurityConfig扩展了WebSecurity配置适配器{ @凌驾 受保护的无效配置(AuthenticationManagerBuilder auth)引发异常{ 认证 .inMemo

我需要配置3个端点,2个具有身份验证,1个不具有身份验证。问题是我得到的所有端点都有错误

  • /users
    无需身份验证
  • /users/1
    需要身份验证
  • /details/1
    需要身份验证
我正在使用依赖项:


org.springframework.boot
弹簧启动安全
我实现了这个类:

@配置
公共类SecurityConfig扩展了WebSecurity配置适配器{
@凌驾
受保护的无效配置(AuthenticationManagerBuilder auth)引发异常{
认证
.inMemoryAuthentication()
.withUser(“管理员”)
.密码(“pwd”)
.角色(“用户”、“管理员”);
}
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http
.httpBasic()
.及()
.授权请求()
.antMatchers(HttpMethod.GET,“/users”).hasAnyRole(“管理员”、“用户”)
.anyRequest().permitAll()
.及()
.授权请求()
.anyRequest().hasAnyRole(“管理员”、“用户”);
}
}

根据您的要求,您只需要简单的http配置,所有人都可以通过公共url访问GET用户,其他人需要基本身份验证。以下内容适用于您

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers(HttpMethod.GET, "/users").permitAll()
                .anyRequest().authenticated()
                .and()
                .httpBasic();

    }

根据您的要求,您只需要简单的http配置,所有人都可以通过公共url访问GET用户,其他人需要基本身份验证。下面的内容适用于您

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers(HttpMethod.GET, "/users").permitAll()
                .anyRequest().authenticated()
                .and()
                .httpBasic();

    }