extreact ldap cofig

This commit is contained in:
Peter Hormanns 2019-07-24 13:57:39 +02:00
parent 565913903a
commit 0d15cbf294
6 changed files with 122 additions and 67 deletions

View File

@ -0,0 +1,108 @@
package de.jalin.ldapadmin.ldap;
import de.jalin.ldapadmin.server.LDAPUriParser;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LDAPConfig {
private static LDAPConfig config = null;
private String ldapProviderUrl;
private String ldapSecurityPrincipal;
private String ldapSecurityPassword;
private String ldapDistinguishedName;
private String ldapHost;
private String ldapPort;
private boolean ldapUseTLS;
private String smtpFromAddress;
private String smtpHost;
private String smtpPort;
private LDAPConfig() {
ldapProviderUrl = "ldap://localhost:10389/dc=domain,dc=example,dc=com";
ldapDistinguishedName = "dc=domain,dc=example,dc=com";
ldapHost = "localhost";
ldapPort = "10389";
ldapUseTLS = false;
ldapSecurityPrincipal = "uid=admin,ou=system";
ldapSecurityPassword = "secret";
smtpHost = "localhost";
smtpPort = "25";
smtpFromAddress = "nobody@example.com";
try {
final Properties props = loadConfig();
ldapProviderUrl = props.getProperty("provider.url", ldapProviderUrl);
ldapSecurityPrincipal = props.getProperty("security.principal", ldapSecurityPrincipal);
ldapSecurityPassword = props.getProperty("security.password", ldapSecurityPassword);
smtpHost = props.getProperty("smtp.host", smtpHost);
smtpPort = props.getProperty("smtp.port", smtpPort);
smtpFromAddress = props.getProperty("smtp.from", smtpFromAddress);
final LDAPUriParser uriParser = new LDAPUriParser(ldapProviderUrl);
ldapDistinguishedName = uriParser.getDn();
ldapHost = uriParser.getHost();
ldapPort = uriParser.getPort();
ldapUseTLS = uriParser.isUseTLS();
} catch (IOException ex) {
Logger.getLogger(LDAPConfig.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static LDAPConfig getConfig() {
if (config == null) {
config = new LDAPConfig();
}
return config;
}
public String getLdapProviderUrl() {
return ldapProviderUrl;
}
public String getLdapSecurityPrincipal() {
return ldapSecurityPrincipal;
}
public String getLdapSecurityPassword() {
return ldapSecurityPassword;
}
public String getLdapDistinguishedName() {
return ldapDistinguishedName;
}
public String getLdapHost() {
return ldapHost;
}
public String getLdapPort() {
return ldapPort;
}
public boolean isLdapUseTLS() {
return ldapUseTLS;
}
public String getSmtpHost() {
return smtpHost;
}
public String getSmtpPort() {
return smtpPort;
}
public String getSmtpFromAddress() {
return smtpFromAddress;
}
private Properties loadConfig() throws IOException {
final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config.properties");
Properties config = new Properties();
config.load(inputStream);
return config;
}
}

View File

@ -13,8 +13,10 @@ import javax.servlet.http.HttpSession;
import de.jalin.ldapadmin.beans.Group;
import de.jalin.ldapadmin.beans.User;
import de.jalin.ldapadmin.ldap.LDAPConfig;
import de.jalin.ldapadmin.ldap.LDAPSession;
import de.jalin.ldapadmin.ldap.LDAPSessionException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class AbstractLDAPServlet extends HttpServlet {
@ -27,7 +29,7 @@ public class AbstractLDAPServlet extends HttpServlet {
private SortedMap<String, Group> groups;
protected LDAPSession ldapSession;
protected Properties config;
protected LDAPConfig config;
protected void loadData() {
users = new TreeMap<>();
@ -45,14 +47,11 @@ public class AbstractLDAPServlet extends HttpServlet {
@Override
public void init() throws ServletException {
super.init();
final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config.properties");
config = new Properties();
try {
config.load(inputStream);
ldapSession = new LDAPSession(config.getProperty("provider.url"), config.getProperty("security.principal"), config.getProperty("security.password"));
} catch (IOException | LDAPSessionException e) {
LOG.severe(e.getMessage());
throw new ServletException(e);
config = LDAPConfig.getConfig();
ldapSession = new LDAPSession(config.getLdapProviderUrl(), config.getLdapSecurityPrincipal(), config.getLdapSecurityPassword());
} catch (LDAPSessionException ex) {
Logger.getLogger(AbstractLDAPServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}

View File

@ -1,50 +0,0 @@
package de.jalin.ldapadmin.web;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class NaiveTrustManager implements X509TrustManager {
private static final SSLContext SSL_CONTEXT;
static {
try {
SSL_CONTEXT = SSLContext.getInstance("TLSv1.2");
SSL_CONTEXT.init(null, new TrustManager[]{new NaiveTrustManager()}, null);
SSLContext.setDefault(SSL_CONTEXT);
} catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new RuntimeException("Unable to initialise SSL context", e);
}
}
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return SSL_CONTEXT.getSocketFactory().createSocket(host, port);
}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}

View File

@ -25,6 +25,7 @@ import de.jalin.ldapadmin.beans.User;
import de.jalin.ldapadmin.ldap.LDAPSessionException;
import de.jalin.ldapadmin.ldap.SimplePasswordException;
import de.jalin.ldapadmin.ldap.UsersDAO;
import org.apache.commons.lang.CharEncoding;
@WebServlet(name = "ResetPassword", urlPatterns = {"/passwordreset"}, loadOnStartup = 1)
public class ResetPasswordServlet extends AbstractLDAPServlet {
@ -38,9 +39,9 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
@Override
public void init() throws ServletException {
super.init();
smtpHost = config.getProperty("smtp.host", "localhost");
smtpPort = config.getProperty("smtp.port", "25");
smtpFrom = config.getProperty("smtp.from", "nobody@localhost");
smtpHost = config.getSmtpHost();
smtpPort = config.getSmtpPort();
smtpFrom = config.getSmtpFromAddress();
}
@Override
@ -164,7 +165,7 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
}
private static void smtpSend(final String smtpHost, final String smtpPort, final Messages messages, final String fromAddress, final String toAddress, final String subject, final String text) throws IOException {
final SMTPClient client = new SMTPClient();
final SMTPClient client = new SMTPClient(CharEncoding.ISO_8859_1);
final String canonicalHostName = InetAddress.getLocalHost().getHostName();
client.connect(smtpHost, Integer.parseInt(smtpPort));
int reply = client.getReplyCode();
@ -180,7 +181,7 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
}
try (PrintWriter wr = new PrintWriter(sendMessageData)) {
final SimpleSMTPHeader header = new SimpleSMTPHeader(fromAddress, toAddress, subject);
header.addHeaderField("Content-Type", "text/plain; charset=ISO-8859-15");
header.addHeaderField("Content-Type", "text/plain; charset=ISO-8859-1");
header.addHeaderField("Content-Transfer-Encoding", "8bit");
wr.write(header.toString());
wr.write(text);

View File

@ -2,3 +2,4 @@ provider.url=ldap://localhost:10389/dc=domain,dc=example,dc=com
security.principal=uid=admin,ou=system
security.password=streng-geheim
smtp.host=localhost
smtp.from=nobody@example.com

View File

@ -3,10 +3,6 @@
<session-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
<context-param>
<param-name>uri</param-name>
<param-value>ldap://localhost:10389/dc=domain,dc=example,dc=com</param-value>
</context-param>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/css/*</url-pattern>