如何禁用特定 Rest 控制器的 Spring 引导身份验证控制

How to disable Spring Boot Authentication control for a particular Rest Controller

我在客户端使用 Angular 并在后端使用 Spring Boot 创建了一个 CRUD 应用程序。然后我用 Okta 实现了身份验证,一切正常。 问题是我想从我的主页的数据库中检索一些数据,并显示这些信息,即使没有用户经过身份验证,但我一直收到 401 未经授权的 http 响应。

我在 Whosebug 中阅读了一些其他问题,这些是我在 Spring 服务器中尝试的所有配置:

public class SecurityConfig extends WebSecurityConfigurerAdapter{

1)

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.authorizeRequests().anyRequest().authenticated().and().oauth2Client().and().oauth2Login()
            .and().csrf().ignoringAntMatchers("/stats/**");
    http.cors();
}

2)

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.authorizeRequests().antMatchers("/stats/**").permitAll().anyRequest().authenticated().and().oauth2Client().and().oauth2Login();
    http.cors();
}

它们不起作用。这是我尝试联系我的 GET 方法的 @RestController:

@RestController
@RequestMapping("/stats")
@CrossOrigin("http://localhost:4200")
public class StatsController {

@Autowired
private CompanyManagerService companyManagerService;
@Autowired
private ReservationService reservationService;
@Autowired
private ReviewService reviewService;
@Autowired
private UserService userService;


@GetMapping("/company")
public ResponseEntity getCompanyCount(){

    return new ResponseEntity(companyManagerService.getCompanyCount(), HttpStatus.OK);
}

@GetMapping("/field")
public ResponseEntity getFieldCount(){

    return new ResponseEntity(companyManagerService.getFieldCount(), HttpStatus.OK);
}

@GetMapping("/reservation")
public ResponseEntity getReservationCount(){

    return new ResponseEntity(reservationService.getCount(), HttpStatus.OK);
}

@GetMapping("/review")
public ResponseEntity getReviewCount(){

    return new ResponseEntity(reviewService.getCount(), HttpStatus.OK);
}

我该如何解决这种情况?

将配置方法更新为此并尝试

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/stats/**")
        .permitAll()
        .anyRequest()
        .authenticated()
        .and()
        .oauth2Client()
        .and()
        .oauth2Login();
  }
  • 确保您的安全配置 class SecurityConfig 带有 @EnableWebSecurity@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
  • 注释
  • 安全配置 class 遵循包结构并由 Spring 扫描。 spring-component-scanning
  • 应用程序没有上下文路径,如果您使用上下文路径,那么您可以使用 **/stats/**/context-path/stats/**

您可以使用 configure(WebSecurity web) 来绕过安全过滤器链,您将能够访问 GET API

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

如果您同时使用 configure(WebSecurity web)configure(HttpSecurity http) 方法,请确保将 configure(WebSecurity web) 置于 之上按照描述配置(HttpSecurity http)

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

     // Other Configurations...

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

    @Override
    public void configure(HttpSecurity http) throws Exception {

        http
            .csrf().disable().cors().disable()
            .authorizeRequests()
            .antMatchers("**/stats/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .oauth2Client()
            .and()
            .oauth2Login();
      }

     // Other Configurations...

}