Unicon / cas-addons

Open source CAS customizations, extensions, and configuration aids.
http://unicon.github.io/cas-addons/
Apache License 2.0
54 stars 26 forks source link

Access original httpservletrequest from cas server events #57

Closed cm325 closed 10 years ago

cm325 commented 10 years ago

I implemented the cas server events as outlined here:

https://github.com/Unicon/cas-addons/wiki/CAS-server-events

And it's working great for capturing and storing cas login events and information from the principal object (and even the currently active theme). However I'd also like to be able to record the ip address of the request, is there some way to do this from inside this set of pointcuts? Thanks!

dima767 commented 10 years ago

Glad you like this events mini-framework. And thanks for reporting the desired use case. I don't think it's currently possible to get access to HttpServletRequest instance inside the event listeners, but will definitely look into implementing some solution that does.

Best, D.

cm325 commented 10 years ago

Hi, Thanks for the quick response, so ok, that's too bad. Do you have any thoughts on how one might get started on this, if I were to take this up? Thanks-

dima767 commented 10 years ago

OK, here's how you can easily get access to the client IP address from inside any listener implementation. If you use the Inspektr auditing library (the standard one that comes with CAS server), there is a ClientInfo object that is bound to the current request thread and hence available to any objects within that thread of execution via a ThreadLocal wrapper called ClientInfoHolder.

To configure it, one needs to have an Inspektr library on the classpath, and add the following filter definition to the web.xml:

<filter>
        <filter-name>CAS Client Info Logging Filter</filter-name>
        <filter-class>com.github.inspektr.common.web.ClientInfoThreadLocalFilter</filter-class></filter>
<filter-mapping>
       <filter-name>CAS Client Info Logging Filter</filter-name>
       <url-pattern>/*</url-pattern>
</filter-mapping>

and then access the ClientInfo object from anywhere within that thread of execution via a static accessor like so: ClientInfoHolder.getClientInfo()

Here's the example of accessing client IP address from CAS event listener implementation:

https://github.com/Unicon/unicon-cas-overlay/blob/master/src/main/java/net/unicon/cas/overlay/events/LogPublishingCasServiceTicketValidatedEventListener.java#L30

Hope this helps.

Cheers, D.

dima767 commented 10 years ago

Closing for now as there has not been any activity on this issue for a while and there is a way to get at the client IP address from any stack frame within a current thread of execution using the ThreadLocal wrapper from Inspektr library as described above.