{"id":122,"date":"2019-04-24T14:14:51","date_gmt":"2019-04-24T14:14:51","guid":{"rendered":"https:\/\/www.samarthya.me\/wps\/?p=122"},"modified":"2019-04-24T14:14:51","modified_gmt":"2019-04-24T14:14:51","slug":"spring-data-ldap-part-2","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2019\/04\/24\/spring-data-ldap-part-2\/","title":{"rendered":"Spring Data-Ldap: Part 2"},"content":{"rendered":"<p>Now since we have plugged in the LDAP information it is time now to stitch it with Spring Security and the easiest thing to do is<\/p>\n<blockquote>\n<pre>@Configuration\r\n@EnableWebSecurity(debug = true)\r\npublic class WebSecurityConfig extends WebSecurityConfigurerAdapter {\r\n\r\n    private static final Logger logger = LoggerFactory.getLogger(WebSecurityConfig.class);\r\n\r\n\r\n    @Autowired\r\n    private LdapContextSource ldapContextSource;\r\n\r\n    @Override\r\n    protected void configure(HttpSecurity http) throws Exception {\r\n        http.httpBasic().and().authorizeRequests().antMatchers(\"\/users\",\"\/\").permitAll()\r\n                .anyRequest().authenticated().and().csrf().disable();\r\n    }\r\n\r\n    @Override\r\n    protected void configure(AuthenticationManagerBuilder auth) throws Exception {\r\n        auth.ldapAuthentication().contextSource(ldapContextSource)\r\n                .userSearchBase(\"ou=users\")\r\n                .groupSearchBase(\"ou=groups\")\r\n                .groupSearchFilter(\"member={0}\")\r\n                .userDnPatterns(\"ou=users,dc=example,dc=com\")\r\n                .userSearchFilter(\"uid={0}\");\r\n    }\r\n}\r\n\r\n<\/pre>\n<\/blockquote>\n<p>I have already added the ldapContextInformation in the ApacheDSConfiguration class.<\/p>\n<pre>@Bean\r\nLdapTemplate ldapTemplate(ContextSource contextSource) {\r\n    return new LdapTemplate(contextSource);\r\n}<\/pre>\n<p>After weaving this together I exposed a new method in controller<\/p>\n<pre>@ResponseBody\r\n@PostMapping(\"\/protected\")\r\npublic String protectedMethod(HttpServletRequest request) {\r\n    logger.debug(\" Requested: \" + request.getRequestURI() + \" : \" + request.getUserPrincipal().getName() + \" : \" + context.getApplicationName());\r\n    return \"Method Protected\";\r\n}<\/pre>\n<p>and am ready to fire my application with a post method that has been protected by the backed Ldap (Apache DS).<\/p>\n<blockquote><p>POST \/events\/protected HTTP\/1.1<br \/>\n&gt; Host: localhost:8082<br \/>\n&gt; Authorization: Basic c3NoYXJtYTpmaXJld2FsbA==<br \/>\n&gt; User-Agent: insomnia\/6.3.2<br \/>\n&gt; Cookie: JSESSIONID=A049DCE901E82092A38867FA67A773A9<br \/>\n&gt; Accept: *\/*<br \/>\n&gt; Content-Length: 0<br \/>\n&lt; HTTP\/1.1 200<br \/>\n* Replaced cookie JSESSIONID=&#8221;635AE0B0C5C3F7C6BF28FA7776D2FBA0&#8243; for domain localhost, path \/events, expire 0<br \/>\n&lt; Set-Cookie: JSESSIONID=635AE0B0C5C3F7C6BF28FA7776D2FBA0; Path=\/events; HttpOnly<br \/>\n&lt; X-Content-Type-Options: nosniff<br \/>\n&lt; X-XSS-Protection: 1; mode=block<br \/>\n&lt; Cache-Control: no-cache, no-store, max-age=0, must-revalidate<br \/>\n&lt; Pragma: no-cache<br \/>\n&lt; Expires: 0<br \/>\n&lt; X-Frame-Options: DENY<br \/>\n&lt; Content-Type: text\/plain;charset=UTF-8<br \/>\n&lt; Content-Length: 16<br \/>\n&lt; Date: Wed, 24 Apr 2019 14:12:30 GMT<\/p><\/blockquote>\n<p><img decoding=\"async\" class=\"aligncenter size-medium wp-image-123\" src=\"https:\/\/www.samarthya.me\/wps\/wp-content\/uploads\/2019\/04\/Screenshot-2019-04-24-at-7.42.36-PM-300x68.png\" alt=\"\" width=\"300\" height=\"68\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/04\/Screenshot-2019-04-24-at-7.42.36-PM-300x68.png 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/04\/Screenshot-2019-04-24-at-7.42.36-PM-768x175.png 768w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/04\/Screenshot-2019-04-24-at-7.42.36-PM-1024x233.png 1024w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/04\/Screenshot-2019-04-24-at-7.42.36-PM.png 1978w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now since we have plugged in the LDAP information it is time now to stitch it with Spring Security and the easiest thing to do is @Configuration @EnableWebSecurity(debug = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter { private static final Logger logger = LoggerFactory.getLogger(WebSecurityConfig.class); @Autowired private LdapContextSource ldapContextSource; @Override protected void configure(HttpSecurity http) throws Exception { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[17,15],"class_list":["post-122","post","type-post","status-publish","format-standard","hentry","category-others","tag-ldap","tag-spring"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}