tomcat搭建webDav
工具准备: tomcat-8.0.53
前言:由于工作需要,希望自己搭建webdav服务端,刚开始用windowsServer 2012 R2版本自带的IIS服务器搭建,搭建好之后浏览器可以访问,但无法添加网络位置,查询很多资料最终无果。
只好换另一种方式搭建,查询资料后决定用tomcat老搭建webdav,参考教程https://www.cnblogs.com/EasonJim/p/6859345.html搭建后,成功搭建webDav服务,在windows上也可以
添加网络映射,但又发现浏览器不用输入密码就可以访问,这样安全性完全没有保障。 查询了tomcat web.xml安全配置,终于解决了问题。需要在security-constraint节点下添加如下图
黄色部分的配置,重启服务,浏览器访问出现安全验证。
下面专门整理了完整的步骤,作为学习归档,同时献给需要搭建webdav的朋友们,节约搭建时间。
1、在Tomcat的webapps目录下新建webdav文件夹,并在此文件夹下新建WEB-INF\web.xml文件
完整的文件目录为:D:\apache-tomcat-8.0.53\webapps\webdav\WEB-INF\web.xml
2.添加web配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>webdav</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>webdav</servlet-name> <servlet-class>org.apache.catalina.servlets.WebdavServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>true</param-value> </init-param> <!-- Read-Write Access Settings --> <init-param> <param-name>readonly</param-name> <param-value>false</param-value> </init-param> </servlet> <!-- URL Mapping --> <servlet-mapping> <servlet-name>webdav</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <security-constraint> <web-resource-collection> <web-resource-name>webdav</web-resource-name> <!-- Detect WebDAV Methods in URL For Whole Application --> <url-pattern>/*</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> <!-- <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> --> <http-method>PROPFIND</http-method> <http-method>PROPPATCH</http-method> <http-method>COPY</http-method> <http-method>MOVE</http-method> <http-method>LOCK</http-method> <http-method>UNLOCK</http-method> </web-resource-collection> <!-- Restrict access by role --> <auth-constraint> <role-name>*</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> <realm-name>webdav</realm-name> </login-config> <security-role> <description>WebDAV User</description> <role-name>webdav</role-name> </security-role> </web-app>
提示如果通过流浪器访问时不需要用户名密码,将web.xml中以下配置删除后重启服务即可
<http-method>GET</http-method>
<http-method>POST</http-method>
3.添加用户
<role-name>为自定义权限名称。
根据上面权限名称,在Tomcat账号体系中增加账号密码,配置如下:
打开D:\apache-tomcat-8.0.53\conf\tomcat-users.xml
<role rolename="webdav"/> <user username="admin" password="admin" roles="webdav"/>
重启tomcat后访问http:127.0.0.1:8080/webdav,出现以下信息表示成功
输入账号密码后,显示如下
参考1:https://www.cnblogs.com/EasonJim/p/6859345.html
参考2:https://www.cnblogs.com/ansen/articles/2017360.html