I have a Spring Boot application which acts as a server for my frontend, built by webpack and included into my spring boot web archive.
I use webjars to access my frontend scripts and contents.
But there is one problem. To access webjars resources I need to use pathes like:
/webjars/jar-file-name/resource-name.ext
When in my react-js frontend code I use relateve pathes:
/resource-name.ext
I want to rebind paths of webjars to serve all resources /** from /webjars/jar-file-name
I have used this do to do it https://www.webjars.org/documentation#springmvc, but this seems to not work with Spring Boot
#Configuration
#EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/webjars/jar-file-name/");
}
}
It should work with Spring MVC, but don't work in Spring Boot.
Could you please advice the right way to do it?
I think the following code would resolve your issue:
#Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
Here is my static content serving URL for bootstrap 3.1.0 from tutorial: http://localhost:8080/bootstrap/3.1.0/css/bootstrap.min.css
Related
I have a spring boot app serving angular resources in my static [resources/static] folder. I also am using the same project to serve my JSON-REST-API endpoints.
Hence I have defined my REST api under localhost:9090/api/...
My Angular2 app-build is served under localhost:9090/ui/... via static resources
Now I want to forward all my ui/** urls to ui/index.html/**
How do I do this?
P.S. I have introduced a custom static resource url pattern
spring.mvc.static-path-pattern=/ui/**
Then all my ui/** request will look to the static/**
This way I was able to secure my /api/** and "permitAll" ui/** requests
This simple configuration will do the trick and will even refresh your # routes in angular 2 if enabled.
#Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/ui").setViewName("forward:/ui/index.html");
registry.addViewController("/ui/").setViewName("forward:/ui/index.html");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
Pointers:
- #SpringBootApplication should be annotated where the spring boot app is run
- Do not have #EnableWebMvc as this breaks all auto configuration done by spring boot
- See whether this configuration is under a directory you have marked for
#ComponentScan({"com.foo.config"})
- This implementation is specifically tailored for situations where you have a custom spring.mvc.static-path-pattern defined [ui/]
I have a project in which i am using Spring mvc 4 and i need to include there apache shiro security. I've tried to search over the web for a solution for my problem but didn't managed to fins something. Although there is a guide at Shiro's web site, but this example is only for xml configured Spring project, and my project doesn't contain any xml at all, and when i am trying to configure Shiro with annotation only it fails.
When i run this project i have an access to all of my end points, and non of them is being restricted by Shiro.
I guess that my configuration is wrong but i am unable to figure out which part of it is wrong.
I am not allowed to use Spring boot (i know that there is examples for that).
here is my configuration class:
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = "com.myproject.menu")
public class AppConfiguration extends WebMvcConfigurerAdapter{
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
}
#Bean(name="shiroFilter")
public ShiroFilterFactoryBean shiroFilter (){
ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
shiroFilter.setLoginUrl("/MenuTest/login");
shiroFilter.setFilterChainDefinitions("/MenuTest/init=anon");
shiroFilter.setFilterChainDefinitions("/MenuTest/**=authc");
shiroFilter.setSecurityManager(realm());
return shiroFilter;
}
private DefaultWebSecurityManager realm(){
DefaultWebSecurityManager realm = new DefaultWebSecurityManager();
return realm;
}
}
Thx in advance!
Take a look at the 1.4.0 (RC2) release. There is updated Spring and Spring-Boot support.
And examples in the source tree:
https://github.com/apache/shiro/tree/master/samples/spring-mvc
Playing around with Spring Boot + MVC with static HTML pages, while noticed this thing:
Firstly, what I have:
Index controller:
#Controller
public class IndexController {
#RequestMapping("/")
public String index() {
return "index.html";
}
#RequestMapping("/{path:[^\\.]+}/**")
public String forward() {
return "forward:/";
}
}
The Html file is:...\src\main\resources\static\index.html
So when my main application class is:
#SpringBootApplication
public class MyApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Everything works well and in default path: localhost:8080\ I get index.html page content
But if I annotate Application class with #EnableWebMvc
#SpringBootApplication
#EnableWebMvc
public class MyApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
I get exception: javax.servlet.ServletException: Could not resolve view with name 'index.html' in servlet with name 'dispatcherServlet'
But according to this spring doc it is a valid configuration.
Maybe someone can explain me why? Do I understand something wrong?
According to spring-boot's docs
The auto-configuration adds the following features on top of Spring’s defaults:
Static index.html support.
...
If you want to keep Spring Boot MVC features, and you just want to add
additional MVC configuration (interceptors, formatters, view
controllers etc.) you can add your own #Configuration class of type
WebMvcConfigurerAdapter, but without #EnableWebMvc. If you wish to
provide custom instances of RequestMappingHandlerMapping,
RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver you
can declare a WebMvcRegistrationsAdapter instance providing such
components.
So by adding #EnableWebMvc you just disable what spring-boot autoconfiguring for you. Namely static index.html support.
Actually I think when you choose to use spring boot you should use the default config of spring Boot. It means you just have to edit the file application.properties. Now if you use spring mvc, you have to provide your own servlet. So I think mixing up the to is not a good idea. Either you use spring Boot wiht no much config to do or you use spring mvc and you make all the necessary config.
According to Spring Boot MVC structure, you should locate your html file in the templates folder. Then will be visible for Spring Boot
src\main\resources\templates\index.html
I have SpringBoot 1.3.3 and a working Java API exposed on port 8080. However, along with this API I would like to expose an index.html file located directly under {projectName}/src/main/resources/, according to the Springboot documentation this should be served up automatically but when I navigate to http://localhost:8080/index.html I get a 404. I am not using #EnableWebMVC anywhere and have tried adding my own configuration file like below, however nothing has worked so I'm kind of at a loss. Any help would be much appreciated!
#Configuration
#EnableWebMvc
#ComponentScan
public class ServerConfiguration extends WebMvcAutoConfiguration {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
}
Put the files in /src/main/resources/public/
I'm trying to upgrade my spring mvc project to utilize the new annotations and get rid of my xml. Previously I was loading my static resources in my web.xml with the line:
<mvc:resources mapping="/resources/**" location="/resources/" />
Now, I'm utilizing the WebApplicationInitializer class and #EnableWebMvc annotation to startup my service without any xml files, but can't seem to figure out how to load my resources.
Is there an annotation or new configuration to pull these resources back in without having to use xml?
For Spring 3 & 4:
One way to do this is to have your configuration class extend WebMvcConfigurerAdapter, then override the following method as such:
#Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
Spring 5
As of Spring 5, the correct way to do this is to simply implement the WebMvcConfigurer interface.
For example:
#Configuration
#EnableWebMvc
public class MyApplication implements WebMvcConfigurer {
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
See deprecated message in: WebMvcConfigurerAdapter