支持 账户验证

This commit is contained in:
zhouxin 2021-05-25 10:39:58 +08:00
parent 897a279e4f
commit 53ccfd587b
5 changed files with 60 additions and 33 deletions

View File

@ -10,7 +10,7 @@
</parent>
<groupId>com.github.zxbu</groupId>
<artifactId>webdav-aliyundriver</artifactId>
<version>2.1.0</version>
<version>2.2.0</version>
<name>webdav</name>
<description>Demo project for Spring Boot</description>

View File

@ -10,6 +10,7 @@ public class AliYunDriveProperties {
private String workDir = "/etc/aliyun-driver/";
private String agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36";
private String driveId;
private Auth auth;
public String getUrl() {
return url;
@ -58,4 +59,42 @@ public class AliYunDriveProperties {
public void setDriveId(String driveId) {
this.driveId = driveId;
}
public Auth getAuth() {
return auth;
}
public void setAuth(Auth auth) {
this.auth = auth;
}
public static class Auth {
private Boolean enable = true;
private String userName;
private String password;
public Boolean getEnable() {
return enable;
}
public void setEnable(Boolean enable) {
this.enable = enable;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
}

View File

@ -1,12 +1,13 @@
package com.github.zxbu.webdavteambition.config;
import org.apache.catalina.authenticator.DigestAuthenticator;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.realm.GenericPrincipal;
import org.apache.catalina.realm.MessageDigestCredentialHandler;
import org.apache.catalina.realm.RealmBase;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
@ -17,8 +18,12 @@ import java.security.Principal;
import java.util.Collections;
@Component
@ConditionalOnProperty(prefix = "aliyundrive.auth", name = "enable", matchIfMissing = true)
public class EmbeddedTomcatConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>, Ordered {
@Autowired
private AliYunDriveProperties aliYunDriveProperties;
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
@ -29,39 +34,31 @@ public class EmbeddedTomcatConfig implements WebServerFactoryCustomizer<Configur
RealmBase realm = new RealmBase() {
@Override
protected String getPassword(String username) {
return "12345";
if (aliYunDriveProperties.getAuth().getUserName().equals(username)) {
return aliYunDriveProperties.getAuth().getPassword();
}
return "";
}
@Override
protected Principal getPrincipal(String username) {
return new GenericPrincipal(username, "12345", Collections.singletonList("*"));
return new GenericPrincipal(username, aliYunDriveProperties.getAuth().getPassword(), Collections.singletonList("**"));
}
};
MessageDigestCredentialHandler credentialHandler = new MessageDigestCredentialHandler();
// try {
// credentialHandler.setAlgorithm("md5");
// } catch (NoSuchAlgorithmException e) {
// e.printStackTrace();
// }
realm.setCredentialHandler(credentialHandler);
context.setRealm(realm);
DigestAuthenticator valve = new DigestAuthenticator();
DigestAuthenticator digestAuthenticator = new DigestAuthenticator();
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setAuthConstraint(true);
securityConstraint.addAuthRole("*");
// securityConstraint.setUserConstraint("CONFIDENTIAL");
securityConstraint.addAuthRole("**");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
StandardContext standardContext = (StandardContext) context;
context.getPipeline().addValve(valve);
context.getPipeline().addValve(digestAuthenticator);
});
}

View File

@ -42,18 +42,6 @@ public class AliYunDriverFileSystemStore implements IWebdavStore {
@Override
public ITransaction begin(Principal principal, HttpServletRequest req, HttpServletResponse resp) {
LOGGER.debug("begin");
Enumeration<String> headerNames = req.getHeaderNames();
System.out.println(req.getMethod() + " " + req.getRequestURI());
System.out.println("principal:" + principal);
while (headerNames.hasMoreElements()) {
String s = headerNames.nextElement();
System.out.println(s + ":" + req.getHeader(s));
}
System.out.println("-----------------------");
System.out.println();
aliYunDriverClientService.clearCache();
return new Transaction(principal, req, resp);
}
@ -61,7 +49,6 @@ public class AliYunDriverFileSystemStore implements IWebdavStore {
@Override
public void checkAuthentication(ITransaction transaction) {
LOGGER.debug("checkAuthentication");
HttpServletRequest req = transaction.getRequest();
if (transaction.getPrincipal() == null) {
throw new UnauthenticatedException(WebdavStatus.SC_UNAUTHORIZED);
}

View File

@ -1,3 +1,7 @@
logging.level.net.sf.webdav=trace
#logging.level.net.sf.webdav=trace
logging.file.path=/var/log/
logging.file.name=webdav.log
aliyundrive.auth.enable=true
aliyundrive.auth.user-name=admin
aliyundrive.auth.password=admin