users@jersey.java.net

Re: [Jersey] Principal injection?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 04 Nov 2009 16:17:02 +0100

On Nov 4, 2009, at 3:31 PM, Casper Bang wrote:

> I do it the same way, implementing my own Principal such as to be able
> to add custom context (i.e. allow multiple credentials). If I remember
> correctly, this was the recommended aproach by Paul - but it would be
> interesting if you could somehow inject @User, to match the rest of
> Jersey's way of doing things. :)
>

You can develop your own injectable provider to inject the User if you
like.

@Provider
public class UserInjector implements InjectableProvider<Context, Type> {

     private final SecurityContext securityContext;

     public UserInjector(@Context SecurityContext securityContext) {
         this.securityContext = securityContext;
     }

     public ComponentScope getScope() {
         return ComponentScope.PerRequest;
     }

     public Injectable getInjectable(ComponentContext ic, Context a,
Type c) {
         if (c == User.class) {
             return new Injectable<User>() {
                 return
((InternalPrincipal)securityContext.getUserPrincipal()).getUser();
             };
         }
         return null;
     }
}

Paul.

> /Casper
>
>
> Zoltan Arnold NAGY wrote:
>> Hi,
>>
>> I'm wondering if there's a better way.. I'm currently doing it like
>> this:
>> @Path("/d")
>> public class DatasResource {
>> @Context
>> private SecurityContext securityContext;
>>
>> @PUT
>> @Consumes(MediaType.APPLICATION_JSON)
>> public Response addData(Sample s) {
>> User user =
>> ((InternalPrincipal)securityContext.getUserPrincipal()).getUser();
>> ..
>> }
>>
>> my own filter sets the appropriate principal (and securitycontext),
>> and it's
>> always set.
>>
>> Thanks,
>> Zoltan
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>