Polish spring-security-remoting main code

Manually polish `spring-security-remoting` following the formatting
and checkstyle fixes.

Issue gh-8945
This commit is contained in:
Phillip Webb 2020-07-31 22:36:11 -07:00 committed by Rob Winch
parent 5924ed885b
commit 16819be437
2 changed files with 4 additions and 18 deletions

View File

@ -98,7 +98,6 @@ public class JndiDnsResolver implements DnsResolver {
// (highest number)
int highestPriority = -1;
int highestWeight = -1;
for (NamingEnumeration<?> recordEnum = dnsRecord.getAll(); recordEnum.hasMoreElements();) {
String[] record = recordEnum.next().toString().split(" ");
if (record.length != 4) {
@ -123,7 +122,6 @@ public class JndiDnsResolver implements DnsResolver {
catch (NamingException ex) {
throw new DnsLookupException("DNS lookup failed for service " + serviceType + " at " + domain, ex);
}
// remove the "." at the end
if (result.endsWith(".")) {
result = result.substring(0, result.length() - 1);
@ -134,7 +132,6 @@ public class JndiDnsResolver implements DnsResolver {
private Attribute lookup(String query, DirContext ictx, String recordType) {
try {
Attributes dnsResult = ictx.getAttributes(query, new String[] { recordType });
return dnsResult.get(recordType);
}
catch (NamingException ex) {
@ -152,14 +149,12 @@ public class JndiDnsResolver implements DnsResolver {
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
env.put(Context.PROVIDER_URL, "dns:"); // This is needed for IBM JDK/JRE
InitialDirContext ictx;
try {
ictx = new InitialDirContext(env);
return new InitialDirContext(env);
}
catch (NamingException ex) {
throw new DnsLookupException("Cannot create InitialDirContext for DNS lookup", ex);
}
return ictx;
}
}

View File

@ -22,6 +22,7 @@ import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.log.LogMessage;
import org.springframework.remoting.support.RemoteInvocation;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
@ -61,7 +62,6 @@ public class ContextPropagatingRemoteInvocation extends RemoteInvocation {
public ContextPropagatingRemoteInvocation(MethodInvocation methodInvocation) {
super(methodInvocation);
Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
if (currentUser != null) {
this.principal = currentUser.getName();
Object userCredentials = currentUser.getCredentials();
@ -71,7 +71,6 @@ public class ContextPropagatingRemoteInvocation extends RemoteInvocation {
this.credentials = null;
this.principal = null;
}
if (logger.isDebugEnabled()) {
logger.debug("RemoteInvocation now has principal: " + this.principal);
if (this.credentials == null) {
@ -95,26 +94,18 @@ public class ContextPropagatingRemoteInvocation extends RemoteInvocation {
@Override
public Object invoke(Object targetObject)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
if (this.principal != null) {
Authentication request = createAuthenticationRequest(this.principal, this.credentials);
request.setAuthenticated(false);
SecurityContextHolder.getContext().setAuthentication(request);
if (logger.isDebugEnabled()) {
logger.debug("Set SecurityContextHolder to contain: " + request);
}
logger.debug(LogMessage.format("Set SecurityContextHolder to contain: %s", request));
}
try {
return super.invoke(targetObject);
}
finally {
SecurityContextHolder.clearContext();
if (logger.isDebugEnabled()) {
logger.debug("Cleared SecurityContextHolder.");
}
logger.debug("Cleared SecurityContextHolder.");
}
}