<p>The system I am currently working on requires some role-based security, which is well catered for in the Java EE stack. The system intends to be a framework for business domain experts to write their code on top of.</p>

<p>However, there is also a requirement for data <code>security</code>. That is, what information is visible to an end user.</p>

<p>This effectively means reducing visibility to rows (and perhaps even columns) in the database.</p>

<p>We are using Hibernate for our persistence. However, we are using our own annotations so as not to expose our persistence choice to the business domain experts. </p>

<p>For row based security this means we could add an annotation such as <code>@Secured</code> at the entity level, which would cause an extra column to be added to the underlying table to constrain our selects?</p>

<p>For column based security, we could perhaps have <code>@Secured</code> to either assist in query generation, or perhaps use an aspect to filter the information returned?</p>

<p>I'm curious to know how this might affect hibernate's caching mechanisms as well?</p>

<p>I'm sure a lot of others will have had the same issue, and I was wondering how you approached this?</p>

<p>Much appreciated...</p>

[{'answer_id': 34917, 'author': 'JMM', 'author_id': 1299, 'author_profile': 'https://Stackoverflow.com/users/1299', 'pm_score': 4, 'selected': True, 'text': '<p>Hibernate has a <a href="http://in.relation.to/Bloggers/Hibernate3Filters" rel="noreferrer">filter mechanism</a> that may work for you.  The filters will rewrite the queries hibernate generates to include an additional clause to limit the rows returned.  I\'m not aware of anything in hibernate to mask/hide columns.</p>\n\n<p>Your database may also have support for this functionality.  Oracle, for example, has the <a href="http://www.devshed.com/c/a/Oracle/RowLevel-Security-with-Virtual-Private-Database/" rel="noreferrer">Virtual Private Database</a> (VPD) which will rewrite your queries at the database level.  This solution has the added benefit that any external program (e.g. reporting tools) that goes against your db will have your security restrictions enforced.  VPD also has support to mask restricted columns with NULLs.</p>\n\n<p>Unfortunately, the above solutions have not been adequate to support the security requirements for the types projects I typically work on.  There is usually some sort of context that cannot be easily expressed in the above solutions.  For example, users can view data that they have created, or that have been been marked as public, or belong to a project which they manage.</p>\n\n<p>We typically create query/finder/DAO objects where we pass in the values required to enforce the security and then create the query accordingly.</p>\n\n<p>I hope this helps</p>\n'}, {'answer_id': 37755, 'author': 'Brian Matthews', 'author_id': 1969, 'author_profile': 'https://Stackoverflow.com/users/1969', 'pm_score': 1, 'selected': False, 'text': '<p>When using Hibernate filters you need to be aware that the additional restrictions will not be applied to SQL statements generted by the <code>load()</code> or <code>get()</code> methods.</p>\n'}]
['https://Stackoverflow.com/questions/34638', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3295/']