简体   繁体   中英

Windows Authentication in Angular

How to implement Windows Authentication with Angular 2 and above with backend as JAVA. I searched all the places but only seeing the Web API as solution which is specific to .NET

Following Code authenticates from LDAP using pure Java JNDI. The Principle is:-

First Lookup the user using a admin or DN user.

The user object needs to be passed to LDAP again with the user credential.

No Exception means - Authenticated Successfully. Else Authentication Failed.

public static boolean authenticateJndi(String username, String password) throws Exception{
    Properties props = new Properties();
    props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    props.put(Context.PROVIDER_URL, "ldap://LDAPSERVER:PORT");
    props.put(Context.SECURITY_PRINCIPAL, "uid=adminuser,ou=special users,o=xx.com");//adminuser - User with special priviledge, dn user
    props.put(Context.SECURITY_CREDENTIALS, "adminpassword");//dn user password


    InitialDirContext context = new InitialDirContext(props);

    SearchControls ctrls = new SearchControls();
    ctrls.setReturningAttributes(new String[] { "givenName", "sn","memberOf" });
    ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    NamingEnumeration<javax.naming.directory.SearchResult> answers = context.search("o=xx.com", "(uid=" + username + ")", ctrls);
    javax.naming.directory.SearchResult result = answers.nextElement();

    String user = result.getNameInNamespace();

    try {
        props = new Properties();
        props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        props.put(Context.PROVIDER_URL, "ldap://LDAPSERVER:PORT");
        props.put(Context.SECURITY_PRINCIPAL, user);
        props.put(Context.SECURITY_CREDENTIALS, password);

   context = new InitialDirContext(props);
    } catch (Exception e) {
        return false;
    }
    return true;
}

More on

LDAP Authentication using Java .

For angular and spring boot ,

Have a login controller , pass username and password to that controller and then validate the user.Use httpsession for subsequent requests.

@RestController
public class HomeController {

    @PostMapping("/")
    public String index(@RequestBody User user,HttpSession httpSession) {

    if(authenticateJndi(user.getUsername(),user.getPassword()))
   {

   // Login success 

   httpSession.setAttribute("userName",user.getUsername()),;

   }

  else 
    {

   // Login failed 

    }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM