[Vuejs]-Enable CORS in Spring Boot – Vue

0👍

Try this approach. It helps me, but I wrote in java, I converted it to kotlin by intellij idea

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
class CorsFilter : Filter {
    @kotlin.Throws(IOException::class, ServletException::class)
    override fun doFilter(req: ServletRequest, res: ServletResponse, 
    chain: FilterChain) {
        val response = res as HttpServletResponse
        response.setHeader("Access-Control-Allow-Origin", "*")
        response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE, PATCH")
        response.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type, cache-control, x-requested-with")
        response.setHeader("Access-Control-Max-Age", "3600")
        if ("OPTIONS".equals((req as HttpServletRequest).method, ignoreCase = true)) {
            response.status = HttpServletResponse.SC_OK
        } else {
            chain.doFilter(req, res)
        }
    }

    override fun destroy() {
        //Not implemented
    }

    @kotlin.Throws(ServletException::class)
    override fun init(config: FilterConfig?) {
        //Not implemented
    }
}

Leave a comment