gpt4 book ai didi

java - 将 Tomcat Basic Auth 与新的 WebApplicationInitializer 结合使用

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:47 25 4
gpt4 key购买 nike

好吧,我之前在经典的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com