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