Spring Security не выполняет никакой роли / проверки полномочий для запросов API - PullRequest
0 голосов
/ 27 марта 2020

Я написал нижеприведенный весенний фильтр безопасности, который в большинстве случаев работает, как и ожидалось, но он не проверяет роли или полномочия, которые пользователь может иметь или не иметь:

    http
            .anonymous()
            .disable()
            .requestMatcher(request -> {
                return request.getRequestURL().toString().contains("api-docs");
            }).authorizeRequests()
            .anyRequest().hasAnyAuthority("DEVELOPER").anyRequest().authenticated()
            .and()
            .httpBasic()
            .authenticationEntryPoint(this.authEntryPoint())
            .and()
            .addFilterAfter(this.authExceptionFilter(), ExceptionTranslationFilter.class)
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .exceptionHandling()
            .authenticationEntryPoint(this.authEntryPoint())
            .and()
            .csrf()
            .disable();

В приведенном выше фрагменте, Я добавил .hasAnyRole ("DEVELOPER"), чтобы разрешить пользователю доступ к этому API, только если он имеет роль DEVELOPER. Пользователь прошел проверку подлинности, но к нему не была прикреплена роль РАЗРАБОТЧИКА, но ему не было отказано в доступе к API.

1 Ответ

0 голосов
/ 27 марта 2020

Попробуйте это:

http
                .authorizeRequests()
                .antMatchers("/api-docs/**").permitAll()
                .anyRequest().hasAnyAuthority("DEVELOPER")
                .and()
                .httpBasic()
                .authenticationEntryPoint(this.authEntryPoint())
                .and()
                .addFilterAfter(this.authExceptionFilter(), ExceptionTranslationFilter.class)
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(this.authEntryPoint())
                .and()
                .csrf()
                .disable();

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...