Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

@Counted remains 0 for spring security handlers #157

Open
jasw opened this issue Dec 3, 2015 · 2 comments
Open

@Counted remains 0 for spring security handlers #157

jasw opened this issue Dec 3, 2015 · 2 comments

Comments

@jasw
Copy link

jasw commented Dec 3, 2015

Hi all, I think I encountered a bug with 3.1.3 using @counted to annotate a Spring form login SuccessHandler:


/**
 * Spring Security success handler, specialized for Ajax requests.
 */
@Component
public class AjaxAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();


    @Counted
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                        Authentication authentication)
            throws IOException, ServletException {

        response.setStatus(HttpServletResponse.SC_OK);
        if(!response.isCommitted()){

            redirectStrategy.sendRedirect(request, response, "../index.zul");
        }

    }
}

The above code would register a Counter successfully as I could see both in the log and reporter.

However, no matter how many times that method is invoked the counter remains 0.

I do use @EnableMetrics(proxyTargetClass = true)

If remove the annotation and doing the counter by hand like the following, the counter works well:

@Component
public class AjaxAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

    @Inject
    private MetricRegistry metricRegistry;

    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

    private Counter loginCounter;

    //@Timed does not work.
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                        Authentication authentication)
        throws IOException, ServletException {
        if(loginCounter == null){
            loginCounter = metricRegistry.counter("succeededLogin");
        }
        loginCounter.inc();
        response.setStatus(HttpServletResponse.SC_OK);
        if (!response.isCommitted()) {

            redirectStrategy.sendRedirect(request, response, "../index.zul");
        }

    }
}

Thanks.

@ryantenney
Copy link
Owner

Sorry I missed this issue, were you able to resolve it?

The issue should be where you added @EnableMetrics, since the WebApplicationContext (under which security typically runs) is separate from you ApplicationContext, so adding @EnableMetrics to the security config class should fix it.

@streetlight8023
Copy link

you shoud add like this @counted(monotonic=true)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants