2015-09-24 61 views
1

我開始使用J2EE應用程序,並且基於表單的登錄有一個惱人的錯誤。我找不到任何解決方案,我也不知道我還能做什麼。我決定在這裏問,看看你是否可以解決它:)。我正在用eclipse編程,我有一個MySQL數據庫,而且我正在使用eclipse提供的tomcat服務器。基於J2EE表單登錄錯誤

當我運行該應用程序時,服務器工作正常。如果我嘗試進入任何安全區域(/ *),該應用程序會讓我回到登錄頁面。如果我編寫的組合錯誤,程序會檢測到錯誤,並向我發送消息。當我試着使用正確的用戶名和密碼登錄,我收到此錯誤信息:

sep 24, 2015 1:13:27 PM org.apache.catalina.realm.JDBCRealm getRoles 
GRAVE: Excepción al realizar la autentificación 
**com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'username' in 'where clause'** 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:389) 
    at com.mysql.jdbc.Util.getInstance(Util.java:372) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:980) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2535) 
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1911) 
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2034) 
    at org.apache.catalina.realm.JDBCRealm.getRoles(JDBCRealm.java:632) 
    at org.apache.catalina.realm.JDBCRealm.authenticate(JDBCRealm.java:422) 
    at org.apache.catalina.realm.JDBCRealm.authenticate(JDBCRealm.java:353) 
    at org.apache.catalina.authenticator.FormAuthenticator.authenticate(FormAuthenticator.java:294) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:451) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Unknown Source) 

我創建的數據庫下表:

- users , with 2 fields: username and password. 
- user_roles, with 1 field: rolename. 
- users_user_roles, with 2 fields: username and rolename. 

我加入以下代碼web.xml文件:

<login-config> 
     <auth-method>FORM</auth-method> 
     <realm-name>Example Form-Based Authentication Area</realm-name> 
     <form-login-config> 
      <form-login-page>/login.jsp</form-login-page> 
      <form-error-page>/fail_login.jsp</form-error-page> 
     </form-login-config> 
    </login-config> 
    <security-constraint> 
     <web-resource-collection> 
      <web-resource-name>Protected Area</web-resource-name> 
      <description> accessible by authorised users </description> 
      <url-pattern>/*</url-pattern> 
      <http-method>DELETE</http-method> 
      <http-method>GET</http-method> 
      <http-method>POST</http-method> 
      <http-method>PUT</http-method> 
     </web-resource-collection> 
     <auth-constraint> 
      <description>These are the roles who have access</description> 
      <role-name>usuario</role-name> 
     </auth-constraint> 
     <user-data-constraint> 
      <transport-guarantee>NONE</transport-guarantee> 
     </user-data-constraint> 
    </security-constraint> 

,我已經改變了境界代碼在server.xml這樣的:

Realm className="org.apache.catalina.realm.JDBCRealm" 
     debug="99" 
     driverName="org.gjt.mm.mysql.Driver" 
     connectionURL="jdbc:mysql://localhost/apou_ddbb?user=apou&amp;password=apouddbbpassword" 
     userTable="users" userNameCol="username" userCredCol="password" 
     userRoleTable="user_roles" roleNameCol="rolename" 

我按照這個指南:http://archive.oreilly.com/pub/a/onjava/2002/06/12/form.html

回答

2

有幾件事情是錯在這裏。

首先,JDBCRealm可能不應該被使用:它是單線程的,如果你使用你的web應用程序超越你和你的直系親屬,性能將會很糟糕。改爲使用DataSourceRealm。這意味着您必須使用所有JDBC連接信息來定義<Resource>,然後將DataSourceRealm鏈接到<Resource>。它有更多的配置,但它更強大。

其次,您對userRoleTable有錯誤的表格:您想要users_user_roles而不是user_roles。您的user_roles表僅包含可能角色的列表 - 而不是用戶到角色的實際映射。我可能會建議重新命名爲user_roles以簡化roles並重新命名爲users_user_rolesuser_roles以避免混淆:替換表名roles表明它與用戶無關,這是(可以說是)事實。

這是包括在Tomcat用戶指南,在幾個文件分佈在:

+0

非常感謝您的寶貴時間,現在它的工作原理正確:) – mikizas

+0

我遵循了你的建議,並且改變了表名。我以爲他們要求角色表,而不是連接表。 – mikizas

+0

並不是很多人會同時使用這個應用程序,但我會盡力去改變它。你知道我在哪裏可以找到一個教程,或者什麼來幫助我?非常感謝你,你已經解決了我的一天:) – mikizas