authentication - Propagating user information to all layers in Dropwizard -
i using basic authentication mechanism in dropwizard application, capturing logged in user details, this:
@post @timed @consumes(mediatype.application_json) @produces("application/pdf") @path("/") @rolesallowed("user,admin") public response function(@auth user user) throws exception { //some logic around here }
now auditing purposes, want user information passed @ each layer of application, mean in services, daos, exceptionmappers etc , don't want pass function parameter everywhere looks clumsy , has maintainability overhead. question is, there way can set global configuration per rest call or user session , can fetch anywhere want? have been ruby user , in able this:
thread.current[:user] = user
which accessible throughout per user session.
one way achieve using threadlocal of java wherein can set user object , available particular executor thread.
add following resource class.
private static threadlocal<user> localuser = new inheritablethreadlocal<>(); public static shortenerserviceuser getuser() { return localuser.get(); }
whennever function() method invoked, need set user object threadlocal variable.
localuser.set(user);
now, whenever need access user object current thread context, need follows
user localuser = yourresource.getuser();
you can clear user object context using threadlocal.remove() method.
Comments
Post a Comment