Tung
06/21/2024, 8:16 AMapi/validate
. I want to count hits when every client hits this endpoint. I am trying to using Filters
it doesn’t work as expected. The app doesn’t go through the filter. Below is my Filters class.
class CountHitAccessTokenFilters {
def restAccessTokenService
def filters = {
updateHitCount(uri: 'api/validate') {
before = {
println "Come here..."
// <http://log.info|log.info> "Come here..."
restAccessTokenService.countAccessToken()
}
after = { Map model ->
println "Increasing hit count..."
// <http://log.info|log.info> "Increasing hit count..."
// <http://log.info|log.info> model.dump()
restAccessTokenService.countAccessToken()
}
afterView = { Exception e ->
}
}
}
}
I am also using controller interceptor but this endpoint is controlled by the plugin.
Have you ever experienced with this?Tung
06/25/2024, 9:35 AMjdaugherty
06/27/2024, 7:16 PMRestTokenValidationFilter
jdaugherty
06/27/2024, 7:17 PMTung
06/28/2024, 9:43 AMTung
06/28/2024, 11:20 AMpackage net.biomodels.jummp.plugins.security
import grails.plugin.springsecurity.rest.token.AccessToken
import grails.plugin.springsecurity.userdetails.GrailsUser
import grails.transaction.Transactional
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationListener
import org.springframework.security.authentication.event.AuthenticationSuccessEvent
/**
* Service for counting the hits of using Access Token via REST API.
*
*/
@Transactional
class RestValidationTokenService implements ApplicationListener<AuthenticationSuccessEvent> {
private static final Logger LOGGER = LoggerFactory.getLogger(RestValidationTokenService.class)
RestValidationTokenService() {
super()
}
@Override
void onApplicationEvent(AuthenticationSuccessEvent successEvent) {
GrailsUser principal = successEvent.source.principal as GrailsUser
String username = principal.username
String msg
if (successEvent.source instanceof AccessToken) {
String accessToken = successEvent.source.accessToken
msg = """The system has authenticated the principal (username: $username, \
token ending ${accessToken[-8..-1]}) via Access Token Based Authentication."""
AuthTokenManager auth = AuthTokenManager.findByAccessToken(accessToken)
if (auth) {
auth.hitCount = auth.hitCount + 1
if (!auth.save(flush: true)) {
LOGGER.debug("Failed to increment the hit count for the token ending ${accessToken[-8..-1]}")
}
}
} else {
msg = """The system has authenticated the principal (username: $username) \
via Basic or Form Based Authentication."""
}
println(msg)
<http://LOGGER.info|LOGGER.info>(msg)
}
}