Hello everyone, I am using spring security rest fo...
# questions
t
Hello everyone, I am using spring security rest for grails 2.5.6. As you know, the endpoint validation is
api/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.
Copy code
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?
Anyone can help me?
j
I believe the validate endpoint isn't a traditional grails controller. It's a filter so that's why it's not working. The implementation of the /api/validate endpoint is this filter:
Copy code
RestTokenValidationFilter
You may want to look at https://stackoverflow.com/questions/34229750/invoke-a-filter-before-spring-security-filter-chain-in-boot since it's really a question of how do you inject code before/after a filter.
t
Thanks for your response anyway. I have looked into it and thought we might reply on Grails Events.
👍 1
I leave here my codes for anyone needing it.
Copy code
package 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)
    }
}