简体   繁体   中英

A Problem while deploying Spring Boot + Angular application

I created an Angular application with a Spring Boot backend, currently I work locally by running the server on Intellij and the angular App with "ng serve", I want to deploy my app to Heroku.

I searched on the internet, and I saw that one of the first steps to do that is to build the Angular app and copy the "dist" content to "src/main/resources/static".

I copied the content, then tried to run the server (still working locally) and access the app from the browser at this address: " http://localhost:3000/ " (I defined 3000 as the Spring Boot app port).

Unfortunately, I am not seeing my Angular app, what I am seeing is the "Whitelabel Error Page"

 This application has no explicit mapping for /error, so you are seeing this as a fallback. Sat Nov 09 17:26:21 IST 2019 There was an unexpected error (type=Forbidden, status=403). Access Denied

I assume that I am getting "access denied" error because I have a "JWTAuthorizationFilter" on my API - consumers of the API must provide a valid JSON web token in the request header.

My question is: What should I do in order to cause the " http://localhost:3000/ " return the Angular App and not access the API, what am I missing here?

My App has two controllers (@RestController), that have a couple of GET\POST\DELETE\PUT requests.

And I configured a JWTAuthenticationFilter and JWTAuthorizationFilter.

I still don't understand how the separation between the angular application and the Spring Rest API is expressed in the Java spring project deployment.

Can you please help me fix this issue, or guide me where can I find an explanation about this?

What you are trying to do is to serve the Angular App from the spring boot tomcat embedded servlet container, therefore the security configuration affects the endpoints where the static angular files are.

To solve your problem, in your security configuration class (class extending WebSecurityConfigurerAdapter) you could only restrict your actual api endpoints, for example:

http.authorizeRequests().antMatchers( "/api/**").authenticated();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM