org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden: "{"odata.error":{"code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"Attempted to perform an unauthorized operation."}}}". I get this error when I'm trying to create connection between thymeleaf website spring boot java and sharepoint. How to solve this problem? Please help me. Almost 3 weeks, I'm struggling to find the solution. Thank you.
Controller
@GetMapping("/sharepoint-data")
public String getSharePointItems(Model model) {
List<SharePoint> items= sharePointService.getListItems();
model.addAttribute("items", items);
return "views/sharepoint-data";
}
private final RestTemplate restTemplate;
public SharePointService(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}
public List<SharePoint> getListItems() {
HttpHeaders headers = new HttpHeaders();
// headers.set("Accept", "application/json");
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<SharePoint[]> response = restTemplate.exchange(
SHAREPOINT_API_URL,
HttpMethod.GET,
entity,
SharePoint[].class
);
if (response.getStatusCode() == HttpStatus.OK) {
SharePoint[] items = response.getBody();
if (items != null) {
return Arrays.asList(items);
}
}
return Collections.emptyList();
}
sharepoint.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SharePoint Items</title>
</head>
<body>
<h1>SharePoint Items</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<!-- Add other column headers if needed -->
</tr>
</thead>
<tbody>
<tr th:each="item : ${items}">
<!-- <td th:text="${item.id}"></td> -->
<td th:text="${item.title}"></td>
<!-- Display other item properties if needed -->
</tr>
</tbody>
</table>
</body>
<script src="/assets/js/sharepoint.js"></script>
</html>
WebSecurityConfig
@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
return http
.authorizeRequests(auth -> {
auth.antMatchers("/index").permitAll();
auth.antMatchers("/assets/**","/css/**","/js/**","/images/**").permitAll();
auth.antMatchers("/index", "/article","/bulletin","/barChart","/calendar","/eventss","/healthdata","/leaderboard","/leave","/leavedet","/newsletter","/upcomingEvent", "/sharepoint-files", "/sharepoint-items", "/sharepoint-data").permitAll();
auth.antMatchers("/newBulletin", "/newEvent", "/newHealthdata", "/newLeave", "/newLeaveDetail",
"/newNewsletter", "/newUpcomingEvent", "/updateLeave", "/newArticle",
"/update", "/updateBulletin", "/updateEvent", "/updateHealthdata", "/updateArticle",
"/updateLeaveDetail", "/updateUpcomingEvent", "/sharepoint-data").permitAll();
auth.antMatchers(HttpMethod.GET, "/article", "/add").permitAll();
//update Option
auth.antMatchers(HttpMethod.GET, "/updateArticle/{id}/**").permitAll();
auth.antMatchers(HttpMethod.GET, "/updateBulletin/{id}/**").permitAll();
//add Option
auth.antMatchers(HttpMethod.GET, "/add").permitAll();
auth.antMatchers(HttpMethod.GET, "/addEvent/**").permitAll();
auth.antMatchers(HttpMethod.GET, "/addNew/**").permitAll();
auth.antMatchers(HttpMethod.GET, "/addBulletin/**").permitAll();
auth.antMatchers(HttpMethod.GET, "/page/{pageNo").permitAll();
//save Option
auth.antMatchers(HttpMethod.POST, "/saveBulletin").permitAll();
auth.antMatchers(HttpMethod.POST, "/saveArticle").permitAll();
//delete Option
auth.antMatchers(HttpMethod.GET, "/deleteBulletin/{id}/**").permitAll();
auth.antMatchers(HttpMethod.GET, "/deleteArticle/{id}/**").permitAll();
auth.antMatchers(HttpMethod.GET, "/deleteEvent/{id}/**").permitAll();
auth.antMatchers(HttpMethod.GET, "/sharepoint-data").permitAll();
auth.anyRequest().authenticated();
})
.csrf(csrf -> csrf.disable())
.formLogin()
.defaultSuccessUrl("/", true)
.loginPage("/index")
.and()
.oauth2Login()
.and()
.build();
}
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/static/**")
.antMatchers("/resources/**")
.antMatchers("/css/**")
.antMatchers("/scripts/**")
.antMatchers("/images/**");
}