net.optionfactory.spring.authentication.bearer.token.StaticBearerTokenAuthenticationProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of authentication-bearer-token Show documentation
Show all versions of authentication-bearer-token Show documentation
optionfactory-spring authentication-bearer-token
package net.optionfactory.spring.authentication.bearer.token;
import java.util.Collection;
import java.util.Map;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
/**
* Authenticates by matching the configured static token with the request Bearer
* token, granting the configured authorities on authentication success.
*/
public class StaticBearerTokenAuthenticationProvider implements AuthenticationProvider {
private final Map> tokenToAuthorities;
public StaticBearerTokenAuthenticationProvider(String token, Collection extends GrantedAuthority> authorities) {
this(Map.of(token, authorities));
}
public StaticBearerTokenAuthenticationProvider(Map> tokenToAuthorities) {
this.tokenToAuthorities = tokenToAuthorities;
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
final BearerTokenAuthentication bearer = (BearerTokenAuthentication) authentication;
if (!tokenToAuthorities.containsKey(bearer.getCredentials())) {
return null;
}
return bearer.makeAuthenticated(null, tokenToAuthorities.get(bearer.getCredentials()));
}
@Override
public boolean supports(Class> authentication) {
return BearerTokenAuthentication.class.isAssignableFrom(authentication);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy