001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.ldap;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.springframework.boot.context.properties.ConfigurationProperties;
023import org.springframework.core.env.Environment;
024import org.springframework.util.Assert;
025import org.springframework.util.ObjectUtils;
026
027/**
028 * Configuration properties for LDAP.
029 *
030 * @author EddĂș MelĂ©ndez
031 * @since 1.5.0
032 */
033@ConfigurationProperties(prefix = "spring.ldap")
034public class LdapProperties {
035
036        private static final int DEFAULT_PORT = 389;
037
038        /**
039         * LDAP URLs of the server.
040         */
041        private String[] urls;
042
043        /**
044         * Base suffix from which all operations should originate.
045         */
046        private String base;
047
048        /**
049         * Login username of the server.
050         */
051        private String username;
052
053        /**
054         * Login password of the server.
055         */
056        private String password;
057
058        /**
059         * Whether read-only operations should use an anonymous environment.
060         */
061        private boolean anonymousReadOnly;
062
063        /**
064         * LDAP specification settings.
065         */
066        private final Map<String, String> baseEnvironment = new HashMap<>();
067
068        public String[] getUrls() {
069                return this.urls;
070        }
071
072        public void setUrls(String[] urls) {
073                this.urls = urls;
074        }
075
076        public String getBase() {
077                return this.base;
078        }
079
080        public void setBase(String base) {
081                this.base = base;
082        }
083
084        public String getUsername() {
085                return this.username;
086        }
087
088        public void setUsername(String username) {
089                this.username = username;
090        }
091
092        public String getPassword() {
093                return this.password;
094        }
095
096        public void setPassword(String password) {
097                this.password = password;
098        }
099
100        public boolean getAnonymousReadOnly() {
101                return this.anonymousReadOnly;
102        }
103
104        public void setAnonymousReadOnly(boolean anonymousReadOnly) {
105                this.anonymousReadOnly = anonymousReadOnly;
106        }
107
108        public Map<String, String> getBaseEnvironment() {
109                return this.baseEnvironment;
110        }
111
112        public String[] determineUrls(Environment environment) {
113                if (ObjectUtils.isEmpty(this.urls)) {
114                        return new String[] { "ldap://localhost:" + determinePort(environment) };
115                }
116                return this.urls;
117        }
118
119        private int determinePort(Environment environment) {
120                Assert.notNull(environment, "Environment must not be null");
121                String localPort = environment.getProperty("local.ldap.port");
122                if (localPort != null) {
123                        return Integer.valueOf(localPort);
124                }
125                return DEFAULT_PORT;
126        }
127
128}