// Path: src/main/java/org/andidev/applicationname/config/logging/MDC.java
// public class MDC {
// 
//     public static void putSession(String session) {
//         org.slf4j.MDC.put("session", left(session, 9));
//     }
// 
//     public static void removeSession() {
//         org.slf4j.MDC.remove("session");
//     }
// 
//     public static void putUsername(String username) {
//         org.slf4j.MDC.put("username", username);
//     }
// 
//     public static void removeUsername() {
//         org.slf4j.MDC.remove("username");
//     }
// }
// 
// Path: src/main/java/org/andidev/applicationname/util/ApplicationUtils.java
// public static HttpSession getSession() {
//     return getRequest().getSession();
// }
// 
// Path: src/main/java/org/andidev/applicationname/util/ApplicationUtils.java
// public static String getUsername() {
//     User user = getUser();
//     if (user == null) {
//         return null;
//     }
// 
//     return user.getUsername();
// }


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.andidev.applicationname.config.logging.MDC;
import static org.andidev.applicationname.util.ApplicationUtils.getSession;
import static org.andidev.applicationname.util.ApplicationUtils.getUsername;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.AuthenticationDetailsSource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.filter.GenericFilterBean;
                logger.debug("Populated SecurityContextHolder with anonymous token: '"
                        + SecurityContextHolder.getContext().getAuthentication() + "'");
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("SecurityContextHolder not populated with anonymous token, as it already contained: '"
                        + SecurityContextHolder.getContext().getAuthentication() + "'");
            }
        }

        chain.doFilter(req, res);
    }

    @Transactional(readOnly = true)
    protected Authentication createAuthentication(HttpServletRequest request) {
        // Get principal and authorities from UserDetailSertvice
        if (principal == null) {
            UserDetails userDetails = userDetailsService.loadUserByUsername(username);
            principal = userDetails;
            authorities = new ArrayList(userDetails.getAuthorities());
        }

        AnonymousAuthenticationToken auth = new AnonymousAuthenticationToken(key, principal, authorities);
        auth.setDetails(authenticationDetailsSource.buildDetails(request));

        return auth;
    }

    public void setAuthenticationDetailsSource(AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource) {
        Assert.notNull(authenticationDetailsSource, "AuthenticationDetailsSource required");
// Path: src/main/java/org/andidev/applicationname/config/logging/MDC.java
// public class MDC {
// 
//     public static void putSession(String session) {
//         org.slf4j.MDC.put("session", left(session, 9));
//     }
// 
//     public static void removeSession() {
//         org.slf4j.MDC.remove("session");
//     }
// 
//     public static void putUsername(String username) {
//         org.slf4j.MDC.put("username", username);
//     }
// 
//     public static void removeUsername() {
//         org.slf4j.MDC.remove("username");
//     }
// }
// 
// Path: src/main/java/org/andidev/applicationname/util/ApplicationUtils.java
// public static HttpSession getSession() {
//     return getRequest().getSession();
// }
// 
// Path: src/main/java/org/andidev/applicationname/util/ApplicationUtils.java
// public static String getUsername() {
//     User user = getUser();
//     if (user == null) {
//         return null;
//     }
// 
//     return user.getUsername();
// }


// Path: src/main/java/org/andidev/applicationname/config/springsecurity/UserDetailsServiceAnonymousAuthenticationFilter.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.andidev.applicationname.config.logging.MDC;
import static org.andidev.applicationname.util.ApplicationUtils.getSession;
import static org.andidev.applicationname.util.ApplicationUtils.getUsername;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.AuthenticationDetailsSource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.filter.GenericFilterBean;

                logger.debug("Populated SecurityContextHolder with anonymous token: '"
                        + SecurityContextHolder.getContext().getAuthentication() + "'");
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("SecurityContextHolder not populated with anonymous token, as it already contained: '"
                        + SecurityContextHolder.getContext().getAuthentication() + "'");
            }
        }

        chain.doFilter(req, res);
    }

    @Transactional(readOnly = true)
    protected Authentication createAuthentication(HttpServletRequest request) {
        // Get principal and authorities from UserDetailSertvice
        if (principal == null) {
            UserDetails userDetails = userDetailsService.loadUserByUsername(username);
            principal = userDetails;
            authorities = new ArrayList(userDetails.getAuthorities());
        }

        AnonymousAuthenticationToken auth = new AnonymousAuthenticationToken(key, principal, authorities);
        auth.setDetails(authenticationDetailsSource.buildDetails(request));

        return auth;
    }

    public void setAuthenticationDetailsSource(AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource) {
        Assert.notNull(authenticationDetailsSource, "AuthenticationDetailsSource required");
        this.authenticationDetailsSource = authenticationDetailsSource;