This repository has been archived by the owner on Nov 29, 2022. It is now read-only.
/
SAMLContextProviderLB.java
175 lines (148 loc) · 5.55 KB
/
SAMLContextProviderLB.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright 2013 Vladimir Schafer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.saml.context;
import org.opensaml.saml2.metadata.provider.MetadataProviderException;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
/**
* Context provider which overrides request attributes with values of the load-balancer or reverse-proxy in front
* of the local application. The settings help to provide correct redirect URls and verify destination URLs during
* SAML processing.
*/
public class SAMLContextProviderLB extends SAMLContextProviderImpl {
private String scheme;
private String serverName;
private boolean includeServerPortInRequestURL;
private int serverPort;
private String contextPath;
/**
* Method wraps the original request and provides values specified for load-balancer. The following methods
* are overriden: getContextPath, getRequestURL, getRequestURI, getScheme, getServerName, getServerPort and isSecure.
*
* @param request original request
* @param response response object
* @param context context to populate values to
*/
@Override
protected void populateGenericContext(HttpServletRequest request, HttpServletResponse response, SAMLMessageContext context) throws MetadataProviderException {
super.populateGenericContext(new LPRequestWrapper(request), response, context);
}
/**
* Wrapper for original request which overrides values of scheme, server name, server port and contextPath.
* Method isSecure returns value based on specified scheme.
*/
private class LPRequestWrapper extends HttpServletRequestWrapper {
private LPRequestWrapper(HttpServletRequest request) {
super(request);
}
@Override
public String getContextPath() {
return contextPath;
}
@Override
public String getScheme() {
return scheme;
}
@Override
public String getServerName() {
return serverName;
}
@Override
public int getServerPort() {
return serverPort;
}
@Override
public String getRequestURI() {
StringBuilder sb = new StringBuilder(contextPath);
sb.append(getServletPath());
return sb.toString();
}
@Override
public StringBuffer getRequestURL() {
StringBuffer sb = new StringBuffer();
sb.append(scheme).append("://").append(serverName);
if (includeServerPortInRequestURL) sb.append(":").append(serverPort);
sb.append(contextPath);
sb.append(getServletPath());
if (getPathInfo() != null) sb.append(getPathInfo());
return sb;
}
@Override
public boolean isSecure() {
return "https".equalsIgnoreCase(scheme);
}
}
/**
* Scheme of the LB server - either http or https
*
* @param scheme scheme
*/
public void setScheme(String scheme) {
this.scheme = scheme;
}
/**
* Server name of the LB, e.g. www.myserver.com
*
* @param serverName server name
*/
public void setServerName(String serverName) {
this.serverName = serverName;
}
/**
* Port of the server, in case value is <= 0 port will not be included in the requestURL and port
* from the original request will be used for getServerPort calls.
*
* @param serverPort server port
*/
public void setServerPort(int serverPort) {
this.serverPort = serverPort;
}
/**
* When true serverPort will be used in construction of LB requestURL.
*
* @param includeServerPortInRequestURL true to include port
*/
public void setIncludeServerPortInRequestURL(boolean includeServerPortInRequestURL) {
this.includeServerPortInRequestURL = includeServerPortInRequestURL;
}
/**
* Context path of the LB, must be starting with slash, e.g. /saml-extension
*
* @param contextPath context path
*/
public void setContextPath(String contextPath) {
if (contextPath == null || "/".equals(contextPath)) {
contextPath = "";
}
this.contextPath = contextPath;
}
/**
* Verifies that required entities were autowired or set and initializes resolvers used to construct trust engines.
*/
public void afterPropertiesSet() throws ServletException {
super.afterPropertiesSet();
Assert.hasText(scheme, "Scheme must be set");
Assert.hasText(serverName, "Server name must be set");
Assert.notNull(contextPath, "Context path must be set");
if (StringUtils.hasLength(contextPath)) {
Assert.isTrue(contextPath.startsWith("/"), "Context path must be set and start with a forward slash");
}
}
}