Angular + Spring on same port - spring

I deploy my angular project and add to resources/static in spring project, next I create simple controller:
#Controller
class IndexController {
#RequestMapping(value = "/")
fun index(): String {
return "index"
}
}
And now when I run tomcat and set url to localhost:8080/ everything work well I can click on buttons and browser redirect to proper site, But now if I refresh site or set diffrent url, my angular app stop working and I get 404 or spring endpoints. Question is, how I can set angular as default to load when I type diffrent url than this from controller

You need to separate your API endpoints context. For example /api/**. And then, define an endpoint with a regex, something like this ^/api (all other than /api) to resource path.

Related

How to add custom string "api" after the context in the url in Springboot?

I am creating a test SpringBoot Application, I want all my rest endpoints to have "api" string added to all the urls.
For example :
http://localhost:8080/MyApp/api/students
I tried adding
server.servlet.context-path=/api
but it did not help.
You have basically two options
Option 1.
Global setting which affects all paths from all controllers. Add the following setting in your application.properties file from your Spring app
server.servlet.context-path=/api
Option 2.
Local setting which you can apply to one individual controller and which affects all methods inside it.
#RestController
#RequestMapping(value = "/api")
public class YourController
{

Prevent direct call to my spring boot rest API

I have several REST services (on spring boot) used by a javascript file (website) and these services can be called directly by anyone (and you can see the API url/parameters with the developper console on any browser).
So I would like to prevent the direct calls to the API, exept of course from the front side of my app.
I saw that I can use an API key with spring security but is it reliable? Since I think you can see the key if you intercept the message with developper console.
What you can do are the following :
Disable CORS in your springboot application by setting the following globally or per endpoint as you wish.
To set the CORS per endpoint :
#CrossOrigin(origins = "http://localhost:9000")
#GetMapping("/greeting")
public Test testing(#RequestParam(required=false, defaultValue="Test") String name) {
System.out.println("in test");
return new Testing(10, String.format(template, name));
}
You can use spring security to preauthorize your controller endpoints to make sure that only the authorized has access to the controller.
Like for example :
#RestController
#RequestMapping({"/v2/"})
public class ExampleTestController {
#PreAuthorize("hasAuthority('ROLE_ADMIN')")
#RequestMapping(value = "/test", method = RequestMethod.GET)
String test() {
return "Hello";
}
}
Using spring security is safe, as the user is always validated before access is granted . Even while using Oath2 the key generated is after validating the user login and the key can be used to validate every request to the controller by passing it in the header or using it in the rest template.
Another way of isolating your rest endpoints is by using the load balancer (or ngnix or anything) to block requests to these endpoints from outside your domain.

Configure index.html location in Spring initializr project

I created a Spring boot project using Spring initializr.
The project structure is below. /resources/client is the folder I added manually.
After I ran DemoApplication, I hit localhost:8080 and I saw the home page was pointed to /resources/index.html. I wanted to set the home page to be /resources/client/build/index.html, so I added something in application.properties:
spring.mvc.view.prefix=/resources/client/build/ ### also tried /client/build/
spring.mvc.view.suffix=.html.
However, it did not work and the home page was still pointed to /resources/index.html.
Also, The application is using dispatcherServlet but I did not find a dispatcherServlet file.
Is there any way I can use custom index.html location? Thanks.
Maybe you can try to add a HomeController using java code as following:
#Controller
public class HomeController {
#RequestMapping("/")
public String index() {
return "client/build/index.html";
}
}

Spring MVC doesn't find views after changing port 8080 to 80

I have a tomcat8-spring project. The project works good in HTTP or HTTPS.
To get it working with HTTPS, I had to change in servers.xml the port from 8080 to 80. Now, HTML/JSP pages that were found before are now not being found, and Spring MVC throws 404.
My JSP's are now in src\main\webapp\WEB-INF\views. I tried adding methods in the Controller that return the name of the JSP, but it didn't work. I tried Changing WEB-INF to WebContent and that also didn't work.
Where should my JSP's be? What address should I use to access them?
If it interest those who reads - You can tell tomcat to display webpages.
In your #RequestMapping in the controller, simply return the name and address of the HTML/JSP/JS/... page you want to display.
This is the most general folder structure of the Maven Project if you are using it for Spring MVC.
This is how you generally map to a view through a controller . Try doing the forward slash and see if anything changes. F12 Developer tool in your browser can be of great help for debugging
#Controller
public class HelloController {
#RequestMapping("/hello.htm")
public String handleIndexGet() {
return "/pages/hello"; // forward to view hello.jsp
}
}

Would someone answer some questions about Spring and Request Mapping?

I just started studying Spring, and I'm so confused.
I just created a new 'Spring Legacy Project' at STS. HomeController and home.jsp are there.
When I run it on server, it comes through the HomeController first, and arrives to home.jsp.
#RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
return "home";
}
What makes my project go through the HomeController at the beginning? Should I look at any xml file?
What does value="/" mean in #RequestMapping?
At the home.jsp, I made a button to go 'result.jsp'. From the 'result.jsp' I want to go back to home.jsp. but it doesn't work. What should I do?
<input type="button" value="뒤로 " onclick="javascript:location.href='/views/home.jsp'">
Why isn't this button working? Those two JSP files are in same place.
Your app is deployed to some app server, such as tomcat. The request <app server>/<context root> is handled by the app server to the .war with the appropriate context root, e.g. to your app. Your app uses Spring MVC, so it is Spring's RequestMappingHandlerMapping bean from your .war file that initially handles the request and finds your method that will handle this request. It does so by comparing the path in the request with the value of each method annotated with #RequestMapping.
The annotation #RequestMapping(value="/") of your home() method means that request <app server>/<context root> will be handled by your 'home()' method.
Any request from your JSP will go back to the Spring MVC that will try to map it to the appropriate controller method, i.e. to the method annotated by #RequestMapping with the appropriate path relative to the context root. So if your result.jsp just links to "/", it should bring you to the home() method and then to the home.jsp.

Resources