cdi execute code on session start

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

cdi execute code on session start

chongma
what is the best way to run code when a session is first created?  i
thought maybe a @PostConstruct inside a @Named @SessionScoped but it
doesn't trigger.
Reply | Threaded
Open this post in threaded view
|

Re: cdi execute code on session start

Romain Manni-Bucau
@WebListener on a HttpSessionListener?

otherwise @Observes @Initialiazed(SessionScoped.class) in the cdi bean


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<https://rmannibucau.metawerx.net/> | Old Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Book
<https://www.packtpub.com/application-development/java-ee-8-high-performance>

2018-05-07 17:10 GMT+02:00 Matthew Broadhead <[hidden email]
>:

> what is the best way to run code when a session is first created?  i
> thought maybe a @PostConstruct inside a @Named @SessionScoped but it
> doesn't trigger.
>
Reply | Threaded
Open this post in threaded view
|

Re: cdi execute code on session start

chongma
@WebListener works fine

public void processSessionScopedInit(@Observes
@Initialized(SessionScoped.class) HttpSession payload) {} caused an
infinite loop in the code.  i can send you a stack trace but it grows so
long it is hard to find the initial cause

however i have not fixed my problem because
KeycloakPrincipal<KeycloakSecurityContext> is not available at session
start.  The session starts before the redirect to the login page.

is there any way to execute code just after a user signs in?  i am just
trying to log username and ip for security purposes


On 07/05/18 18:18, Romain Manni-Bucau wrote:

> @WebListener on a HttpSessionListener?
>
> otherwise @Observes @Initialiazed(SessionScoped.class) in the cdi bean
>
>
> Romain Manni-Bucau
> @rmannibucau <https://twitter.com/rmannibucau> |  Blog
> <https://rmannibucau.metawerx.net/> | Old Blog
> <http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
> LinkedIn <https://www.linkedin.com/in/rmannibucau> | Book
> <https://www.packtpub.com/application-development/java-ee-8-high-performance>
>
> 2018-05-07 17:10 GMT+02:00 Matthew Broadhead <[hidden email]
>> :
>> what is the best way to run code when a session is first created?  i
>> thought maybe a @PostConstruct inside a @Named @SessionScoped but it
>> doesn't trigger.
>>

Reply | Threaded
Open this post in threaded view
|

Re: cdi execute code on session start

Romain Manni-Bucau
Le mar. 8 mai 2018 10:50, Matthew Broadhead <[hidden email]>
a écrit :

> @WebListener works fine
>
> public void processSessionScopedInit(@Observes
> @Initialized(SessionScoped.class) HttpSession payload) {} caused an
> infinite loop in the code.  i can send you a stack trace but it grows so
> long it is hard to find the initial cause
>


Put a breakpoint ;)

>
> however i have not fixed my problem because
> KeycloakPrincipal<KeycloakSecurityContext> is not available at session
> start.  The session starts before the redirect to the login page.
>
> is there any way to execute code just after a user signs in?  i am just
> trying to log username and ip for security purposes
>

Depends your login mecanism, maybe have a look on keycloak side



>
> On 07/05/18 18:18, Romain Manni-Bucau wrote:
> > @WebListener on a HttpSessionListener?
> >
> > otherwise @Observes @Initialiazed(SessionScoped.class) in the cdi bean
> >
> >
> > Romain Manni-Bucau
> > @rmannibucau <https://twitter.com/rmannibucau> |  Blog
> > <https://rmannibucau.metawerx.net/> | Old Blog
> > <http://rmannibucau.wordpress.com> | Github <
> https://github.com/rmannibucau> |
> > LinkedIn <https://www.linkedin.com/in/rmannibucau> | Book
> > <
> https://www.packtpub.com/application-development/java-ee-8-high-performance
> >
> >
> > 2018-05-07 17:10 GMT+02:00 Matthew Broadhead <
> [hidden email]
> >> :
> >> what is the best way to run code when a session is first created?  i
> >> thought maybe a @PostConstruct inside a @Named @SessionScoped but it
> >> doesn't trigger.
> >>
>
>