I'm trying to throw a ForbiddenException
when a user tries to access a page that needs authentication first. However, it gives me a ClassNotFoundException
instead.
What I wanted to do is handle the ForbiddenException
in an exception handler class and return a proper HTML file(error/403).
I know I can just return the view from the controller but I want to do this via Exception handler. Please suggest how could this be achieved?
I added implementation 'javax.ws.rs:javax.ws.rs-api:2.0'
in build.gradle
import javax.ws.rs.ForbiddenException;
@GetMapping("/mypage")
public ModelAndView displayMypage(ModelAndView modelAndView) {
if (httpSession.getAttribute("token") == null) {
throw new ForbiddenException();
}
String accessToken =
httpSession.getAttribute("token").toString();
// ユーザー情報取得
GitHubTemplate gitHubTemplate = new GitHubTemplate(accessToken);
GitHubUserProfile gitHubUserProfile = gitHubTemplate.userOperations().getUserProfile();
String userName = gitHubUserProfile.getUsername();
// ユーザー情報をセットして、mypageを返す
modelAndView.addObject("userName", userName);
modelAndView.setViewName("mypage");
return modelAndView;
}