【问题标题】:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'USER_NAME' in 'where clause' - container managed authenticationcom.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'where 子句'中的未知列'USER_NAME' - 容器管理的身份验证
【发布时间】:2014-05-31 10:33:30
【问题描述】:

在我提出问题 - Authorization and authentication on the web application with JSF, Hibernate and Tomcat 之后,我开发了一些代码,我相信我非常接近解决方案。

首先,我正在使用 Eclipse 和 Apache Tomcat 使用 Java EE、JSF、Hibernate、MySQL 编写一个 Web 应用程序。我不使用 Spring、EJB 等。我实现了“容器管理身份验证”。我分享了该项目的部分代码。 他们有标准的简单代码来开发 SERVLET 登录。当您看着它们时,您会很容易理解。

POJO 类;

public class User {

    // Primary Key
    private int USER_ID;
    
    private String USER_NAME;
    private String PASSWORD;
    private String FIRST_NAME;
    private String LAST_NAME;
    ...

    public User(String uSER_NAME, String pASSWORD, String fIRST_NAME,
            String lAST_NAME, ...) {
        super();
        USER_NAME = uSER_NAME;
        PASSWORD = pASSWORD;
        FIRST_NAME = fIRST_NAME;
        ...
    }

    /**
     * Default empty constructor needed for hibernate entity
     */
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * Getters and setters
     */
        ...
        ...
}

public class UserGroup {

// Primary Key
private int USER_GROUP_ID;

// Reference for User table - foreign key
private Set<User> USERS;

private String USER_GROUP_NAME;

private boolean ADMINISTRATOR_SCR;
private boolean USER_SCR;
private boolean PLANNING_SCR;
...

public UserGroup(Set<User> uSERS, String uSER_GROUP_NAME,
        boolean aDMINISTRATOR_SCR, boolean uSER_SCR, boolean pLANNING_SCR,
        ...) {
    super();
    USERS = uSERS;
    USER_GROUP_NAME = uSER_GROUP_NAME;
    ADMINISTRATOR_SCR = aDMINISTRATOR_SCR;
    ...
}

/**
 * Default empty constructor needed for hibernate entity
 */
public UserGroup() {
    // TODO Auto-generated constructor stub
}
/**
 * Getters and setters
 */
    ...
    ...
}

休眠映射文件;

<hibernate-mapping>
    <class name="folder.User" table="USER">
    <id name="USER_ID" type="int">
        <column name="USER_ID" />
        <generator class="native" />
    </id>
    <property name="USER_NAME" type="java.lang.String">
        <column name="USER_NAME" />
    </property>
        <property name="PASSWORD" type="java.lang.String">
            <column name="PASSWORD" />
        </property>
        <property name="FIRST_NAME" type="java.lang.String">
            <column name="FIRST_NAME" />
        </property>
        <property name="LAST_NAME" type="java.lang.String">
            <column name="LAST_NAME" />
       ...
    </class>
</hibernate-mapping>

<hibernate-mapping>
    <class name="folder.UserGroup" table="USERGROUP">
        <id name="USER_GROUP_ID" type="int">
            <column name="USER_GROUP_ID" />
            <generator class="native" />
        </id>
        <set name="USERS" table="USER" inverse="false" lazy="true" cascade="all">
            <key>
                <column name="USER_GROUP_ID" />
            </key>
            <one-to-many class="folder.User" />
        </set>
        <property name="USER_GROUP_NAME" type="java.lang.String">
            <column name="USER_GROUP_NAME" />
        </property>
        <property name="ADMINISTRATOR_SCR" type="boolean">
            <column name="ADMINISTRATOR_SCR" />
        </property>
        <property name="USER_SCR" type="boolean">
            <column name="USER_SCR" />
        </property>
        ...
    </class>
</hibernate-mapping>

web.xml 配置;

    <security-constraint>
        <display-name>Restricted</display-name>
        <web-resource-collection>
            <web-resource-name>Restricted Area</web-resource-name>
            <url-pattern>/authorized/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>user</role-name>
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/login.xhtml</form-login-page>
            <form-error-page>/login.xhtml</form-error-page>
        </form-login-config>
    </login-config>
    <security-role>
        <role-name>user</role-name>
    </security-role>

server.xml tomcat 配置;

 <Realm className="org.apache.catalina.realm.JDBCRealm"
       driverName="com.mysql.jdbc.Driver"
       connectionURL="jdbc:mysql://localhost:3306/authentication_db"
       connectionName="..." connectionPassword="..."
       userTable="user" userNameCol="USER_NAME" userCredCol="PASSWORD" 
       userRoleTable="usergroup" roleNameCol="USER_GROUP_NAME" />

最后是登录功能;

public String login(){
        
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        try {
            //Login via the Servlet Context
            request.login(getLoginName(), getLoginPass());
       

            return "success";
        } catch (ServletException e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid Login", null));
            e.printStackTrace();
        }
        return "failure";
}

在我将记录放在用户表和用户组上并运行应用程序后,它看起来工作正常。但是,我收到此错误;

2014 年 16 月 16 日下午 4:38:48 org.apache.catalina.realm.JDBCRealm getRoles

严重:执行身份验证时出现异常

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 未知 “where 子句”中的“USER_NAME”列

在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

在 sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown 来源)

在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown 资源) ... ...

请帮帮我!

编辑:

MySQL 表定义:

表:用户

列:

USER_ID int(11) AI PK

USER_NAME varchar(255)

密码 varchar(255)

FIRST_NAME varchar(255)

LAST_NAME varchar(255)

...

EMAIL varchar(255)

USER_GROUP_ID int(11) FK(用户组表 -> USER_GROUP_ID)


表:用户组

列:

USER_GROUP_ID int(11) AI PK

USER_GROUP_NAME varchar(255)

ADMINISTRATOR_SCR 位 (1)

USER_SCR 位(1)

PLANNING_SCR 位(1) ...

【问题讨论】:

  • 你能把MySql的表定义也贴出来吗?
  • 我添加了MySQL表信息。它们都是利用hibernate的持久化特性生成的。

标签: hibernate jsf tomcat authentication servlet-filters


【解决方案1】:

我解决了这个问题。问题在于 JDBCRealm 数据格式。 JDBCRealm 是 Tomcat 6 Realm 接口的实现,它在通过 JDBC 驱动程序访问的关系数据库中查找用户。我必须以特殊格式创建用户和用户组表才能使用 JDBCRealm。要查看此配置,请访问 Apache Tomcat 网站的页面 - https://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html

【讨论】:

    猜你喜欢
    • 2015-11-11
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多