Spring webflux - Как защитить RESTful частные ресурсы? - PullRequest
0 голосов
/ 25 сентября 2019

HI У меня есть приложение springboot webflux с защитой весенней загрузки.Я аутентифицируюсь, используя базовую аутентификацию.У меня есть конечная точка, предоставляющая частный ресурс /users/{userId}/notifications, и я хочу убедиться, что только пользователи, прошедшие проверку подлинности, могут получить доступ к конкретному частному ресурсу.

Например, я аутентифицируюсь как пользователь с идентификатором: 3

Я МОГУ получить доступ: /users/3/notifications

Я НЕ МОГУ доступ: /users/1/notifications

Здесьмой пример кода с использованием @PreAuthorize, но он совсем не работает

    @PostMapping("/users/{userId}/notifications")
    // @Secured(value = ["userid"]) - also doesn't work
    @PreAuthorize("#userId == principal.id")
    fun addEmailNotification(@RequestBody emailNotificationCreationForm: @Valid EmailNotificationCreationForm): Mono<ResponseEntity<EmailNotificationCreationResponse>> {
//        log.info("Adding email notification {}", emailNotificationCreationForm)
        return Mono
                .just(emailNotificationCreationForm)
                .doOnNext { println(it) }
                .flatMap { Mono.just(EmailNotificationSetting("aa","bb")) }
                .map { EmailNotificationCreationResponse(true, 201, it) }
                .map { ResponseEntityUtils.toResponseEntity(it, HttpStatus.CREATED) }
    }

Конфигурация безопасности

@Configuration
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
internal open class SecurityConfig(private val userService: UserService) {

    companion object {
        private val AUTH_WHITELIST = arrayOf("/actuator/**", "/v2/api-docs", "/swagger-resources", "/swagger-resources/**", "/configuration/ui", "/configuration/security", "/swagger-ui.html", "/webjars/**")
    }

    @Bean
    open fun securityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        return http
                .csrf().disable()
                .authorizeExchange()
                .pathMatchers(HttpMethod.POST, REGISTRATION_REQUESTS_ENDPOINT).permitAll()
                .pathMatchers(HttpMethod.POST, AUTH_ENDPOINT).permitAll()
                .pathMatchers(*AUTH_WHITELIST).permitAll()
                .pathMatchers(HttpMethod.POST, Endpoints.forSecurity(REGISTRATION_REQUEST_CONFIRMATIONS_ENDPOINT)).permitAll()
                .pathMatchers(HttpMethod.GET, Endpoints.forSecurity(REGISTRATION_REQUEST_CONFIRMATION_ENDPOINT)).permitAll()
                .anyExchange().authenticated()
                .and()
                .httpBasic()
                .and()
                .formLogin().disable()
                .build()
    }
...