我有一个Java-webapp。 webapp打包为war-file。这些war文件允许通过HTTP直接传递的静态内容。对于这场战争中的servlet,我可以进行HTTP身份验证(使用servlet本身实现)。但我也想要静态内容的HTTP-auth。我怎么能意识到这一点?

有帮助吗?

解决方案

创建一个实现javax.servlet.Filter的类。请参阅过滤器要点

主要方法是传递ServletRequest,ServletResponse和FilterChain对象的doFilter。这就是您执行身份验证的地方。

然后在web.xml中声明您的过滤器和过滤器映射如下(映射到每个请求)

    <filter>
            <filter-name>Authentication Filter</filter-name>
            <filter-class>
                    com.nfsdsystems.security.filters.AuthenticationFilter</filter-class>
    </filter>
    <filter-mapping>
            <filter-name>Authentication Filter</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>

其他提示

将静态html文件放在目录中,并在web.xml中定义安全性约束。将约束映射到适当的角色。

<security-constraint>
        <display-name>securedResources</display-name>
        <web-resource-collection>
            <web-resource-name>securedRes</web-resource-name>
            <url-pattern>/secured/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>PUT</http-method>
            <http-method>HEAD</http-method>
            <http-method>TRACE</http-method>
            <http-method>POST</http-method>
            <http-method>DELETE</http-method>
            <http-method>OPTIONS</http-method>
        </web-resource-collection>
        <auth-constraint>
            <description>
            authenticatedUser_securedRes</description>
            <role-name>authenticatedUsed</role-name>
        </auth-constraint>
    </security-constraint>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top