Skip to content
Open

Cleanup #17801

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
Expand Up @@ -33,6 +33,7 @@
* and using the current URL minus the artifact and the corresponding value.
*
* @author Rob Winch
* @author Ngoc Nhan
*/
final class DefaultServiceAuthenticationDetails extends WebAuthenticationDetails
implements ServiceAuthenticationDetails {
Expand Down Expand Up @@ -70,14 +71,13 @@ public String getServiceUrl() {

@Override
public boolean equals(Object obj) {
if (this == obj) {
if (super.equals(obj)) {
return true;
}
if (!super.equals(obj) || !(obj instanceof DefaultServiceAuthenticationDetails)) {
return false;
if (obj instanceof DefaultServiceAuthenticationDetails that) {
return this.serviceUrl.equals(that.getServiceUrl());
}
ServiceAuthenticationDetails that = (ServiceAuthenticationDetails) obj;
return this.serviceUrl.equals(that.getServiceUrl());
return false;
}

@Override
Expand All @@ -100,17 +100,18 @@ public String toString() {
/**
* If present, removes the artifactParameterName and the corresponding value from the
* query String.
* @param request
* @param request the current {@link HttpServletRequest} to obtain the
* {@link #getServiceUrl()} from.
* @param artifactPattern the {@link Pattern} that will be used to clean up the query
* string from containing the artifact name and value. This can be created using
* {@link #createArtifactPattern(String)}.
* @return the query String minus the artifactParameterName and the corresponding
* value.
*/
private String getQueryString(final HttpServletRequest request, final Pattern artifactPattern) {
final String query = request.getQueryString();
if (query == null) {
return null;
}
String result = artifactPattern.matcher(query).replaceFirst("");
if (result.length() == 0) {
String result = (query != null) ? artifactPattern.matcher(query).replaceFirst("") : "";
if (result.isEmpty()) {
return null;
}
Copy link
Contributor

@ronodhirSoumik ronodhirSoumik Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String result = (query == null) ? "" : artifactPattern.matcher(query).replaceFirst("");

if (result.isEmpty()) {
    return null;
}

// strip off the trailing & only if the artifact was the first query param
Expand All @@ -121,8 +122,9 @@ private String getQueryString(final HttpServletRequest request, final Pattern ar
* Creates a {@link Pattern} that can be passed into the constructor. This allows the
* {@link Pattern} to be reused for every instance of
* {@link DefaultServiceAuthenticationDetails}.
* @param artifactParameterName
* @return
* @param artifactParameterName the artifactParameterName that is removed from the
* current URL. The result becomes the service url. Cannot be null or an empty String.
* @return a {@link Pattern}
*/
static Pattern createArtifactPattern(String artifactParameterName) {
Assert.hasLength(artifactParameterName, "artifactParameterName is expected to have a length");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.method.AbstractFallbackMethodSecurityMetadataSource;
import org.springframework.util.StringUtils;

/**
* Sources method security metadata from major JSR 250 security annotations.
Expand Down Expand Up @@ -108,7 +109,7 @@ private String getRoleWithDefaultPrefix(String role) {
if (role == null) {
return role;
}
if (this.defaultRolePrefix == null || this.defaultRolePrefix.length() == 0) {
if (!StringUtils.hasLength(this.defaultRolePrefix)) {
return role;
}
if (role.startsWith(this.defaultRolePrefix)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.util.function.SingletonSupplier;

/**
* Base root object for use in Spring Security expression evaluations.
*
* @author Luke Taylor
* @author Evgeniy Cheban
* @author Ngoc Nhan
* @since 3.0
*/
public abstract class SecurityExpressionRoot implements SecurityExpressionOperations {
Expand Down Expand Up @@ -167,7 +169,8 @@ public final boolean isFullyAuthenticated() {
/**
* Convenience method to access {@link Authentication#getPrincipal()} from
* {@link #getAuthentication()}
* @return
* @return the <code>Principal</code> being authenticated or the authenticated
* principal after authentication.
*/
public @Nullable Object getPrincipal() {
return getAuthentication().getPrincipal();
Expand Down Expand Up @@ -228,15 +231,15 @@ public void setPermissionEvaluator(PermissionEvaluator permissionEvaluator) {
/**
* Prefixes role with defaultRolePrefix if defaultRolePrefix is non-null and if role
* does not already start with defaultRolePrefix.
* @param defaultRolePrefix
* @param role
* @return
* @param defaultRolePrefix the default prefix to add to roles.
* @param role the role that should be required.
* @return a {@code String} role
*/
private static String getRoleWithDefaultPrefix(@Nullable String defaultRolePrefix, String role) {
if (role == null) {
return role;
}
if (defaultRolePrefix == null || defaultRolePrefix.length() == 0) {
if (!StringUtils.hasLength(defaultRolePrefix)) {
return role;
}
if (role.startsWith(defaultRolePrefix)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@

import org.jspecify.annotations.Nullable;

import org.springframework.util.StringUtils;

/**
* Implementation of PasswordEncoder.
*
* @author Rob Winch
* @since 7.0
*/
public abstract class AbstractValidatingPasswordEncoder implements PasswordEncoder {

@Override
Expand All @@ -32,21 +40,20 @@ public abstract class AbstractValidatingPasswordEncoder implements PasswordEncod

@Override
public final boolean matches(@Nullable CharSequence rawPassword, @Nullable String encodedPassword) {
if (rawPassword == null || rawPassword.length() == 0 || encodedPassword == null
|| encodedPassword.length() == 0) {
return false;
if (StringUtils.hasLength(rawPassword) && StringUtils.hasLength(encodedPassword)) {
return matchesNonNull(rawPassword.toString(), encodedPassword);
}
return matchesNonNull(rawPassword.toString(), encodedPassword);
return false;
}

protected abstract boolean matchesNonNull(String rawPassword, String encodedPassword);

@Override
public final boolean upgradeEncoding(@Nullable String encodedPassword) {
if (encodedPassword == null || encodedPassword.length() == 0) {
return false;
if (StringUtils.hasLength(encodedPassword)) {
return upgradeEncodingNonNull(encodedPassword);
}
return upgradeEncodingNonNull(encodedPassword);
return false;
}

protected boolean upgradeEncodingNonNull(String encodedPassword) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.security.web.authentication.rememberme;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -59,6 +58,7 @@
* @author Rob Winch
* @author Eddú Meléndez
* @author Onur Kagan Ozcan
* @author Ngoc Nhan
* @since 2.0
*/
public abstract class AbstractRememberMeServices
Expand Down Expand Up @@ -129,7 +129,7 @@ public Authentication autoLogin(HttpServletRequest request, HttpServletResponse
return null;
}
this.logger.debug("Remember-me cookie detected");
if (rememberMeCookie.length() == 0) {
if (rememberMeCookie.isEmpty()) {
this.logger.debug("Cookie was empty");
cancelCookie(request, response);
return null;
Expand Down Expand Up @@ -170,7 +170,7 @@ public Authentication autoLogin(HttpServletRequest request, HttpServletResponse
*/
protected String extractRememberMeCookie(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if ((cookies == null) || (cookies.length == 0)) {
if (cookies == null) {
return null;
}
for (Cookie cookie : cookies) {
Expand Down Expand Up @@ -220,12 +220,7 @@ protected String[] decodeCookie(String cookieValue) throws InvalidCookieExceptio
}
String[] tokens = StringUtils.delimitedListToStringArray(cookieAsPlainText, DELIMITER);
for (int i = 0; i < tokens.length; i++) {
try {
tokens[i] = URLDecoder.decode(tokens[i], StandardCharsets.UTF_8.toString());
}
catch (UnsupportedEncodingException ex) {
this.logger.error(ex.getMessage(), ex);
}
tokens[i] = URLDecoder.decode(tokens[i], StandardCharsets.UTF_8);
}
return tokens;
}
Expand All @@ -238,12 +233,7 @@ protected String[] decodeCookie(String cookieValue) throws InvalidCookieExceptio
protected String encodeCookie(String[] cookieTokens) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < cookieTokens.length; i++) {
try {
sb.append(URLEncoder.encode(cookieTokens[i], StandardCharsets.UTF_8.toString()));
}
catch (UnsupportedEncodingException ex) {
this.logger.error(ex.getMessage(), ex);
}
sb.append(URLEncoder.encode(cookieTokens[i], StandardCharsets.UTF_8));
if (i < cookieTokens.length - 1) {
sb.append(DELIMITER);
}
Expand Down Expand Up @@ -382,7 +372,7 @@ protected void setCookie(String[] tokens, int maxAge, HttpServletRequest request

private String getCookiePath(HttpServletRequest request) {
String contextPath = request.getContextPath();
return (contextPath.length() > 0) ? contextPath : "/";
return contextPath.isEmpty() ? "/" : contextPath;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* bypassed by the malicious addition of parameters to the path component.
*
* @author Luke Taylor
* @author Ngoc Nhan
*/
final class RequestWrapper extends FirewalledRequest {

Expand All @@ -56,7 +57,7 @@ final class RequestWrapper extends FirewalledRequest {
super(request);
this.strippedServletPath = strip(request.getServletPath());
String pathInfo = strip(request.getPathInfo());
if (pathInfo != null && pathInfo.length() == 0) {
if (pathInfo != null && pathInfo.isEmpty()) {
pathInfo = null;
}
this.strippedPathInfo = pathInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.security.web.util.UrlUtils;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;

/**
Expand All @@ -58,6 +59,7 @@
* @author Andrey Grebnev
* @author Ben Alex
* @author Luke Taylor
* @author Ngoc Nhan
*/
public class DefaultSavedRequest implements SavedRequest {

Expand Down Expand Up @@ -206,21 +208,17 @@ private void addLocale(Locale locale) {
* @since 4.2
*/
private void addParameters(Map<String, String[]> parameters) {
if (!ObjectUtils.isEmpty(parameters)) {
for (String paramName : parameters.keySet()) {
Object paramValues = parameters.get(paramName);
if (paramValues instanceof String[]) {
this.addParameter(paramName, (String[]) paramValues);
}
else {
logger.warn("ServletRequest.getParameterMap() returned non-String array");
}
}
if (ObjectUtils.isEmpty(parameters)) {
return;
}
}

private void addParameter(String name, String[] values) {
this.parameters.put(name, values);
for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
String name = entry.getKey();
String[] values = entry.getValue();
if (values != null) {
this.parameters.put(name, values);
}
}
}

/**
Expand Down Expand Up @@ -378,7 +376,7 @@ private static String createQueryString(String queryString, String matchingReque
if (matchingRequestParameterName == null) {
return queryString;
}
if (queryString == null || queryString.length() == 0) {
if (!StringUtils.hasLength(queryString)) {
return matchingRequestParameterName;
}
return UriComponentsBuilder.newInstance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.security.web.util;

import org.springframework.util.StringUtils;

/**
* Internal utility for escaping characters in HTML strings.
*
Expand All @@ -25,7 +27,7 @@
public abstract class TextEscapeUtils {

public static String escapeEntities(String s) {
if (s == null || s.length() == 0) {
if (!StringUtils.hasLength(s)) {
return s;
}
StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void pathParametersAreRemovedFromPathInfo() {
String path = entry.getKey();
String expectedResult = entry.getValue();
// Should be null when stripped value is empty
if (expectedResult.length() == 0) {
if (expectedResult.isEmpty()) {
expectedResult = null;
}
request.setPathInfo(path);
Expand Down