私は古いjavaスプリングプロジェクトをspringbootにリファクタリングし、伝統的な戦争スタイルで展開します。何らかの理由で、アプリケーションを起動するために従来のweb.xmlに固執する必要があります。 Springbootの遺産のおかげで、私は、web.xmlでこれを達成することができますスプリングエンドアクチュエータがアクチュエータエンドポイントのために401を返す
<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
が必要です。
https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-monitoring.html
特にこのセクション:
おかげでポールは、私は、この問題のために、いくつかの深い理由があるのだろうか。比較として、私はこのプロジェクトをリファクタリングするためにサーブレット3.0スタイルを使用します。つまり、SpringbootServletInitializerから拡張されたクラスを作成します。サーブレット3.0をサポートするWebコンテナによって自動的に検出され、そのクラスを使用してアプリケーションを設定し、 DispatcherServletをインジェクトするなど、springbootによって行われます。このように、 'application.properties'を変更しないで、認証情報を入力した後にエンドポイントを訪問することができます。なぜweb.xmlを使用していないのかわかりません。 – xiangyq000
その点は不明です。たぶんここで説明したようにweb.xmlを捨てるかもしれません: http://www.baeldung.com/spring-xml-vs-java-config – PaulNUK