Java tutorial
/* Copyright 2018 Ericsson AB. For a full list of individual contributors, please see the commit history. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.ericsson.eiffel.remrem.generate.config; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.http.SessionCreationPolicy; /** * This class is used to enable the ldap authentication based on property * activedirectory.generate.enabled=true in property file. * */ @Profile("!integration-test") @Configuration @ConditionalOnProperty(value = "activedirectory.generate.enabled") @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(SecurityConfig.class); @Value("${activedirectory.ldapUrl}") private String ldapUrl; @Value("${activedirectory.managerPassword}") private String managerPassword; @Value("${activedirectory.managerDn}") private String managerDn; @Value("${activedirectory.userSearchFilter}") private String userSearchFilter; @Value("${activedirectory.rootDn}") private String rootDn; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { LOGGER.debug("LDAP server url: " + ldapUrl); auth.ldapAuthentication().userSearchFilter(userSearchFilter).contextSource().managerDn(managerDn) .root(rootDn).managerPassword(decode(managerPassword)).url(ldapUrl); } @Override protected void configure(HttpSecurity http) throws Exception { LOGGER.debug("LDAP authentication enabled"); http.authorizeRequests().anyRequest().authenticated().and().sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.ALWAYS).and().httpBasic().and().csrf().disable(); } // To decrypt Base64 encode ldap manager password private String decode(String password) { return StringUtils.newStringUtf8(Base64.decodeBase64(password)); } }