使用Tomcat Basic Auth和新的WebApplicationInitializer

时间:2013-09-18 14:43:46

标签: java spring tomcat servlets

好的,所以我以前在经典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方法设置为BASIC,就像在xml中一样:

<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>User Auth</realm-name>
</login-config>

但是没有看到Java类中的等价物。有帮助吗?谢谢!

1 个答案:

答案 0 :(得分:4)

WebApplicationInitializer基本上是Servlet 3.0 ServletContainerInitializer的Spring扩展。

ServletContainerInitializerServletContext更具体的一些事情是你不能做的,其中一个是配置一些安全组件,login-config

相反,您可以使用设置为ServletContainerInitializer的属性web.xml同时拥有metadata-completefalse。例如,

<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>元素。

相关问题