public static SecurityContext createSecurityContext(final Principal principal, final HttpServletRequest request) {
        val authorities = principal.getAttributes().keySet().stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
        val user = new User(principal.getId(), RandomUtils.generateSecureRandomId(), authorities);
        val authenticationToken = new PreAuthenticatedAuthenticationToken(user,
            new SecurityContextCredential(principal.getId()), authorities);
        authenticationToken.setAuthenticated(true);
        authenticationToken.setDetails(new PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails(request, authorities));
        val context = new SecurityContextImpl(authenticationToken);
        context.setAuthentication(authenticationToken);
        return context;
    }
Create security context.

@param principal the principal
@param request   the request
@return the security context
core/cas-server-core-util-api/src/main/java/org/apereo/cas/util/spring/SecurityContextUtils.java