Hi I am trying to include a front-end in the /resources folder of my application.
My directory structure is as follows:
src
main
java // backend located here
webapp
resource //my html/css/js
I am using oauth for security of the backend:
MethodSecurityConfig.java
#Configuration
#EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
#Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
return new OAuth2MethodSecurityExpressionHandler();
}
}
ResourceServerConfigurerAdapter.java
#Configuration
#EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
#Override
public void configure(final HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/resources/**").permitAll().and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.and().authorizeRequests().anyRequest().authenticated();
}
}
ResourceWebConfig.java
#Configuration
#EnableWebMvc
#ComponentScan({"eu.emif.resource.web"})
public class ResourceWebConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("(/resources/");
}
}
Whenever I try to go to http://localhost:8082/style.css
Or http://localhost:8082/resources/style.css
I am getting
<oauth>
<error_description>
Full authentication is required to access this resource
</error_description>
<error>unauthorized</error>
</oauth>
Help would be much appreciated.
Change you directory structure to as follows:
src
main
java // backend located here
resources(By default these files are not added in deployment assembly and mostly used for loading java resources like xml file,properies file etc.)
webapp
resource(create new folder here and put your frontend(html/js/css) files here)
Related
I need to serve static index.html page from "/" GET request in spring-web.
My module is included into bigger one, packaged as WAR and deployed to tomcat.
I've tried
#Configuration
#EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
System.out.println("WebMvcConfigurer - addResourceHandlers() function get loaded...");
registry.addResourceHandler("/WEB-INF/classes/index.html")
.addResourceLocations("/WEB-INF/classes/index.html");
}
}
and placed index.html in resources folder. Still 404. Can anybody help me out with understanding what have I done wrong?
The pattern of the ResourceHandler should reflect the query path, in your case /index.html
Resources inside a jar/war /WEB-INF/classes/ can be accessed via classpath:, in your case classpath:/index.html
So your config should have been like this
#Configuration
#EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/index.html")
.addResourceLocations("classpath:/index.html");
}
}
You can simplify if you have more resources with
registry.addResourceHandler("/*.html")
.addResourceLocations("classpath:/");
I have searching the web for hours for this solution. I want to have access on localhost/index.html but not in localhost/layout/style.css.
My static resources are from external folder and the filee tree goes as the obvious:
->site
->layout
->style.css
->index.html
So far, all things i have tried, either disable access to index.html, either both index and style.css have access.
The thing i found online that makes the most sense is:
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/*.html").permitAll();
// http.authorizeRequests().antMatchers("/**").permitAll();
super.configure(http);
}
#Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/layout/**");
super.configure(web);
}
but when i use localhot/layout/style.css it shows the css file.
my WebMvcConfigurerAdapter:
#Configuration
public class StaticResourceConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("file:mylocation/site/");
}
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("redirect:/index.html");
}
}
Annotations on start up:
#SpringBootApplication
#EnableWebMvc
public class Main extends SpringBootServletInitializer
What would be the way to be able to see index.html but not all the css files inside layout folder?
P.S: Maybe this is not even possible?
In my Maven project with Spring Boot I have a following folder with a static content(Html, CSS, JavaScript files):
/src/main/webapp/docs/
this is my application properties:
server.port: 8080
server.contextPath: /admin-api
When a user access a following url: http://localhost:8080/admin-api/docs I want to show him a static content from /src/main/webapp/docs/ folder.
I'm trying to configure ResourceHandler in my WebMvcConfigurerAdapter but it doesn't work:
#EnableWebMvc
#Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String[] staticResourceMappingPath = { "/docs/", "classpath:/docs/" };
registry.addResourceHandler("/docs").addResourceLocations(staticResourceMappingPath);
}
...
}
http://localhost:8080/admin-api/docs returns 404 Not Found.
What am I doing wrong and how to properly configure it ?
Your addResourceHandlers method should be like below.
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/docs/**").addResourceLocations("/docs/");
}
Now if you have a static page say index.html in docs folder then you can access it using http://localhost:8080/admin-api/docs/index.html
I have a Spring Rest controller and its NOT spring boot application. Its just a REST API Project. I want to integrate swagger2 in my project. I tried all the examples in Net and in demo but with no luck. When i try to execute http://localhost:8085/context/swagger-ui.html i get 404 error. Please find my confugration below and let me know if there is any discrepencies. Any help is highly appreciated.
jars - under /WEB-INF/lib
google-collections-1.0.jar
springfox-core-2.2.2.jar
springfox-schema-2.2.2.jar
springfox-spi-2.2.2.jar
springfox-spring-web-2.2.2.jar
springfox-staticdocs-2.2.2.jar
springfox-swagger-common-2.2.2.jar
springfox-swagger-ui-2.2.2.jar
springfox-swagger2-2.2.2.jar
My swagger config class -
#EnableSwagger2
public class SwaggerConfiguration {
}
My springconfig class
#EnableWebMvc
#ComponentScan(basePackageClasses = controller.class)
#Import(SwaggerConfiguration.class)
public class SpringConfiguration extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
My Application initializer class below as per springfox-java demo . I tried with and without the below class and its not working either way.
Application Initializer class
public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{controller.class};
}
#Override
protected String[] getServletMappings() {
return new String[]{"/*"};
}
}
I can access my rest urls but not swagger-ui.html in the same context.
Please let me know what i am missing here?
I add the manual selection of controllers:
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("my.package.to.api"))
.paths(regex("/product.*")) //optional
.build();
}
}
given details are not sufficient to reproduce/analyse the issue.
But, today I faced similar problem, ofcourse used SpringBoot, and solved myself as below:
as my sample have one controller class and an application class having main method, I created packages as below, and it got solved:
hello
controllers
HelloController
swagger
SwaggerConfig2
HelloApplication
I'm receiving a 404 error when accessing a particular page in my spring boot web application.
The strange thing is that I don't receive that error when the resource is mapped to a different location.
#RequestMapping(value="report", method = RequestMethod.GET)
public String getReportPage() {
return "templates/report.html";
}
works just fine while
#RequestMapping(value="report/{uuid}", method = RequestMethod.GET)
public String getReportPage() {
return "templates/report.html";
}
does not. I need the uuid parameter for my angular service so I cannot simply remove that from the path. I've tried adding the path variable to the model; that makes no difference.
The directory structure is set up as follows:
webapp
resources
...
templates
report.html
The configuration is pretty much an out of the box spring boot with some added resource handlers and some basic security:
#Configuration
public class MvcConfiguration extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/", "file:resources/");
}
}
#Configuration
#EnableWebSecurity
class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and()
.csrf().disable();
}
}
#Configuration
#EnableGlobalMethodSecurity(prePostEnabled = true)
class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {
#Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService());
auth.authenticationProvider(authProvider());
}
.... custom user details service and authentication provider ...
}
Any thoughts about what may be causing this issue?
Edit: After some further investigation, it looks like anything mapped beyond the first level doesn't work for the web controller (but the rest controllers are working just fine). For example, a mapping with the value /web/report doesn't work either.
While looking through debug messages I found that the application was looking for the pages in the wrong place:
DEBUG : Looking up handler method for path /report/templates/report.html
Which is why only top level requests were working.
Changing the mapping as such:
#RequestMapping(value="report/{uuid}", method = RequestMethod.GET)
public String getReportPage() {
return "/templates/report.html";
}
fixed the problem.