X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=server%2Fcompbio%2Fcontrollers%2FUserController.java;h=a00bd15ebafc5c05f9ddc3d0e9c4627ec97f088b;hb=c1ae48e93c766434005e5d5201af8ad23bc0a59b;hp=7dbfdf437d2bd18c28d1411744e276914c862225;hpb=341971e3170e4e957656a61ae2ffcc0e50ecf621;p=proteocache.git diff --git a/server/compbio/controllers/UserController.java b/server/compbio/controllers/UserController.java index 7dbfdf4..a00bd15 100644 --- a/server/compbio/controllers/UserController.java +++ b/server/compbio/controllers/UserController.java @@ -3,9 +3,15 @@ package compbio.controllers; import java.util.Date; import java.util.regex.Pattern; +import javax.naming.directory.DirContext; + +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.mail.SimpleMailMessage; -import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.security.ldap.LdapUtils; +import org.springframework.ldap.core.ContextSource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; @@ -23,7 +29,9 @@ import compbio.cassandra.CassandraUserManager; @Controller public class UserController { - // @Inject + @Autowired + ContextSource contextSource; + // JavaMailSender mailSender; private final Pattern EMAIL = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"); @@ -131,4 +139,29 @@ public class UserController { // mailSender.send(message); } + 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"; + } + }