zxbu
4 years ago
committed by
GitHub
9 changed files with 183 additions and 16 deletions
@ -0,0 +1,70 @@
@@ -0,0 +1,70 @@
|
||||
package com.github.zxbu.webdavteambition.config; |
||||
|
||||
import org.apache.catalina.authenticator.DigestAuthenticator; |
||||
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; |
||||
import org.springframework.core.Ordered; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
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) { |
||||
|
||||
TomcatServletWebServerFactory tomcatServletWebServerFactory = (TomcatServletWebServerFactory) factory; |
||||
|
||||
tomcatServletWebServerFactory.addContextCustomizers(context -> { |
||||
|
||||
RealmBase realm = new RealmBase() { |
||||
@Override |
||||
protected String getPassword(String username) { |
||||
if (aliYunDriveProperties.getAuth().getUserName().equals(username)) { |
||||
return aliYunDriveProperties.getAuth().getPassword(); |
||||
} |
||||
return ""; |
||||
} |
||||
|
||||
@Override |
||||
protected Principal getPrincipal(String username) { |
||||
return new GenericPrincipal(username, aliYunDriveProperties.getAuth().getPassword(), Collections.singletonList("**")); |
||||
} |
||||
}; |
||||
|
||||
MessageDigestCredentialHandler credentialHandler = new MessageDigestCredentialHandler(); |
||||
realm.setCredentialHandler(credentialHandler); |
||||
context.setRealm(realm); |
||||
|
||||
DigestAuthenticator digestAuthenticator = new DigestAuthenticator(); |
||||
SecurityConstraint securityConstraint = new SecurityConstraint(); |
||||
securityConstraint.setAuthConstraint(true); |
||||
securityConstraint.addAuthRole("**"); |
||||
SecurityCollection collection = new SecurityCollection(); |
||||
collection.addPattern("/*"); |
||||
securityConstraint.addCollection(collection); |
||||
context.addConstraint(securityConstraint); |
||||
context.getPipeline().addValve(digestAuthenticator); |
||||
}); |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public int getOrder() { |
||||
return Ordered.LOWEST_PRECEDENCE; |
||||
} |
||||
} |
@ -1,3 +1,7 @@
@@ -1,3 +1,7 @@
|
||||
#logging.level.net.sf.webdav=trace |
||||
logging.file.path=/var/log/ |
||||
logging.file.name=webdav.log |
||||
logging.file.name=webdav.log |
||||
|
||||
aliyundrive.auth.enable=true |
||||
aliyundrive.auth.user-name=admin |
||||
aliyundrive.auth.password=admin |
||||
|
Loading…
Reference in new issue