public static Function<X509CertificateAuthentication, String> patternExtractor(final String key,
            final Function<? super X509CertificateAuthentication, String> fallback) {
        requireNonNull(key, "key");
        requireNonNull(fallback, "fallback");
        final Pattern pattern = Pattern.compile(key + "=(.+?)(?:,|$)", Pattern.CASE_INSENSITIVE);
        return authentication -> {
            final Object principal = authentication.getPrincipal();
            if (principal instanceof X500Principal) {
                final X500Principal x500Principal = (X500Principal) principal;
                final Matcher matcher = pattern.matcher(x500Principal.getName());
                if (matcher.find()) {
                    return matcher.group(1);
                }
            }
            return fallback.apply(authentication);
        };
    }
Creates a new case-insensitive pattern extractor with the given pattern.

@param key The case insensitive key to use (Example: 'CN').
@param fallback The fallback function to use if the key was not present in the subject.
@return The newly created extractor.
grpc-server-spring-boot-starter/src/main/java/net/devh/boot/grpc/server/security/authentication/X509CertificateAuthenticationProvider.java