Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace dynamic error message with static "Access Denied" #16528

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void csrfWhenMutateWithThenDisablesCsrf() {
.expectStatus()
.isEqualTo(HttpStatus.FORBIDDEN)
.expectBody()
.consumeWith((b) -> assertThat(new String(b.getResponseBody())).contains("CSRF"));
.consumeWith((b) -> assertThat(new String(b.getResponseBody())).contains("Access Denied"));
this.client.mutateWith(SecurityMockServerConfigurers.csrf()).post().exchange().expectStatus().isOk();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,7 +54,7 @@ public Mono<Void> handle(ServerWebExchange exchange, AccessDeniedException ex) {
response.setStatusCode(this.httpStatus);
response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
DataBufferFactory dataBufferFactory = response.bufferFactory();
DataBuffer buffer = dataBufferFactory.wrap(ex.getMessage().getBytes(Charset.defaultCharset()));
DataBuffer buffer = dataBufferFactory.wrap("Access Denied".getBytes(Charset.defaultCharset()));
return response.writeWith(Mono.just(buffer)).doOnError((error) -> DataBufferUtils.release(buffer));
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,9 +23,9 @@

import org.springframework.http.HttpStatus;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.http.server.reactive.MockServerHttpResponse;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.server.ServerWebExchange;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
Expand All @@ -39,7 +39,7 @@
public class HttpStatusServerAccessDeniedHandlerTests {

@Mock
private ServerWebExchange exchange;
private MockServerWebExchange exchange;

private HttpStatus httpStatus = HttpStatus.FORBIDDEN;

Expand All @@ -62,7 +62,9 @@ public void commenceWhenNoSubscribersThenNoActions() {
public void commenceWhenSubscribeThenStatusSet() {
this.exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/").build());
this.handler.handle(this.exchange, this.exception).block();
assertThat(this.exchange.getResponse().getStatusCode()).isEqualTo(this.httpStatus);
MockServerHttpResponse response = this.exchange.getResponse();
assertThat(response.getStatusCode()).isEqualTo(this.httpStatus);
assertThat(response.getBodyAsString().block()).isEqualTo("Access Denied");
}

@Test
Expand All @@ -71,7 +73,9 @@ public void commenceWhenCustomStatusSubscribeThenStatusSet() {
this.handler = new HttpStatusServerAccessDeniedHandler(this.httpStatus);
this.exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/").build());
this.handler.handle(this.exchange, this.exception).block();
assertThat(this.exchange.getResponse().getStatusCode()).isEqualTo(this.httpStatus);
MockServerHttpResponse response = this.exchange.getResponse();
assertThat(response.getStatusCode()).isEqualTo(this.httpStatus);
assertThat(response.getBodyAsString().block()).isEqualTo("Access Denied");
}

}