Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think it generally not a good idea insert the user object into the request object every time:</p> <p>The request lifetime is very short, so you might end up making round trips to caches or even worse to the database on each http-request to retrieve an object, that you might not even need and that get's deleted immideately afterwards. So if you must, better store the whole object in the session instead of just the id.</p> <p>Generally, I'd suggest you write a <code>AuthenticationService</code> with a method <code>isLoggedIn()</code> that returns true when the user is authenticated and a method <code>getLoggedInUser()</code> that returns this object.</p> <pre><code>class AuthenticationService { def transactional = false boolean isLoggedIn() { return session.user_id } def getLoggedInUser() { return User.get(session.user_id) } } </code></pre> <p>Then you use the Filter for redirection if not authenticated, and maybe the Interceptor for storing the local reference <code>user = authenticationService.loggedInUser</code>. But also I don't think this the best way to go. I suggest you'd create an abstract <code>AuthenticationAwareController</code> as base class for all your controllers in src/groovy and there have the convenience method like <code>user</code></p> <pre><code>class AuthenticationAwareController { def authenticationService def getUser() { return authenticationService.loggedInUser() } } </code></pre> <p>This way, you can later change you mind about storing the user however you like and don't have to change your code. Also you benefit from Caches in Hibernate, that share already retrieved user object instances between different sessions, so db roundtrips are avoided.</p> <p>You still should check the retrieved user object for validity or throw an <code>AuthenticationException</code> in case the retrieval does not succeed. (Maybe something like <code>AuthenticationService.getLoggedInUser(failOnError = false)</code>.)</p> <p>You can even make this Service/ControllerBase a small plugin an reuse that on every application or go directly with the spring security plugin... ;-)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload