首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Boot /actuator根终结点返回404

Spring Boot /actuator根终结点返回404
EN

Stack Overflow用户
提问于 2019-10-22 16:59:32
回答 2查看 259关注 0票数 0

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

代码语言:javascript
复制
  @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        if(isProd) {
            http.authorizeRequests().antMatchers("/actuator/", "/actuator").denyAll();
        }
    }
EN

回答 2

Stack Overflow用户

发布于 2019-10-22 17:52:24

代码语言:javascript
复制
@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);
    }
}

或者使用这种方式

代码语言:javascript
复制
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
      .antMatchers("/", "/home").access("hasRole('USER')")
      .antMatchers("/admin/**").hasRole("ADMIN")
      .and()
      // some more method calls
      .formLogin();
}
票数 1
EN

Stack Overflow用户

发布于 2019-10-22 17:04:26

你不需要使用Spring Security。

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

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

默认情况下,健康和信息是通过web公开的。

因此您可以在生产和开发中使用来运行您的应用程序

代码语言:javascript
复制
-Dmanagement.endpoints.web.exposure.include=*
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58500599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档