【问题标题】:Return 404 on Spring Boot /actuator root endpoint在 Spring Boot /actuator 根端点上返回 404
【发布时间】:2020-02-18 09:17:43
【问题描述】:

当在生产中我想禁用 /actuator 端点但仍然允许 /actuator/health。我已经使用 SecurityConfigurerAdapter 尝试了下面的代码,但它返回 500。我想返回 404 并获得“找不到页面”错误页面。非常感谢任何帮助

  @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        if(isProd) {
            http.authorizeRequests().antMatchers("/actuator/", "/actuator").denyAll();
        }
    }

【问题讨论】:

    标签: java spring spring-boot spring-security spring-boot-actuator


    【解决方案1】:

    您不必使用 Spring Security。

    这可以使用属性进行配置:

    https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-endpoints-exposing-endpoints

    默认情况下,健康和信息通过网络公开。

    因此,您可以在生产和开发中继续运行您的应用程序

    -Dmanagement.endpoints.web.exposure.include=*
    

    【讨论】:

    • 不要以为你明白我的问题。文字 /actuator 端点默认也是公开的,它返回公开的执行器端点。我不希望这个在生产中。我想要的是 /actuator/health 但不是 /actuator
    • 你为什么要这样做?这将只显示可用端点的链接
    【解决方案2】:
    @Configuration
    @EnableWebSecurity
    //@EnableOAuth2Sso
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Autowired
        private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
        @Autowired
        private JwtRequestFilter jwtRequestFilter;
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .csrf().disable()
                    // dont authenticate this particular request
                    .authorizeRequests()
                    .antMatchers(
                            "/api/login",
                            "/user/create-new-user",
                            "/user/get-verification",
                            "/user/pwd-reset",
                            "/user/pwd-reset/verification",
                            "/api/swagger-ui.html")
                    .permitAll()
    //                .antMatchers("/**").permitAll().hasRole("ADMIN")
                    .anyRequest()
                    .fullyAuthenticated()
                    .and()
                    .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
                    .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    //                .and()
    //                .logout()
    //                .logoutRequestMatcher(new AntPathRequestMatcher("/api/logout")).logoutSuccessUrl("/https://www.baeldung.com/spring-security-logout")
    //                .invalidateHttpSession(true).deleteCookies("JSESSIONID");
    
            // Add a filter to validate the tokens with every request
            http
                    .addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
        }
    }
    

    或者这样用

    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
          .antMatchers("/", "/home").access("hasRole('USER')")
          .antMatchers("/admin/**").hasRole("ADMIN")
          .and()
          // some more method calls
          .formLogin();
    }
    

    【讨论】: