好吧,我之前在经典的 web.xml 中使用过这种技术,但现在我使用 WebApplicationInitializer 时遇到了问题。
我的 WebApplicationInitializer 包含以下代码:
HttpConstraintElement constraint = new HttpConstraintElement(
TransportGuarantee.NONE,
new String[]{"sponsorUsers"});
ServletSecurityElement servletSecurity =
new ServletSecurityElement(constraint);
dispatcher.setServletSecurity(servletSecurity);
我正在尝试为 servlet 中的任何资源请求的任何 http 方法要求基本身份验证(用户名+密码)。 我得到的只是一个 403 - 没有提示输入用户名。 我怀疑我需要将 auth-method 设置为 BASIC,就像我在 xml 中所做的那样:
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>User Auth</realm-name>
</login-config>
但在 Java 类中看不到等效项。有什么帮助吗?谢谢!
最佳答案
A WebApplicationInitializer
基本上是 Servlet 3.0 的 Spring 扩展 ServletContainerInitializer
.
有一些事情是您不能用 ServletContainerInitializer
做的, 或 ServletContext
更具体地说,其中之一是配置一些安全组件,例如 login-config
.
相反,您可以同时拥有 ServletContainerInitializer
和一个 web.xml
使用属性 metadata-complete
设置为 false
.例如,
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false" version="3.0">
然后在其中添加您的 <login-config>
元素。
关于java - 将 Tomcat Basic Auth 与新的 WebApplicationInitializer 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18875356/