I made a CRUD app using Angular on client side and Spring Boot on the backend. I implemented then authentication with Okta and everything works fine. The problem is that I want to retrieve some data from database for my homepage, and show those info even if no user is authenticated, but I keep receiving a 401 Unauthorized http response.
I read some other question here in stackoverflow and these are all the configuration that I tried in my Spring server:
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();
}
They don't work. This is the @RestController that I'm trying to contact for my GET methods:
@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);
}
How can I solve this situation?