2014-05-15 2 views
1

я создал простую страницу входа в систему с помощью JSF (NetBeans) теперь я хочу, чтобы сделать этот простой вход в качестве активного аутентификации каталогов, как сделать это ..JSF аутентификации Active Directory

**** мой код * XHTML ***

<h:head> 
    <title>LogIn</title> 
    <h:outputStylesheet library="css" name="style.css"/> 
</h:head> 
<h:body> 
    <h:form> 
    <fieldset> 
    <legend>LogIn</legend> 
    <p:messages autoUpdate="true" severity="info" closable="false" /> 
    <div styleclass="label"> 
     <h:outputLabel id="usernameOutputId" value="Username : " /> 
    </div> 
    <div styleclass="textbox"> 
     <h:inputText id="usernameInputId" value="#{userLogin.userName}" 
     required="true" requiredMessage="Enter username" size="20" /> 
     <br/> 
     <span><h:message for="usernameInputId" errorClass="errorMessage" /></span> 
    </div> 
    <br/> 
    <div styleclass="label"> 
     <h:outputLabel id="passwordOutputId" value="Password : " /> 
    </div> 
    <div styleclass="textbox"> 
     <h:inputSecret id="passwordInputId" value="#{userLogin.password}" 
     required="true" requiredMessage="Enter password" size="20" /> 
     <br/> 
     <span><h:message for="passwordInputId" errorClass="errorMessage" /></span> 
    </div> 
    <br/> 
    <h:commandButton id="userLoginCmdBtnId" value="LOGIN" 
        action="#{userLogin.Process}" /> 
    </fieldset> 

</h:form> 
</h:body> 
</html> 

** мой Java-код *****

package login; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 
import javax.faces.application.FacesMessage; 


import javax.faces.bean.ManagedBean; 


import javax.faces.bean.SessionScoped; 
import javax.faces.context.FacesContext; 

@ManagedBean(name = "userLogin") 
@SessionScoped 
public class LogIn { 

public String userName; 
public String password; 
public FacesMessage message; 

public String getUserName() { 
    return userName; 
} 

public void setUserName(String userName) { 
    this.userName = userName; 
} 

public String getPassword() { 
    return password; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 

public FacesMessage getMessage() { 
    return message; 
} 

public void setMessage(FacesMessage message) { 
    this.message = message; 
} 


public String Process() throws Exception { 

    Connection c = null; 
     Statement st = null; 
       Class.forName("org.postgresql.Driver"); 
     c = DriverManager 
      .getConnection("jdbc:postgresql://localhost:5432/test", 
      "postgres", "admin"); 
     c.setAutoCommit(false); 
     System.out.println("Opened database successfully"); 

     st = c.createStatement(); 
     ResultSet res = st.executeQuery("SELECT * FROM users;"); 


     String sql="select * from users"; 
     res=st.executeQuery(sql); 

     while(res.next()) 
     { 
     System.out.println("hiii"); 
     System.out.println(res.getString(1)); 
     System.out.println("Uid="+userName); 
      if (userName.equalsIgnoreCase(res.getString(1)) 
         && password.equalsIgnoreCase(res.getString(2))) 
         { 
          System.out.println("Login Successful"); 
          return "success"; 
      } 
         else{ 
           message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Authentication Failed Earror.", ""); 
           FacesContext.getCurrentInstance().addMessage(null, message); 
           return "failure"; 

         } 

       } 

      return""; 
} 

ответ

0

Попробуйте что-то вроде т его для входа в систему с помощью LDAP protocal к Active Directory

Hashtable<String, String> props = new Hashtable<String, String>(); 
      String principalName = userName + "@" + "MYDOMAIN"; 
      props.put(Context.SECURITY_PRINCIPAL, principalName); 
      props.put(Context.SECURITY_CREDENTIALS, password); 
      DirContext context = null; 
      context = LdapCtxFactory.getLdapCtxInstance("ldap://ad.mydomain:389", props); 
      System.out.println("User login successful: "); 

Подставьте правильный домен и LDAP URL (и порт) в коде.

Если вы достигнете линии System.out.println, тогда логин будет успешным, иначе вы получите javax.naming.AuthenticationException exception.

Смежные вопросы