java - JSF Activity 目录身份验证

标签 java jsf netbeans ldap

我已经使用 jsf(netbeans) 创建了简单的登录页面,现在我想将这个简单的登录作为 Activity 目录身份验证如何做到这一点..

****我的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"";
 }

最佳答案

尝试类似的操作,使用 LDAP 协议(protocol)登录 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 异常。

关于java - JSF Activity 目录身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23673963/

相关文章:

java - 暂停 ScheduledExecutorService

java - Hibernate注释异常: Use of @OneToMany or @ManyToMany targeting an unmapped class

java - 在 EJB JSF 的一个 session 中存储同步版本的实体

java - 如果另一个菜单中的值发生更改,如何更改 SelectOneMenu 中的列表?

java - 如何在 Java EE 上使用 Hibernate、C3P0 处理数据库连接失败?

java - HashMap的HashMap->输出问题

java - GWT 生成器获取编译时间

java - 如何 "save"我的程序?

java - 当我使用运行文件进行测试时,NetBeans GUI 设计表现不一致

java - 我将 JSP 与 NetBeans 一起使用,但出现以下错误