2017-09-20 3 views
1

Je crée mon propre fournisseur d'authentification personnalisé. En ce moment je vérifie juste un nom d'utilisateur statique et un mot de passe mais plus tard ceci sera remplacé par quelque chose plus avancé, ainsi pendant que je n'ai pas besoin d'un fournisseur de coutume dans ce cas cela ne m'aidera pas beaucoup code supplémentaire que je n'ai pas encore ajouté. Cela dit, c'est mon code dans son état brisé.La sécurité d'amorçage ne respecte pas les rôles avec AuthenticationProvider

Mon fournisseur d'authentification personnalisé:

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.security.authentication.AuthenticationProvider; 
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 
import org.springframework.security.core.Authentication; 
import org.springframework.security.core.AuthenticationException; 
import org.springframework.security.core.GrantedAuthority; 
import org.springframework.security.core.authority.SimpleGrantedAuthority; 
import org.springframework.stereotype.Component; 

import java.util.ArrayList; 
import java.util.List; 

@Component 
public class SatAuthenticationProvider implements AuthenticationProvider { 

    private static final Logger LOGGER = LoggerFactory.getLogger(SatAuthenticationProvider.class); 

    public SatAuthenticationProvider() { 
    LOGGER.info("*** CustomAuthenticationProvider created"); 
    } 

    @Override 
    public boolean supports(Class<? extends Object> authentication) { 
    return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication)); 
    } 

    @Override 
    public Authentication authenticate(Authentication authentication) throws AuthenticationException { 

    LOGGER.info("*** Creating authentication"); 
    if (authentication.getName().equals("test") && authentication.getCredentials().equals("test")) { 
     List<GrantedAuthority> grantedAuths = new ArrayList<>(); 
     grantedAuths.add(new SimpleGrantedAuthority("USER")); 
     grantedAuths.add(new SimpleGrantedAuthority("ADMIN")); 
     return new UsernamePasswordAuthenticationToken(authentication.getName(), authentication.getCredentials(), grantedAuths); 
    } else { 
     return null; 
    } 

    } 
} 

Voici ma configuration de sécurité qui consomme est:

import com.comcast.iot.das.auth.SatAuthenticationProvider; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) 
public class DeviceSecurityConfig extends WebSecurityConfigurerAdapter { 

    private static final Logger LOGGER = LoggerFactory.getLogger(SatAuthenticationProvider.class); 

    @Autowired 
    private SatAuthenticationProvider satAuthenticationProvider; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
    LOGGER.info("*** configuring http"); 
    http.authorizeRequests().anyRequest() 
     .hasRole("USER") 
     .and() 
     .httpBasic(); 
    } 

    @Autowired 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
    LOGGER.info("*** Setting builder"); 
    auth.authenticationProvider(this.satAuthenticationProvider); 
    } 
} 

Maintenant, quand je lance ce si je CURL pour frapper un point final sans utilisateur et mot de passe spécifié je reçois ce qui suit:

% curl http://localhost:8080/molecule 
{ 
    "timestamp" : 1505925047977, 
    "status" : 401, 
    "error" : "Unauthorized", 
    "message" : "Full authentication is required to access this resource", 
    "path" : "/molecule" 
} 

Si je spécifie le nom d'utilisateur correct et passe mot que je reçois le texte suivant:

% curl -u test:test http://localhost:8080/molecule 
{ 
    "timestamp" : 1505925033015, 
    "status" : 403, 
    "error" : "Forbidden", 
    "message" : "Access is denied", 
    "path" : "/molecule" 
} 

Enfin si je spécifie le mauvais nom d'utilisateur et mot de passe, je reçois le texte suivant:

% curl -u test:test2 http://localhost:8080/molecule 
{ 
    "timestamp" : 1505925199406, 
    "status" : 401, 
    "error" : "Unauthorized", 
    "message" : "No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken", 
    "path" : "/molecule" 
} 

Un dernier point, alors que je ne peux pas obtenir des rôles de travailler directement je peux l'obtenir pour tester si un utilisateur est authentifié du tout et l'utiliser pour donner la permission. Ce n'est pas une solution viable, car j'ai besoin de rôles, mais cela peut donner à quelqu'un qui essaie de répondre à cette question quelques conseils.

donc je peux changer la méthode de configuration de la classe DeviceSecurityConfig comme suit:

@Override 
    protected void configure(HttpSecurity http) throws Exception { 
    LOGGER.info("*** configuring http"); 
    http.authorizeRequests().anyRequest() 
     .authenticated() 
     .and() 
     .httpBasic(); 
    } 

Avec cette nouvelle version du code mes demandes papillotes semblent au moins le travail comme prévu (mais il n'y a aucun moyen d'ajouter des rôles bien sûr) :. Voici les résultats de la boucle avec le code que je viens de mentionner.

Aucun nom d'utilisateur et mot de passe:

% curl http://localhost:8080/molecule 
{ 
    "timestamp" : 1505925444957, 
    "status" : 401, 
    "error" : "Unauthorized", 
    "message" : "Full authentication is required to access this resource", 
    "path" : "/molecule" 
} 

mot de passe incorrect:

% curl -u test:test2 http://localhost:8080/molecule 
{ 
    "timestamp" : 1505925456018, 
    "status" : 401, 
    "error" : "Unauthorized", 
    "message" : "No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken", 
    "path" : "/molecule" 
} 

Le mot de passe et nom d'utilisateur en utilisant la commande suivante retourne maintenant la réponse complète forme le point final je pense habituellement (OMIS).

% curl -u test:test http://localhost:8080/molecule 

Répondre

2

autorités de rôle devraient être préfixées avec ROLE_:

grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER")); 
grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));