![JAR search and dependency download from the Maven repository](/logo.png)
org.lognet.springboot.grpc.security.BasicAuthSchemeSelector Maven / Gradle / Ivy
package org.lognet.springboot.grpc.security;
import java.util.Base64;
import java.util.Optional;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
public class BasicAuthSchemeSelector implements AuthenticationSchemeSelector {
private static final String prefix = "Basic ";
@Override
public Optional getAuthScheme(CharSequence authorization) {
if (authorization.subSequence(0, prefix.length()).toString().equalsIgnoreCase(prefix)) {
String token =
new String(
Base64.getDecoder()
.decode(
authorization
.subSequence(prefix.length(), authorization.length())
.toString()));
int delim = token.indexOf(":");
if (delim == -1) {
throw new BadCredentialsException("Invalid basic authentication token");
}
return Optional.of(
new UsernamePasswordAuthenticationToken(
token.substring(0, delim), token.substring(delim + 1)));
}
return Optional.empty();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy