+ private boolean authenticate(String userDn, String credentials) {
+ DirContext ctx = null;
+ try {
+ ctx = contextSource.getContext(userDn, credentials);
+ return true;
+ } catch (Exception e) {
+ // Context creation failed - authentication did not succeed
+ System.out.println("LDAP Login failed");
+ return false;
+ } finally {
+ // It is imperative that the created DirContext instance is always
+ // closed
+ LdapUtils.closeContext(ctx);
+ }
+ }
+
+ @RequestMapping(value = "/ldaplogindo", method = RequestMethod.POST)
+ public String LDAPlogin(Model model, @RequestParam("j_username") String username, @RequestParam("j_password") String credentials) {
+ System.out.println("Try to authenticate with LDAP: username: " + username + ", credentials: " + credentials);
+ if (authenticate(username, credentials)) {
+ return "/home";
+ }
+ return "/public";
+ }
+