簡體   English   中英

在 Spring 引導/執行器根端點上返回 404

[英]Return 404 on Spring Boot /actuator root endpoint

在生產中,我想禁用 /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();
        }
    }

您不必使用 Spring 安全性。

這可以使用屬性進行配置:

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

默認情況下,運行狀況和信息通過 web 公開。

因此,您可以在運行應用程序的生產和開發中進行中繼

-Dmanagement.endpoints.web.exposure.include=*
@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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM