Возврат 404 на конечную точку пружины пружины и привода - PullRequest
0 голосов
/ 22 октября 2019

Когда в производстве я хочу отключить конечную точку / исполнительного механизма, но все же разрешить / исполнительный механизм / исправностьЯ пробовал приведенный ниже код, используя SecurityConfigurerAdapter, но он возвращает 500. Я хочу вернуть 404 и получить страницу с ошибкой «страница не найдена». Любая помощь очень ценится

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

Ответы [ 2 ]

1 голос
/ 22 октября 2019
@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();
}
0 голосов
/ 22 октября 2019

Вам не нужно использовать Spring Security.

Это можно настроить с помощью свойств:

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

По умолчанию здоровье и информация предоставляются через Интернет. .

Таким образом, вы можете включить для производства, а в разработке вы запускаете свое приложение с

-Dmanagement.endpoints.web.exposure.include=*
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...