【问题标题】:spring-boot, spring-security and dropwizard metricsspring-boot、spring-security 和 dropwizard 指标
【发布时间】:2015-09-03 07:30:03
【问题描述】:

我有一个带有 spring-security 和 dropwizard 指标的 spring-boot 应用程序。它使用 Angularjs 作为前端。身份验证是使用单独的 login.html 页面完成的,其中 angularjs 控制器将凭据发布到“/login”,并在将响应路由到 index.html(单独的 angularjs 应用程序)之后。在我尝试访问 dropwizard 指标之前,这一切都很好。在这种情况下,我收到一个 spring-security 异常,说用户是匿名的(所有其他 url 都可以正常工作)。

我的 spring-security 配置:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
@EnableWebSecurity
public class FormLoginSecurityConfigurer extends WebSecurityConfigurerAdapter {

    private class AuthSuccessHandler implements AuthenticationSuccessHandler {
        @Override
        public void onAuthenticationSuccess(HttpServletRequest request,
                HttpServletResponse response, Authentication authentication)
                throws IOException, ServletException {
            response.setStatus(HttpServletResponse.SC_OK);
        }
    }

    private class AuthFailureHandler implements AuthenticationFailureHandler {
        @Override
        public void onAuthenticationFailure(HttpServletRequest request,
                HttpServletResponse response, AuthenticationException exception)
                throws IOException, ServletException {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        }
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/login.html", "/scripts/login/**", "/libs/**", "/styles/**", "/images/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage("/login.html").loginProcessingUrl("/login")
                    .usernameParameter("username").passwordParameter("password")
                    .successHandler(new AuthSuccessHandler())
                    .failureHandler(new AuthFailureHandler())
                .and().logout().logoutUrl("/logout").logoutSuccessUrl("/login.html")
                .and().addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class)
                .csrf().csrfTokenRepository(CsrfHeaderFilter.csrfTokenRepository());
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
    }
}

metrics servlet 在 ServletContextInitilizer 中注册:

/**
 * Configuration of web application with Servlet 3.0 APIs.
 */
@Configuration
public class WebConfigurer implements ServletContextInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        initMetrics(servletContext,
                EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ASYNC));
    }

    /**
     * Initializes Metrics.
     */
    private void initMetrics(ServletContext servletContext, EnumSet<DispatcherType> disps) {
        log.debug("Initializing Metrics registries");
        servletContext.setAttribute(InstrumentedFilter.REGISTRY_ATTRIBUTE,
                metricRegistry);
        servletContext.setAttribute(MetricsServlet.METRICS_REGISTRY,
                metricRegistry);

        log.debug("Registering Metrics Filter");
        FilterRegistration.Dynamic metricsFilter = servletContext.addFilter("webappMetricsFilter",
                new InstrumentedFilter());

        metricsFilter.addMappingForUrlPatterns(disps, true, "/*");
        metricsFilter.setAsyncSupported(true);

        log.debug("Registering Metrics Servlet");
        ServletRegistration.Dynamic metricsAdminServlet =
                servletContext.addServlet("metricsServlet", new MetricsServlet());

        metricsAdminServlet.addMapping("/metrics/metrics/*");
        metricsAdminServlet.setAsyncSupported(true);
        metricsAdminServlet.setLoadOnStartup(2);
    }
}

但是,当我访问 /metrics/metrics 下的任何内容时,浏览器会提示进行基本身份验证。响应具有以下标头WWW-Authenticate:"Basic realm="Spring""。其他资源下载正常。

我是这种应用程序的新手,有点沮丧:) 感谢任何帮助。

【问题讨论】:

  • 您真的需要 Dropwizard 指标 servlet 吗?如果您只是在应用程序中包含 spring-boot-actuator,您将获得一个 /metrics 端点,它无论如何都会公开 dropwizard 指标。
  • 我试过没有 Dropwizard servlet 注册,但它没有解决问题 - 但你的评论让我搜索,我找到了这个线程 stackoverflow.com/questions/23174275/… 经过几次尝试后,我发现从我的删除 @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)配置解决了问题。现在 /metrics url 受到保护并且身份验证工作。我从 spring-boot-security 示例之一中获得了@Order。反正我也不知道这东西是干什么的:|
  • 谢谢@jst - 你的评论真的很有帮助。

标签: spring-security spring-boot dropwizard


【解决方案1】:

如果一个人知道要查找什么,这似乎都在文档中 - link

可以使用外部属性 (management.security.*) 修改执行器安全功能。要覆盖应用程序访问规则,请添加类型为 WebSecurityConfigurerAdapter 的 @Bean,如果您不想覆盖执行器访问规则,请使用 @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER),如果您想覆盖执行器访问规则,请使用 @Order(ManagementServerProperties.ACCESS_OVERRIDE_ORDER)执行器访问规则。

将顺序更改为 ManagementServerProperties.ACCESS_OVERRIDE_ORDER,现在可以使用了。

【讨论】:

    猜你喜欢
    • 2019-07-16
    • 2015-06-09
    • 1970-01-01
    • 2019-06-25
    • 2014-07-25
    • 2017-09-11
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    相关资源
    最近更新 更多