Skip to content
Merged
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
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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 @@ -107,31 +107,36 @@ protected String determineTargetUrl(HttpServletRequest request, HttpServletRespo
if (isAlwaysUseDefaultTargetUrl()) {
return this.defaultTargetUrl;
}
// Check for the parameter and use that if available
String targetUrl = null;
if (this.targetUrlParameter != null) {
targetUrl = request.getParameter(this.targetUrlParameter);
if (StringUtils.hasText(targetUrl)) {
if (this.logger.isTraceEnabled()) {
this.logger.trace(LogMessage.format("Using url %s from request parameter %s", targetUrl,
this.targetUrlParameter));
}
return targetUrl;
}
String targetUrlParameterValue = getTargetUrlParameterValue(request);
if (StringUtils.hasText(targetUrlParameterValue)) {
trace("Using url %s from request parameter %s", targetUrlParameterValue, this.targetUrlParameter);
return targetUrlParameterValue;
}
if (this.useReferer && !StringUtils.hasLength(targetUrl)) {
targetUrl = request.getHeader("Referer");
if (this.logger.isTraceEnabled()) {
this.logger.trace(LogMessage.format("Using url %s from Referer header", targetUrl));
}
if (this.useReferer) {
trace("Using url %s from Referer header", request.getHeader("Referer"));
return request.getHeader("Referer");
}
if (!StringUtils.hasText(targetUrl)) {
targetUrl = this.defaultTargetUrl;
if (this.logger.isTraceEnabled()) {
this.logger.trace(LogMessage.format("Using default url %s", targetUrl));
}
return this.defaultTargetUrl;
}

private String getTargetUrlParameterValue(HttpServletRequest request) {
if (this.targetUrlParameter == null) {
return null;
}
String value = request.getParameter(this.targetUrlParameter);
if (value == null) {
return null;
}
if (StringUtils.hasText(value)) {
return value;
}
return this.defaultTargetUrl;
}

private void trace(String msg, String... msgParts) {
if (this.logger.isTraceEnabled()) {
this.logger.trace(LogMessage.format(msg, msgParts));
}
return targetUrl;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2002-2023 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.
* 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.web.authentication;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Dayan Kodippily
*/
public class AbstractAuthenticationTargetUrlRequestHandlerTests {

public static final String REQUEST_URI = "https://example.org";

public static final String DEFAULT_TARGET_URL = "/defaultTarget";

public static final String REFERER_URL = "https://www.springsource.com/";

public static final String TARGET_URL = "https://example.org/target";

private MockHttpServletRequest request;

private MockHttpServletResponse response;

private AbstractAuthenticationTargetUrlRequestHandler handler;

@BeforeEach
void setUp() {
this.request = new MockHttpServletRequest();
this.response = new MockHttpServletResponse();
this.handler = new AbstractAuthenticationTargetUrlRequestHandler() {
@Override
protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response) {
return super.determineTargetUrl(request, response);
}
};
this.handler.setDefaultTargetUrl(DEFAULT_TARGET_URL);
this.request.setRequestURI(REQUEST_URI);
}

@Test
void returnDefaultTargetUrlIfUseDefaultTargetUrlTrue() {
this.handler.setAlwaysUseDefaultTargetUrl(true);
assertThat(this.handler.determineTargetUrl(this.request, this.response)).isEqualTo(DEFAULT_TARGET_URL);
}

@Test
void returnTargetUrlParamValueIfParamHasValue() {
this.handler.setTargetUrlParameter("param");
this.request.setParameter("param", TARGET_URL);
assertThat(this.handler.determineTargetUrl(this.request, this.response)).isEqualTo(TARGET_URL);
}

@Test
void targetUrlParamValueTakePrecedenceOverRefererIfParamHasValue() {
this.handler.setUseReferer(true);
this.handler.setTargetUrlParameter("param");
this.request.setParameter("param", TARGET_URL);
assertThat(this.handler.determineTargetUrl(this.request, this.response)).isEqualTo(TARGET_URL);
}

@Test
void returnDefaultTargetUrlIfTargetUrlParamHasNoValue() {
this.handler.setTargetUrlParameter("param");
this.request.setParameter("param", "");
assertThat(this.handler.determineTargetUrl(this.request, this.response)).isEqualTo(DEFAULT_TARGET_URL);
}

@Test
void returnDefaultTargetUrlIfTargetUrlParamHasNoValueContainsOnlyWhiteSpaces() {
this.handler.setTargetUrlParameter("param");
this.request.setParameter("param", " ");
assertThat(this.handler.determineTargetUrl(this.request, this.response)).isEqualTo(DEFAULT_TARGET_URL);
}

@Test
void returnRefererUrlIfUseRefererIsTrue() {
this.handler.setUseReferer(true);
this.request.addHeader("Referer", REFERER_URL);
assertThat(this.handler.determineTargetUrl(this.request, this.response)).isEqualTo(REFERER_URL);
}

@Test
void returnDefaultTargetUrlIfUseRefererIsFalse() {
this.handler.setUseReferer(false);
this.request.addHeader("Referer", REFERER_URL);
assertThat(this.handler.determineTargetUrl(this.request, this.response)).isEqualTo(DEFAULT_TARGET_URL);
}

}