Skip to content

Commit bc9f8ec

Browse files
edeandrearwinch
authored andcommitted
Add HttpStatusServerEntryPoint
An HttpStatusServerEntryPoint is missing on the reactive side - essentially the reactive equivalent of HttpStatusEntryPoint. Fixes gh-5082
1 parent 5385097 commit bc9f8ec

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.server.authentication;
18+
19+
import org.springframework.http.HttpStatus;
20+
import org.springframework.security.core.AuthenticationException;
21+
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
22+
import org.springframework.util.Assert;
23+
import org.springframework.web.server.ServerWebExchange;
24+
25+
import reactor.core.publisher.Mono;
26+
27+
/**
28+
* A {@link ServerAuthenticationEntryPoint} that sends a generic {@link HttpStatus} as a
29+
* response. Useful for JavaScript clients which cannot use Basic authentication since the
30+
* browser intercepts the response.
31+
*
32+
* @author Eric Deandrea
33+
* @since 5.1
34+
*/
35+
public class HttpStatusServerEntryPoint implements ServerAuthenticationEntryPoint {
36+
private final HttpStatus httpStatus;
37+
38+
public HttpStatusServerEntryPoint(HttpStatus httpStatus) {
39+
Assert.notNull(httpStatus, "httpStatus cannot be null");
40+
this.httpStatus = httpStatus;
41+
}
42+
43+
@Override
44+
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException authException) {
45+
return Mono.fromRunnable(() -> exchange.getResponse().setStatusCode(this.httpStatus));
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.server.authentication;
18+
19+
import static org.assertj.core.api.Assertions.*;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import org.springframework.http.HttpStatus;
25+
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
26+
import org.springframework.mock.web.server.MockServerWebExchange;
27+
import org.springframework.security.core.AuthenticationException;
28+
29+
/**
30+
* @author Eric Deandrea
31+
* @since 5.1
32+
*/
33+
public class HttpStatusServerEntryPointTests {
34+
private MockServerHttpRequest request;
35+
private MockServerWebExchange exchange;
36+
private AuthenticationException authException;
37+
private HttpStatusServerEntryPoint entryPoint;
38+
39+
@Before
40+
public void setup() {
41+
this.request = MockServerHttpRequest.get("/").build();
42+
this.exchange = MockServerWebExchange.from(this.request);
43+
this.authException = new AuthenticationException("") { };
44+
this.entryPoint = new HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED);
45+
}
46+
47+
@Test
48+
public void constructorNullStatus() {
49+
assertThatExceptionOfType(IllegalArgumentException.class)
50+
.isThrownBy(() -> new HttpStatusServerEntryPoint(null))
51+
.withMessage("httpStatus cannot be null");
52+
}
53+
54+
@Test
55+
public void unauthorized() {
56+
this.entryPoint.commence(this.exchange, this.authException).block();
57+
assertThat(this.exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
58+
}
59+
}

0 commit comments

Comments
 (0)