Angular中的Windows身份验证

时间:2019-05-07 11:20:58

标签: java angular spring-boot

如何使用Angular 2和更高版本(后端为JAVA)实施Windows身份验证。我搜索了所有地方,但只看到了Web API作为.NET特有的解决方案

1 个答案:

答案 0 :(得分:0)

以下代码使用纯Java JNDI从LDAP进行身份验证。原理是:-

  

首先使用管理员或DN用户查找用户。

     

用户对象需要再次与用户一起传递给LDAP   凭据。

     

没有异常意味着-成功认证。其他认证   失败。

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;
}

更多信息

LDAP Authentication using Java

对于角靴和弹簧靴,

  

具有一个登录控制器,将用户名和密码传递给该控制器   控制器,然后验证用户。使用httpsession处理后续请求。

@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 

    }

    }
}