From cc15245e4d6ed4a50cd5df4e770fa317c1ade6a9 Mon Sep 17 00:00:00 2001 From: corsoft Date: Fri, 13 Jun 2014 07:24:22 +0100 Subject: [PATCH] LDAP working - MVC needs fixing --- spring-security-mvc-ldap/pom.xml | 63 +++++++++++++++++-- .../baeldung/controller/HomeController.java | 15 +++++ .../MyBasicAuthenticationEntryPoint.java | 31 --------- .../java/org/baeldung/spring/Application.java | 17 +++++ .../java/org/baeldung/spring/MvcConfig.java | 39 ------------ .../baeldung/spring/SecSecurityConfig.java | 17 ----- .../org/baeldung/spring/SecurityConfig.java | 29 +++++++++ .../java/org/baeldung/spring/WebConfig.java | 17 ----- .../src/main/resources/users.ldif | 14 +++++ .../src/main/resources/webSecurityConfig.xml | 11 +++- 10 files changed, 142 insertions(+), 111 deletions(-) create mode 100644 spring-security-mvc-ldap/src/main/java/org/baeldung/controller/HomeController.java delete mode 100644 spring-security-mvc-ldap/src/main/java/org/baeldung/security/basic/MyBasicAuthenticationEntryPoint.java create mode 100644 spring-security-mvc-ldap/src/main/java/org/baeldung/spring/Application.java delete mode 100644 spring-security-mvc-ldap/src/main/java/org/baeldung/spring/MvcConfig.java delete mode 100644 spring-security-mvc-ldap/src/main/java/org/baeldung/spring/SecSecurityConfig.java create mode 100644 spring-security-mvc-ldap/src/main/java/org/baeldung/spring/SecurityConfig.java delete mode 100644 spring-security-mvc-ldap/src/main/java/org/baeldung/spring/WebConfig.java create mode 100644 spring-security-mvc-ldap/src/main/resources/users.ldif diff --git a/spring-security-mvc-ldap/pom.xml b/spring-security-mvc-ldap/pom.xml index 11e875a071..57af0f1d48 100644 --- a/spring-security-mvc-ldap/pom.xml +++ b/spring-security-mvc-ldap/pom.xml @@ -7,11 +7,16 @@ spring-security-mvc-ldap war + + + org.springframework.boot + spring-boot-starter-parent + 1.1.1.RELEASE + - org.springframework.security spring-security-web @@ -30,10 +35,10 @@ spring-core ${org.springframework.version} - - commons-logging - commons-logging - + + commons-logging + commons-logging + @@ -78,6 +83,52 @@ ${org.springframework.version} + + + + + org.springframework.boot + spring-boot-starter-web + 1.1.1.RELEASE + + + + org.springframework.security + spring-security-ldap + 3.2.4.RELEASE + + + + org.springframework.ldap + spring-ldap-core + 2.0.2.RELEASE + + + org.springframework.ldap + spring-ldap-core-tiger + 2.0.2.RELEASE + + + + + org.apache.directory.server + apacheds-server-jndi + 1.5.5 + + + + commons-collections + commons-collections + 3.2.1 + + + org.apache.mina + mina-core + 2.0.7 + + + + @@ -126,7 +177,7 @@ log4j-over-slf4j ${org.slf4j.version} - + diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/HomeController.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/HomeController.java new file mode 100644 index 0000000000..7d9a836500 --- /dev/null +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/HomeController.java @@ -0,0 +1,15 @@ +package org.baeldung.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class HomeController { + + @RequestMapping("/homepage.html") + public @ResponseBody String index() { + return "homepage"; + } +} + diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/security/basic/MyBasicAuthenticationEntryPoint.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/security/basic/MyBasicAuthenticationEntryPoint.java deleted file mode 100644 index 968237227f..0000000000 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/security/basic/MyBasicAuthenticationEntryPoint.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.baeldung.security.basic; - -import java.io.IOException; -import java.io.PrintWriter; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.springframework.security.core.AuthenticationException; -import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint; -import org.springframework.stereotype.Component; - -@Component -public class MyBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint { - - @Override - public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException) throws IOException, ServletException { - response.addHeader("WWW-Authenticate", "Basic realm=\"" + getRealmName() + "\""); - response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); - final PrintWriter writer = response.getWriter(); - writer.println("HTTP Status " + HttpServletResponse.SC_UNAUTHORIZED + " - " + authException.getMessage()); - } - - @Override - public void afterPropertiesSet() throws Exception { - setRealmName("Baeldung"); - super.afterPropertiesSet(); - } - -} diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/spring/Application.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/spring/Application.java new file mode 100644 index 0000000000..6d0621e3dd --- /dev/null +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/spring/Application.java @@ -0,0 +1,17 @@ +package org.baeldung.spring; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan +@EnableAutoConfiguration +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} \ No newline at end of file diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/spring/MvcConfig.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/spring/MvcConfig.java deleted file mode 100644 index 74c11478ee..0000000000 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/spring/MvcConfig.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.baeldung.spring; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; -import org.springframework.web.servlet.view.InternalResourceViewResolver; -import org.springframework.web.servlet.view.JstlView; - -@Configuration -@EnableWebMvc -public class MvcConfig extends WebMvcConfigurerAdapter { - - public MvcConfig() { - super(); - } - - // API - - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); - - registry.addViewController("/homepage.html"); - } - - @Bean - public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); - - return bean; - } -} \ No newline at end of file diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/spring/SecSecurityConfig.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/spring/SecSecurityConfig.java deleted file mode 100644 index 9f0591d7db..0000000000 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/spring/SecSecurityConfig.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.baeldung.spring; - -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.ImportResource; - -@Configuration -@ImportResource({ "classpath:webSecurityConfig.xml" }) -@ComponentScan("org.baeldung.security") -public class SecSecurityConfig { - - public SecSecurityConfig() { - super(); - } - - -} diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/spring/SecurityConfig.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/spring/SecurityConfig.java new file mode 100644 index 0000000000..026afa114a --- /dev/null +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/spring/SecurityConfig.java @@ -0,0 +1,29 @@ +package org.baeldung.spring; + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; + +@Configuration +@EnableWebMvcSecurity +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(AuthenticationManagerBuilder auth) + throws Exception { + + auth.ldapAuthentication() + .userSearchBase("ou=people") + .userSearchFilter("(uid={0})") + .groupSearchBase("ou=groups") + .groupSearchFilter("member={0}") + .contextSource() + .root("dc=baeldung,dc=com") + .ldif("classpath:users.ldif"); + + } + + + +} diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/spring/WebConfig.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/spring/WebConfig.java deleted file mode 100644 index fa6f5f6d56..0000000000 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/spring/WebConfig.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.baeldung.spring; - -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; - -@Configuration -@ComponentScan("org.baeldung.web") -public class WebConfig extends WebMvcConfigurerAdapter { - - public WebConfig() { - super(); - } - - // API - -} \ No newline at end of file diff --git a/spring-security-mvc-ldap/src/main/resources/users.ldif b/spring-security-mvc-ldap/src/main/resources/users.ldif new file mode 100644 index 0000000000..59712d59e1 --- /dev/null +++ b/spring-security-mvc-ldap/src/main/resources/users.ldif @@ -0,0 +1,14 @@ +dn: ou=people,dc=baeldung,dc=com +objectclass: top +objectclass: organizationalUnit +ou: people + +dn: uid=baeldung,ou=people,dc=baeldung,dc=com +objectclass: top +objectclass: person +objectclass: organizationalPerson +objectclass: inetOrgPerson +cn: David Lightman +sn: Lightman +uid: baeldung +userPassword: password \ No newline at end of file diff --git a/spring-security-mvc-ldap/src/main/resources/webSecurityConfig.xml b/spring-security-mvc-ldap/src/main/resources/webSecurityConfig.xml index e5310da187..67db0dba6d 100644 --- a/spring-security-mvc-ldap/src/main/resources/webSecurityConfig.xml +++ b/spring-security-mvc-ldap/src/main/resources/webSecurityConfig.xml @@ -13,6 +13,9 @@ + + + + + + + \ No newline at end of file