Servlet filter which detects if the authenticated user is a service account My concern with this code is that I am forcing execution repeatedly through the same pathway for the sake of a readability that I'm not convinced by. 

It's a servlet filter which detects if the authenticated user is a service account (non-human) and then does some checks on them within the if clause.

import org.springframework.security.core.Authentication;    
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;

public class ServiceAccountFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain)
            throws IOException, ServletException {
        if (request instanceof HttpServletRequest
            &amp;&amp; ((HttpServletRequest) request).getUserPrincipal() != null
            &amp;&amp; ((HttpServletRequest) request).getUserPrincipal()
                    instanceof Authentication
            &amp;&amp; ((Authentication)
                ((HttpServletRequest) request).getUserPrincipal())
                .getPrincipal() != null
            &amp;&amp; ((Authentication)
                ((HttpServletRequest) request).getUserPrincipal())
                .getPrincipal() instanceof MyUser
            &amp;&amp; ((MyUser) ((Authentication)
                ((HttpServletRequest) request).getUserPrincipal())
                .getPrincipal()).getUsername() != null
            &amp;&amp; ((MyUser) ((Authentication)
                ((HttpServletRequest) request).getUserPrincipal())
                .getPrincipal()).getUsername().length() &gt; 6) {
            MyUser myUser = (MyUser) ((Authentication)
                    ((HttpServletRequest) request).getUserPrincipal())
                    .getPrincipal();
            // app-specific tests
        }
        chain.doFilter(request, response);
    }


Would it be better to instantiate all of these objects and create a deeply nested set of simplified if clauses?

My opinion is that it might be marginally more performant but it wouldn't be any more readable - or am I already blind to how unreadable it is?
One thing that is sure is that it will be more readable to move those tests in one method : 

public void doFilter(ServletRequest request,
                     ServletResponse response,
                     FilterChain chain)
        throws IOException, ServletException {
    if ( isAuthenticated(request) ) {
        MyUser myUser = getAuthenticatedUser(request);
        // app-specific tests
    }
}


Then it depends but most of the time your code is more readable when extracting intermediate steps in variables or methods.

private boolean isAuthenticated(ServletRequest request) {
    return (request instanceof HttpServletRequest) &amp;&amp;
           hasValidPrincipal((HttpServletRequest) request);
} 

private boolean hasValidPrincipal(HttpServletRequest request) {
    MyUser user = getAuthenticatedUser(request);
    return user!=null &amp;&amp; user.getUsername().length()&gt;6;
}

private MyUser getAuthenticatedUser(HttpServletRequest request) {
    Object principal = request.getUserPrincipal();
    Authentication authentication = null;
    if ( !(principal instanceof Authentication) ) {
        return null;
    }
    authentication = (Authentication) request.getUserPrincipal();
    Object user = authentication.getPrincipal();

    return (user instanceof MyUser)
        ?(MyUser) user
        :null;
    }
}