I have a Spring Boot app with a single page React app (using React Router v4) hosted by Spring Boot itself. When running it locally, it works perfectly. It is configured to run on port 5000 so going to localhost:5000 redirects to my index.html If I refresh the page, it refreshes the page and displays the same page.
On AWS Elastic Beanstalk 403 Unauthorized doesn't seem to automatically redirect to my index.html page like it does locally though.
I was able to fix this on AWS by adding the following:
@Controller
class ForwardingController : ErrorController {
companion object {
private const val ERROR_PATH = "/error"
private val log = LoggerFactory.getLogger(ForwardingController::class.java)
}
override fun getErrorPath(): String = ERROR_PATH
// This isn't used by the app under normal circumstances, but it is needed on AWS
@RequestMapping(value = ERROR_PATH)
fun errorRedirect(response: HttpServletResponse): String {
log.debug("Redirecting to index.html with error redirect mapping.")
return "forward:/index.html"
}
}
I'm confused about why this is needed though.
I have Spring Security configured with this:
http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.cors().and()
.csrf().disable()
.authorizeRequests()
// Permit all for static resources
.antMatchers("/").permitAll()
.antMatchers("/*.*").permitAll()
.antMatchers("/static/**").permitAll()
// Security for API
.antMatchers("/**/public/**").permitAll()
.antMatchers("/**${PathAuth.ADMIN}/**").hasAuthority(UserType.ADMIN.authority)
.anyRequest().authenticated().and()
.addFilter(JwtAuthenticationFilter(authenticationManager()))
.addFilter(JwtAuthorizationFilter(authenticationManager()))
.exceptionHandling().accessDeniedPage("/index.html")
I expected my accessDeniedPage exception handling to forward 403s to my index.html page. Is there a better way to do this? Why does it work perfectly locally without the ErrorController but fails on AWS Elastic Beanstalk?
The error redirect causes real errors to be incorrectly reported in the browser, so a better solution than my redirect hack would be good!