Access javascript files present inside JAR in parent springboot project - java

I have to provide static .js files through a JAR project.
For this I created a JAR(using MAVEN) which looks like:
parent
|--com
|--META-INF
|--webapp
|--resources
|--js
|--myjs.js
Now, I added this JAR to my parent spring-boot project and in one of the JSPs added
<script src="resources/js/myjs.js"></script>
This gives me a 404 error.
My conclusion: Either the boot project does not merge webapp folders from JARs into its own webapp or I am accessing the file incorrectly.
These questions did not help(cannot change WebMvcConfigurerAdapter and java code is strictly not allowed ):
SpringBoot - accessing a file inside resources folder
Serve static resources within jar files by Spring boot
Usually, such things are handled using overlays but that would be and overkill for a simple use case.

Your webapp folder is secured folder.You cannot access files directly inside it.
You need to permit all your incoming requests of js and other static files as following:-
#Configuration
#EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/js/**", "/css/**").permitAll();
http.csrf().disable();
}
}

Related

Launch Spring Boot WicketWebApplication inside another java Application without main

i want to use Spring Boot with Spring Security and Apache Wicket 8.0 as web view.
It`s easy to do it with SpringBoot and just simply run jar file, but i want to do something like this:
Minecraft Server (Spigot, Bukkit, etc..) Scanning /plugins/ folder, and looking for .jar files.
All files must contain plugin.yml in root of .jar archive. This plugin.yml file contains a path to class file, for example - BukkitMain and this class MUST extend JavaPlugin.
Then MinecraftServer core is executing onEnable() method of BukkitMain.class (so, if we override this method - server code will execute all code inside this method)
For Example:
public class BukkitMain extends JavaPlugin {
#Override
public void onEnable(){
//SpringApplicationBuilder.run(....);
}
}
So, i can use Spring-boot-plugin in build part of maven .pom file, but it's packing all class files and resources to BOOT-INF inside jar and it's requiring main(String args[]) method to run SpringApplication. It`s bad variant, because i can't access my BukkitMain to run it.
When i use Maven Compiler Plugin and packing it to jar - it's all ok, but there is new problem. My BukkitMain trying to run SpringApplicationBuilder, but i'm getting ClassNotFoundException, cause there is no SpringApplicationBuilder inside jar.

Static web resources in Spring Boot + Spring Security + Thymeleaf

I'm trying to make Spring Security permit access to static resources to all users, but for now nothing works.
When I used jsp in previous project, the solution was simple:
http
.authorizeRequests()
.antMatchers("/static/**").permitAll()
.anyRequest().authenticated()
Static folder was placed inside webapp folder which was the root folder and was easily detected by Spring Security. Now, because of Thymeleaf, there is no webapp folder and all the static folders are placed into src/main/resources. I have no idea, how to create antMatcher for something that is inside resources folder... I tried that:
http
.authorizeRequests()
.antMatchers("resources:/static/**").permitAll()
.anyRequest().authenticated()
It never worked. What is the solution?
ps. I have seen a statement that Spring Boot + Spring Security allows this intra-resources access by default, but it does not.
The solution is found. For my folder structure src/main/resource/static/css I should have used
.antMatchers("/css/**").permitAll()
instead of
.antMatchers("/static/**").permitAll()
Check my answer there: Spring boot mapping static html
Basically you have to add resource handlers by extending WebMvcConfigurerAdapter to map http://yoursite/static_url_prefix to your static_app_dir directory in your resources directory.
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static_url_prefix/**").addResourceLocations("classpath:/static_app_dir/");
super.addResourceHandlers(registry);
}
This will intercept all request coming to http://yoursite/static_url_prefix and return results from classpath://static_app_dir in your jar file or from /resources/static_app_dir when running application from your IDE.
Spring security can be configured as before as it has nothing to do with it i.e. your first code example seems correct.

Mapping my Spring boot application to a HTML file on local host

I'm trying to link a HTML page to my Spring boot application.
I've connected a SQL DB to it, and have set up the needed controllers, but cannot map the HTML page to the local host.
Here is the GIT for the project.
https://github.com/ThierryLucDenichaud/SpringBoot_SQL_SPRING_HTML.git
Create a configuration file that look like the following:
#Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("indexPage.html");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
This will create a default controller for serving your index.html on /.
A little recommendation, change indexPage.html to index.html and also setViewName("indexPage.html") to setViewName("index.html") as index.html is usually the default in most systems and frameworks.
EDIT
I just noticed your public folder is in src/main while it should be in src/main/resources. spring boot won't handle your static files unless they are first in src/main/resources then in public as the default folder that is exposed to the outside world.
Also in Java you should place classpath resources in src/main/resources or src/test/resources for tests.

Running Spring Boot application through Eclipse picks up test classes

I am developing a Spring Boot application using STS with the Gradle plugin.
I have a different configuration for tests, to prevent our Selenium tests from having to login.
So in src/test/java/etc I have something like this:
#Configuration
#EnableGlobalMethodSecurity(prePostEnabled = true)
#EnableWebSecurity
public static class SecurityConfig extends WebSecurityConfigurerAdapter
{
#Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests().anyRequest().permitAll();
}
}
Whereas in src/main/java I have an equivalent class that configures login etc, requiring login for all pages.
If I run the application through the Gradle plugin (bootRun), everything works fine.
However, if I run or debug it through Eclipse directly (e.g. right clicking on the project, Run As->Spring Boot App or by clicking
the run/debug buttons in the Spring or Java view) then the test config is applied, so access is granted to all pages without login.
I'm guessing that the test classes are being included in the classpath when I start the application this way.
Is there an easy way to prevent this from happening?
When you run the test from eclipse, the classpath is prepared by eclipse (and not by maven or gradle).
Eclipse only uses one classpath per project and does not know anything about dependency scopes (like 'compile' or 'test').
So the classpath always contains any resources of a referenced project.
You cannot change this behavior of eclipse.
You need to use naming conventions, profile etc. to avoid accidental use of test resources.
You can append #TestComponent to you test configuration class. These bean configurations will be skipped during component scan of your application. Depending on the component scan configuration, you need to define an #ComponentScan exclude filter:
excludeFilters = #ComponentScan.Filter(value = TestComponent.class, type = FilterType.ANNOTATION))

Dynamic Web Application - platform with components

I am doing a research on how to make a proper structure for my web application.
It will be a web application serving as a platform for additional, independent components.
The components must be able to map requests by using the #Controller annotaion.
So far I have learned, that:
The platform will be deployed as a .war file on Tomcat.
The platform classpath location will contain components in a form of .jar files.
My question is:
How to setup the components and the platform, so that platform will make use of the components' #Controllers?
So far I have the platform.war running on Tomcat. It is annotation based Spring configuration.
I also have the first component, it is a single Java class with #Controller annotation and first mapping. For some reason when I include this component in the classpath of the platform and try to access the url mapped in the component, the application returns 404 error. In the log files it says "No mapping found for HTTP request" so it does not initialize the component's #Controller.
For further explanation click here.
In your JAR file, create a package defining your namespace, i.e: "com.platformproject.web". Then all you need to do is put the JAR file in WEB-INF/lib (or better use Maven Modules) and scan the annotations at startup:
MvcConfig.java
#EnableWebMvc
#Configuration
#ComponentScan(basePackages = { "com.platformproject.web" })
public class MvcConfig extends WebMvcConfigurerAdapter { ... }

Categories