当前位置: 首页 > 知识库问答 >
问题:

Spring引导/执行器根endpoint上的返回404

田兴朝
2023-03-14

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

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

共有1个答案

葛子昂
2023-03-14
@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();
}
 类似资料: