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;
@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}");
// 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";
+ }
+
}