Spring Actuator endpoints not found after changing dispatcherServlet url mapping - spring

I've developed my first rest service with Spring Boot and Spring Actuator. I needed to change the servlet mapping to a different one. So my code is the following in a #Configuration class:
#Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
#Bean
public ServletRegistrationBean dispatcherRegistration(final DispatcherServlet dispatcherServlet) {
final ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
registration.addUrlMappings("/api/*");
return registration;
}
The service endpoint is working fine, as I've excluded basic security from it in application.properties:
security.ignored = **/directdebit/**
The problem is that I can't reach the Actuator endpoints. When I try localhost:8080/api/info I get a 404 Not Found error code and I get an audit event with the following data:
AuditEvent [timestamp=Thu Sep 11 12:12:29 CEST 2014, principal=anonymousUser, type=AUTHORIZATION_FAILURE, data={message=Access is denied, type=org.springframework.security.access.AccessDeniedException}]
UPDATE
After removing Spring Security from classpath, I can reach localhost:8080/api/info. So, the problem is related to the security applied to the management endpoints when Spring Security found in classpath.
UPDATE
Still fighting with this. I've restored String Security to the classpath and from stack trace I can see that the mappings are not found:
2014-09-16 11:01:09.011 DEBUG 780 --- [nio-8080-exec-2] o.s.s.web.util.matcher.OrRequestMatcher : Trying to match using Ant [pattern='/mappings']
2014-09-16 11:01:09.011 DEBUG 780 --- [nio-8080-exec-2] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/api/mappings'; against '/mappings'
2014-09-16 11:01:09.011 DEBUG 780 --- [nio-8080-exec-2] o.s.s.web.util.matcher.OrRequestMatcher : Trying to match using Ant [pattern='/mappings/']
2014-09-16 11:01:09.011 DEBUG 780 --- [nio-8080-exec-2] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/api/mappings'; against '/mappings/'
2014-09-16 11:01:09.011 DEBUG 780 --- [nio-8080-exec-2] o.s.s.web.util.matcher.OrRequestMatcher : Trying to match using Ant [pattern='/mappings.*']
2014-09-16 11:01:09.012 DEBUG 780 --- [nio-8080-exec-2] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/api/mappings'; against '/mappings.*'
2014-09-16 11:01:09.012 DEBUG 780 --- [nio-8080-exec-2] o.s.s.web.util.matcher.OrRequestMatcher : No matches found
2014-09-16 11:01:09.012 DEBUG 780 --- [nio-8080-exec-2] o.s.s.web.util.matcher.OrRequestMatcher : Trying to match using Ant [pattern='/**']
2014-09-16 11:01:09.012 DEBUG 780 --- [nio-8080-exec-2] o.s.s.w.u.matcher.AntPathRequestMatcher : Request '/api/mappings' matched by universal pattern '/**'
2014-09-16 11:01:09.012 DEBUG 780 --- [nio-8080-exec-2] o.s.s.web.util.matcher.OrRequestMatcher : matched
It looks like the management endpoints are broken if the servlet context path is not '/'. So the question is how can I make Actuator management endpoints aware of the servlet context path?.

The Actuator endpoints are mapped differently from the DispatcherServlet. You can change the path of the Actuator endpoints with the application property management.contextPath, which is by default '/'. You should be able to access your endpoints with localhost:8080/info for example.

Related

Hilla not starting Vite - what am I missing?

I have created a hilla app using this:
npx #vaadin/cli init --hilla --auth hilla-with-auth
Works fine!
Now I am trying to add that to an existing spring boot application, but I am having issues with Vite not starting as it should. No exception. No help in the debug output.
I have added:
relevant files in the root (package.json, vite.config.ts, etc.)
vaadin-featureflags.properties in resources folder
hilla dependencies in pom.xml as well as the build plugin
hill annotations to my application class (#Theme, #PWA) and made it extends SpringBootServletInitializer implements AppShellConfigurator
But even if everything seems to be initialized correctly, Vite does not start. Can anyone guide me in the right direction?
This is the most relevant output log:
2022-06-07 08:04:54.046 DEBUG 4947 --- [restartedMain] c.v.f.s.f.s.FullDependenciesScanner :
List of npm dependencies found in the project:
- #hilla/form 1.0.1 dev.hilla.EndpointController
....
2022-06-07 08:04:54.057 DEBUG 4947 --- [restartedMain] c.v.f.s.f.TaskGeneratePackageJson : writing file /Users/michael/Development/Previsto/previsto-server/target/flow-frontend/package.json.
2022-06-07 08:05:02.102 INFO 4947 --- [restartedMain] o.a.container.JSR356AsyncSupport : JSR 356 Mapping path /vaadinServlet
2022-06-07 08:05:02.176 INFO 4947 --- [restartedMain] c.v.f.s.DefaultDeploymentConfiguration :
Vaadin is running in DEBUG MODE.
When deploying application for production, remember to disable debug features. See more from https://vaadin.com/docs/
The following EXPERIMENTAL features are enabled:
- Use Vite for faster front-end builds
2022-06-07 08:05:02.207 DEBUG 4947 --- [restartedMain] c.v.f.s.c.PushRequestHandler : Using pre-initialized Atmosphere for servlet springServlet
2022-06-07 08:05:02.210 DEBUG 4947 --- [restartedMain] c.v.flow.server.VaadinServletService : Using 'com.vaadin.flow.server.communication.IndexHtmlRequestHandler' in client mode bootstrapping
2022-06-07 08:05:02.212 DEBUG 4947 --- [restartedMain] com.vaadin.flow.server.VaadinService : The application has the following routes:
2022-06-07 08:05:02.956 INFO 4947 --- [restartedMain] c.v.flow.server.frontend.FrontendTools : Project node version 16.10.0 is older than 16.14.0. Using node from /Users/michael/.vaadin.
2022-06-07 08:05:03.404 DEBUG 4947 --- [http-nio-8080-exec-1] c.v.f.s.s.VaadinDefaultRequestCache : Saving request to /
2022-06-07 08:05:03.471 DEBUG 4947 --- [http-nio-8080-exec-2] c.v.b.devserver.AbstractDevServerRunner : Requesting resource from Vite http://localhost:0/login
2022-06-07 08:05:03.486 ERROR 4947 --- [http-nio-8080-exec-2] o.a.c.c.C.[.[.[/].[springServlet] : Servlet.service() for servlet [springServlet] threw exception
java.net.ConnectException: Can't assign requested address (connect failed)
at ...

JSP page not rendering with Spring Boot in Spring Tool Suite IDE

I ended up getting this log as a result of running my Spring Boot App with a jsp.
2017-12-29 13:30:52.412 WARN 12631 --- [nio-8080-exec-2]
o.s.web.servlet.PageNotFound No mapping found for HTTP
request with URI [/SpringBootTrial/] in DispatcherServlet with name
'dispatcherServlet'
2017-12-29 13:30:56.463 WARN 12631 --- [nio-8080-exec-3]
o.s.web.servlet.PageNotFound No mapping found for HTTP
request with URI [/SpringBootTrial/display] in DispatcherServlet with
name 'dispatcherServlet'
2017-12-29 13:30:56.464 ERROR 12631 --- [nio-8080-exec-3]
o.s.boot.web.support.ErrorPageFilter : Cannot forward to error
page for request [/welcome] as the response has already been
committed. As a result, the response may have the wrong status code.
If your application is running on WebSphere Application Server you may
be able to resolve this problem by setting
com.ibm.ws.webcontainer.invokeFlushAfterService to false
It's evident that in Spring Boot, manual configuration is not needed. So, how should I fix this error?
I am clueless how to fix. I configured the jsp resolver in application.properties.
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
I have tomcat-embedded-jasper in the pom.xml.

Spring boot 1.5.4 oauth2 Resource Server with Spring boot 1.2.4 oauth2 authorization server

I am new to Spring boot and Spring oauth2 and somehow after googling got examples using spring boot 1.2.4 with mongodb. The following are the setup:
Spring boot 1.2.4 and Spring oauth2 2.0.7 for Authorization Server
Spring boot 1.5.4 and spring oauth2 2.0.7 for Resource Server
The controller code snippet for Resource Server is as below :
#RequestMapping("/hello")
#ResponseBody
#PreAuthorize("hasRole('Dev')")
//#PreAuthorize("hasRole('Dev')")
public String helloWorld() {
return "Hello World.";
}
When resource and authorization servers were spring boot 1.2.4 authorization were working FINE but with different versions it seems to be incompatible, as it throws an error :
{
"error": "access_denied",
"error_description": "Access is denied"
}
When i put the logging in 1.5.4 this is the log result :
2017-06-28 23:25:13 [http-nio-8081-exec-1] DEBUG o.s.s.a.i.a.MethodSecurityInterceptor - Secure object: ReflectiveMethodInvocation: public java.lang.String st.malike.auth.client.http.DemoController.helloWorld(); target is of class [st.malike.auth.client.http.DemoController]; Attributes: [[authorize: '#oauth2.throwOnError(hasRole('Dev'))', filter: 'null', filterTarget: 'null']]
2017-06-28 23:25:13 [http-nio-8081-exec-1] DEBUG o.s.s.a.i.a.MethodSecurityInterceptor - Previously Authenticated: org.springframework.security.oauth2.provider.OAuth2Authentication#1c32aba2: Principal: null; Credentials: [PROTECTED]; Authenticated: true; Details: remoteAddress=0:0:0:0:0:0:0:1, tokenType=BearertokenValue=<TOKEN>; Granted Authorities: Dev
2017-06-28 23:25:13 [http-nio-8081-exec-1] DEBUG o.s.s.access.vote.AffirmativeBased - Voter: org.springframework.security.access.prepost.PreInvocationAuthorizationAdviceVoter#6d6b90, returned: -1
2017-06-28 23:25:13 [http-nio-8081-exec-1] DEBUG o.s.s.access.vote.AffirmativeBased - Voter: org.springframework.security.access.vote.RoleVoter#1deeabb9, returned: 0
2017-06-28 23:25:13 [http-nio-8081-exec-1] DEBUG o.s.s.access.vote.AffirmativeBased - Voter: org.springframework.security.access.vote.AuthenticatedVoter#1c3cd0b, returned: 0
2017-06-28 23:25:13 [http-nio-8081-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'delegatingApplicationListener'
2017-06-28 23:25:13 [http-nio-8081-exec-1] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public java.lang.String st.malike.auth.client.http.DemoController.helloWorld()]: org.springframework.security.access.AccessDeniedException: Access is denied
2017-06-28 23:25:13 [http-nio-8081-exec-1] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [public java.lang.String st.malike.auth.client.http.DemoController.helloWorld()]: org.springframework.security.access.AccessDeniedException: Access is denied
2017-06-28 23:25:13 [http-nio-8081-exec-1] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [public java.lang.String st.malike.auth.client.http.DemoController.helloWorld()]: org.springframework.security.access.AccessDeniedException: Access is denied
2017-06-28 23:25:13 [http-nio-8081-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - Could not complete request
org.springframework.security.access.AccessDeniedException: Access is denied
Now I know the line is causing trouble in Spring-boot-1.5.3
o.s.s.access.vote.AffirmativeBased - Voter: org.springframework.security.access.prepost.PreInvocationAuthorizationAdviceVoter#6d6b90, returned: -1
Please help how to make authorizations with the above setups or how to make the authorization work.
I changed spring-security version to 3.2.10.RELEASE in Resource Server with spring-boot 1.5.3 , all working fine. It seems that spring-security logic in 4.x is changed and is not compatible.

static file not loading in spring boot for web-secure-jdbc

I am using spring boot web secure jdbc sample spring-boot-sample-web-secure-jdbc and want to modify it to load static html files (no template like thymeleaf or jsp needed). I have tried putting the html files under src/main/resources but they are not loading.
I noticed that static file example in samples spring-boot-sample-web-static inherits SpringBootServletInitializer for main class while web secure jdbc use WebMvcConfigurerAdapter. I suppose there might be some more configuration needed to make it work them. Below is my dir tree:
src/main/java/hello/MyController.java
src/main/resources/backupshell.html
src/main/resources/myscripts.js
After moving resources to src/main/resources/resources direct url load works fine but via request url mapping as below is not loading:
#RequestMapping("/admin")
//#ResponseBody
String admin() {
//return "Hello World admin!";
return "backupshell";
}
and logs snippet is as below:
2016-04-18 16:16:51.716 DEBUG 8896 --- [nio-8081-exec-9] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/backupshell] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver#7a9c84a5]]] and 1 interceptor
2016-04-18 16:16:51.716 DEBUG 8896 --- [nio-8081-exec-9] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/backupshell] is: -1
2016-04-18 16:16:51.717 DEBUG 8896 --- [nio-8081-exec-9] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2016-04-18 16:16:51.717 DEBUG 8896 --- [nio-8081-exec-9] o.s.web.servlet.DispatcherServlet : Successfully completed request
2016-04-18 16:16:51.718 DEBUG 8896 --- [nio-8081-exec-9] o.s.web.servlet.DispatcherServlet : Successfully completed request
2016-04-18 16:16:51.718 DEBUG 8896 --- [nio-8081-exec-9] o.s.b.c.web.OrderedRequestContextFilter : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade#24880cb8
2016-04-18 16:16:51.718 DEBUG 8896 --- [nio-8081-exec-9] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2016-04-18 16:16:51.720 DEBUG 8896 --- [nio-8081-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
Regards,
Miten.
The root resource directory src/main/resources/ isn't exposed publicly. As described in this post there are 4 directories which are automatically loaded by Spring Boot to serve static content:
/public/
/static/
/resources/
/META-INF/resources/
In your case you can put the HTML and the JS file inside ,e.g., the /public directory:
src/main/resources/public/backupshell.html
src/main/resources/public/myscripts.js
Then you can access the files with the address of your application, with default configuration it would be:
localhost:8080/backupshell.html
Note that 3rd option /resource/ doesn't mean that src/main/resources/ is exposed. It's the src/main/resources/resources/ directory, which can feel quite cumbersome.

Exclude resources in Spring Cloud Sleuth

I am investigating integrating Spring Cloud Sleuth into a new Spring Boot service we are building and want to exclude certain URL's from generating spans for example health check endpoints which are polled periodically.
Checking the code I can see that the TraceFilter created in the TraceWebAutoConfiguration.java has a DEFAULT_SKIP_PATTERN which can be overridden by setting spring.sleuth.instrument.web.skipPattern.
However even if I access a resource under the DEFAULT_SKIP_PATTERN I am still observing that a trace & span are being created i.e.
17:28:46.142 [XNIO-2 task-1] INFO o.s.c.sleuth.log.Slf4jSpanListener - Starting span: MilliSpan(begin=1454394526140, end=0, name=http/info, traceId=908b24a3-143f-4949-8f86-5df867e3985b, parents=[], spanId=908b24a3-143f-4949-8f86-5df867e3985b, remote=false, exportable=false, annotations={}, processId=null, timelineAnnotations=[])
17:28:46.143 [XNIO-2 task-1] INFO o.s.c.sleuth.log.Slf4jSpanListener - Continued span: MilliSpan(begin=1454394526140, end=0, name=http/info, traceId=908b24a3-143f-4949-8f86-5df867e3985b, parents=[], spanId=908b24a3-143f-4949-8f86-5df867e3985b, remote=false, exportable=false, annotations={}, processId=null, timelineAnnotations=[])
17:28:46.146 [XNIO-2 task-1] INFO io.undertow.servlet - Initializing Spring FrameworkServlet 'dispatcherServlet'
17:28:46.146 [XNIO-2 task-1] INFO o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization started
17:28:46.170 [XNIO-2 task-1] INFO o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 24 ms
17:28:46.216 [XNIO-2 task-1] INFO o.s.c.sleuth.log.Slf4jSpanListener - Stopped span: MilliSpan(begin=1454394526140, end=1454394526215, name=http/info, traceId=908b24a3-143f-4949-8f86-5df867e3985b, parents=[], spanId=908b24a3-143f-4949-8f86-5df867e3985b, remote=false, exportable=false, annotations={}, processId=null, timelineAnnotations=[])
Am I missing something? I am currently using 1.0.0.M4
A skipped resource does not stop a span from being created, it only stops the data from being collected and exported. So your logs look normal to me. In snapshots you would see the "exportable" flag set to false in skipped resources.

Resources