Skip to content

Commit 0c55c54

Browse files
committed
Document how to use Jersey with Spring Security's method security
Closes gh-12995
1 parent e52b721 commit 0c55c54

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,30 @@ that is compatible with Thymeleaf 3.0.
14851485

14861486

14871487

1488+
[[howto-jersey]]
1489+
== Jersey
1490+
1491+
1492+
1493+
[[howto-jersey-spring-security]]
1494+
=== Secure Jersey endpoints with Spring Security
1495+
Spring Security can be used to secure a Jersey-based web application in much the same
1496+
way as it can be used to secure a Spring MVC-based web application. However, if you want
1497+
to use Spring Security's method-level security with Jersey, you must configure Jersey to
1498+
use `setStatus(int)` rather `sendError(int)`. This prevents Jersey from committing the
1499+
response before Spring Security has had an opportunity to report an authentication or
1500+
authorization failure to the client.
1501+
1502+
The `jersey.config.server.response.setStatusOverSendError` must be set to `true` on the
1503+
application's `ResourceConfig` bean, as shown in the following example:
1504+
1505+
[source,java,indent=0]
1506+
----
1507+
include::{code-examples}/jersey/JerseySetStatusOverSendErrorExample.java[tag=resource-config]
1508+
----
1509+
1510+
1511+
14881512
[[howto-http-clients]]
14891513
== HTTP clients
14901514

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-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.boot.jersey;
18+
19+
import java.util.Collections;
20+
21+
import javax.servlet.http.HttpServletResponse;
22+
23+
import org.glassfish.jersey.server.ResourceConfig;
24+
25+
import org.springframework.stereotype.Component;
26+
27+
/**
28+
* Example configuration for a Jersey {@link ResourceConfig} configured to use
29+
* {@link HttpServletResponse#setStatus(int)} rather than
30+
* {@link HttpServletResponse#sendError(int)}.
31+
*
32+
* @author Andy Wilkinson
33+
*/
34+
public class JerseySetStatusOverSendErrorExample {
35+
36+
// tag::resource-config[]
37+
@Component
38+
public class JerseyConfig extends ResourceConfig {
39+
40+
public JerseyConfig() {
41+
register(Endpoint.class);
42+
setProperties(Collections.singletonMap(
43+
"jersey.config.server.response.setStatusOverSendError", true));
44+
}
45+
46+
}
47+
// end::resource-config[]
48+
49+
static class Endpoint {
50+
51+
}
52+
53+
}

0 commit comments

Comments
 (0)