【问题标题】:springboot actuator returns 401 for actuator endpointsspringboot 执行器为执行器端点返回 401
【发布时间】:2018-04-12 08:48:05
【问题描述】:

我正在将一个旧的 java spring 项目重构为 springboot,并以传统的战争风格进行部署。出于某种原因,我必须坚持使用传统的 web.xml 来启动应用程序。感谢 Springboot legacy,我可以通过 web.xml 实现这一点:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.MyServerServletConfig</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>myservlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

另外,我添加了springboot执行器依赖。 application.properties 如下:

endpoints.enabled=true
endpoints.sensitive=true
management.security.enabled=true
management.context-path=/manage
security.user.password=myserver
security.user.name=myserver

应用程序可以正常启动,但是当我尝试从浏览器访问端点时,它只返回一个401 Full authentication is required to access this resource 错误,而不要求我输入用户名和密码。相关日志如下:

16:00:04.264 DEBUG [resin-port-8081-19] org.springframework.web.servlet.DispatcherServlet: DispatcherServlet with name 'myservlet' processing GET request for [/manage/health]
16:00:04.264 DEBUG [resin-port-8081-19] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping: Looking up handler method for path /manage/health
16:00:04.264 DEBUG [resin-port-8081-19] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping: Returning handler method [public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(javax.servlet.http.HttpServletRequest,java.security.Principal)]
16:00:04.264 DEBUG [resin-port-8081-19] org.springframework.web.servlet.DispatcherServlet: Last-Modified value for [/manage/health] is: -1
16:00:04.264 DEBUG [resin-port-8081-19] org.springframework.core.env.PropertySourcesPropertyResolver: Found key 'endpoints.sensitive' in [applicationConfig: [classpath:../conf/application.properties]] with type [String]
16:00:04.265 DEBUG [resin-port-8081-19] org.springframework.web.servlet.DispatcherServlet: Successfully completed request

这个问题的原因可能是什么? springboot 自动构建的 DispatherServlet 与 web.xml 中明确定义的 DispatherServlet 之间有什么区别吗?出于安全考虑,management.security.enabled=true 是必须的。

【问题讨论】:

    标签: spring spring-boot spring-security spring-boot-actuator


    【解决方案1】:

    请参阅 Spring Boot 手册的这一部分:

    https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-monitoring.html

    特别是本节:

    48.1 访问敏感端点

    通过将端点标记为敏感,除非您以适当的角色登录,否则您无法看到它们。所以你必须为你的敏感端点设置 Spring Security。

    这里有一篇很好的博客文章介绍了如何做到这一点:

    http://www.devglan.com/spring-security/securing-spring-boot-actuator-endpoints-with-spring-security

    【讨论】:

    • 谢谢保罗,但我想知道这个问题有一些更深层次的原因。作为对比,我使用 servlet 3.0 风格来重构这个项目,即编写一个从 SpringbootServletInitializer 扩展而来的类,它可以被支持 servlet 3.0 的 Web 容器自动检测,并使用该类来配置应用程序并留下其他工作由springboot完成,比如注入一个DispatcherServlet。这样,application.properties 不变,输入认证信息后就可以访问端点。我不知道为什么它在使用 web.xml 时不起作用。
    • 不确定这一点。也许只是放弃 web.xml,如下所述:baeldung.com/spring-xml-vs-java-config
    猜你喜欢
    • 1970-01-01
    • 2020-11-14
    • 2021-08-19
    • 2018-12-06
    • 1970-01-01
    • 2018-01-07
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多