Ich habe zwei Zugriffsebenen in der Anwendung: für alle und nur für autorisierte.Authentifizierung fehlgeschlagen: Nein AuthenticationProvider
Ich bin Login als registrierter Benutzer, aber wenn ich versuche, Daten anzufordern, die geschützt Ich erhalte eine Fehlermeldung:
Authentication Failed: No AuthenticationProvider found for com.company.security.tokenAuth.TokenAuthentication
Meine TokenAuthentication
Klasse:
public class TokenAuthentication extends AbstractAuthenticationToken {
private static final long serialVersionUID = -4021530026682433724L;
private UserDetails principal;
private String token;
public TokenAuthentication(String token) {
super(new HashSet<>());
this.token = token;
}
public TokenAuthentication(String token, Collection<? extends GrantedAuthority> authorities,
boolean isAuthenticated, UserDetails principal) {
super(authorities);
this.principal = principal;
this.setAuthenticated(isAuthenticated);
}
@Override
public Object getCredentials() {
return null;
}
@Override
public UserDetails getPrincipal() {
return principal;
}
public String getToken() {
return token;
}
}
Meine TokenAuthenticationProvider
Klasse :
@Component
public class TokenAuthenticationProvider implements AuthenticationProvider {
private TokenService tokenService;
private AccountDetailsService accountService;
public TokenAuthenticationProvider(TokenService tokenService, AccountDetailsService accountService) {
this.tokenService = tokenService;
this.accountService = accountService;
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
if (authentication instanceof TokenAuthentication) {
return processAuthentication((TokenAuthentication) authentication);
} else {
authentication.setAuthenticated(false);
return authentication;
}
}
@Override
public boolean supports(Class<?> aClass) {
return aClass.equals(TokenAuthentication.class);
}
private TokenAuthentication processAuthentication(TokenAuthentication authentication) {
try {
Account token = tokenService.parseToken(authentication.getToken());
Set<GrantedAuthority> authorities = new HashSet<>();
authorities.add(new SimpleGrantedAuthority(token.getRole().name()));
return new TokenAuthentication(authentication.getToken(), authorities,
true, new AccountDetails((Account) accountService.loadUserByUsername(token.getEmail())));
} catch (ValidationException e) {
throw new AuthenticationServiceException("Invalid token");
} catch (Exception e) {
throw new AuthenticationServiceException("Token corrupted");
}
}
}
Was ist mein Problem? Vielen Dank für Ihre Hilfe.
Vielleicht ein Problem mit meiner Konfiguration. Denn wenn Sie den Code ausführen und den Swagger überprüfen, funktioniert es. – Garazd