Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA and dynamic user authentication with the database

 
adam smith ii
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to use JPA and dynamic based user authentication with the database? I would like to capture the users credentials in a form and authenticate them with the database and have JPA use that connection. I am not interested in Realms, because it sound likes I would need to set up additional tables. If the user is an authorized database user, that is all that is required for the application. I don't want to expose a user-id and password in a "configuration" xml file.

Also, I have three database environments (with the same schema), and would like to be able to specify which database server to use at run time.

Concepts required to solve this problem, steps and an example would be appreciated.

Application is JSF 2.0 currently using container managed persistence via EclipseLink.
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can pass connection/JPA properties such as user/password when acquiring an EntityManagerFactory or an EntityManager.

See,
http://en.wikibooks.org/wiki/Java_Persistence/Auditing_and_Security

and,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Auditing

For passing the server, you could pass a different database URL in the properties when acquiring an EntityManagerFactory.

Not that using a different database login per user can affect your connection pool usage, so you may not be able to pool connections, unless you use something like Oracle proxy authentication.
 
adam smith ii
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James,
I tried passing the data source in the properties map to the constructor for the EntityManager / EntityManagerFactory, and believe I achieved partial success for one entity. I believe I stumbled because the JPA split my single database table into two entities, presumably because of a two column key and I did not know how to address that issue. Data retrieval worked, but data update failed.

I will study the references you provided, many thanks.

In the mean time I cloned the project and replaced JPA with application controlled database connection and SQL statement coding.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic