I have a Spring boot application with a one-page React application (using React Router v4) hosted by Spring itself. When working on a local computer, it works fine. It is configured to run on the port 5000
, so go to localhost:5000
redirects to mine index.html
. If I refresh the page, it refreshes the page and displays the same page.
In AWS, Elastic Beanstalk 403 Unauthorized does not seem to automatically redirect index.html to my page as it happens locally.
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
@RequestMapping(value = ERROR_PATH)
fun errorRedirect(response: HttpServletResponse): String {
log.debug("Redirecting to index.html with error redirect mapping.")
return "forward:/index.html"
}
}
I am confused about why this is necessary.
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")
, accessDeniedPage
403s index.html. ? ErrorController
, AWS Elastic Beanstalk?
, , , , !