This repository has been archived by the owner on Nov 29, 2022. It is now read-only.
/
SAMLRelayStateSuccessHandler.java
74 lines (64 loc) · 2.97 KB
/
SAMLRelayStateSuccessHandler.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
/* Copyright 2010 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Implementation of a success handler which interprets meaning of the RelayState inside SAMLCredential
* as an URL to redirect user to.
*
* @author Vladimir Schafer
*/
public class SAMLRelayStateSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
/**
* Class logger.
*/
protected static final Logger log = LoggerFactory.getLogger(SAMLRelayStateSuccessHandler.class);
/**
* Implementation tries to load RelayString from the SAMLCredential authentication object and in case the state
* is present uses it as the target URL. In case the state is missing behaviour is the same as of the
* SavedRequestAwareAuthenticationSuccessHandler.
*/
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
Object credentials = authentication.getCredentials();
if (credentials instanceof SAMLCredential) {
SAMLCredential samlCredential = (SAMLCredential) credentials;
String relayStateURL = getTargetURL(samlCredential.getRelayState());
if (relayStateURL != null) {
log.debug("Redirecting to RelayState Url: " + relayStateURL);
getRedirectStrategy().sendRedirect(request, response, relayStateURL);
return;
}
}
super.onAuthenticationSuccess(request, response, authentication);
}
/**
* Method is responsible for processing relayState and returning URL the system can redirect to. Method
* can decide to ignore the relayState and redirect user to default location by returning null.
*
* @param relayState relay state to process, can be null
* @return null to ignore the state, URL to redirect to otherwise
*/
protected String getTargetURL(String relayState) {
return relayState;
}
}