James Hao
James Hao

Reputation: 905

jhipster refresh url cause "Cannot GET /user-management"

I create a jhipster app successfully, then login with admin/admin, click user-management, all works, the url changes to localhost:9000/user-management.

However, when I refresh url using chrome refresh button, the page broken with message: "Cannot GET /user-management", after press F12 to launch debugger, it has below error message in console:

Refused to execute inline script because it violates the following Content Security Policy directive: "default-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-GKWAMtgBzlCzmucztJIeDl/kD0MKNqAT5HDcFIff2+A='), or a nonce ('nonce-...') is required to enable inline execution. Note also that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.

Please help, thanks in advance.

Note: yarn v1.3.2, "@angular/core": "5.2.0", java 1.8, jwt, elastic search, Chinese, mysql, angular 5

Upvotes: 0

Views: 1735

Answers (1)

Gaël Marziou
Gaël Marziou

Reputation: 16294

If you set useHash: false and click on refresh, the request is sent to the server so you get this exact error you had: a client route being processed by server and not found. So you must adapt the server side using a servlet filter, see details in https://github.com/jhipster/generator-jhipster/issues/4794#issuecomment-304097246

Please note also that this approach does not easily work for gateways in microservices architecture.

Here is an example of such a filter that you can adapt and which forwards requests for client routes to '/' so that they get interpreted by the angular app in index.html:

public class AngularRouteFilter extends OncePerRequestFilter {

    // add the values you want to redirect for
    private static final Pattern PATTERN = Pattern.compile("^/((api|swagger-ui|management|swagger-resources)/|favicon\\.ico|v2/api-docs).*");

    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain filterChain)
        throws ServletException, IOException {
        if (isServerRoute(request)) {
            filterChain.doFilter(request, response);
        } else {
            RequestDispatcher rd = request.getRequestDispatcher("/");
            rd.forward(request, response);
        }
    }

    protected static boolean isServerRoute(HttpServletRequest request) {
        if (request.getMethod().equals("GET")) {
            String uri = request.getRequestURI();
            return PATTERN.matcher(uri).matches();
        }
        return true;
    }
}

Upvotes: 3

Related Questions