Skip to main content
Mejora de formato.
Origen Enlace
Ruslan López
  • 11.8k
  • 14
  • 44
  • 88

@Override public UserDetails loadUserByUsername(String usuarionombre) throws UsernameNotFoundException {

@Override
public UserDetails loadUserByUsername(String usuarionombre) throws UsernameNotFoundException {
    
    Usuario usuario=usuarioRepository.findByUsuarionombre(usuarionombre);
    
    UserBuilder  builder=null;
    
    if(usuario!=null){
        
        builder=org.springframework.security.core.userdetails.User.withUsername(usuario.getUsuarioNombre());
        builder.password(new BCryptPasswordEncoder().encode(usuario.getClave()));
        builder.roles(usuario.getRol().getNombre());
        //builder.authorities(getGrantedAuthorities((Collection<Rol>) usuario));
        
    }else{
        throw new UsernameNotFoundException("Nombre Usuario" + usuarionombre+" No encontrado ");
    }
    
    return builder.build();
    
}

@Configuration @EnableWebSecurity public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter{Configuración de seguridad:

@Configuration
@EnableWebSecurity
public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter{
    
    @Autowired
    private AuthenticationEntryPoint entryPoint;
    
    
    
    private Usuario usuario;
    

     @Bean
      public UserDetailsService userDetailsService() {
        return new MyUsuarioDetalleService();
      };
    
      @Bean
      public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
      };
      
    @Override
    protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
        auth
            .userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
        
        
    
    }

@Override protected void configure(final HttpSecurity http) throws Exception { final CustomFilter tokenFilter = new CustomFilter(); http.addFilterBefore(tokenFilter, BasicAuthenticationFilter.class); http
.authorizeRequests() .antMatchers("/**").hasRole("admin") .anyRequest().hasRole("admin")


@Override
    protected void configure(final HttpSecurity http) throws Exception {
        final CustomFilter tokenFilter = new CustomFilter();
        http.addFilterBefore(tokenFilter, BasicAuthenticationFilter.class);
        http            
            .authorizeRequests()
            .antMatchers("/**").hasRole("admin")
            .anyRequest().hasRole("admin")
            
            .and()
            .httpBasic()
            .authenticationEntryPoint(entryPoint);
        
        http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class);
        
    }
    

    
    
}

}

@Override public UserDetails loadUserByUsername(String usuarionombre) throws UsernameNotFoundException {

    Usuario usuario=usuarioRepository.findByUsuarionombre(usuarionombre);
    
    UserBuilder  builder=null;
    
    if(usuario!=null){
        
        builder=org.springframework.security.core.userdetails.User.withUsername(usuario.getUsuarioNombre());
        builder.password(new BCryptPasswordEncoder().encode(usuario.getClave()));
        builder.roles(usuario.getRol().getNombre());
        //builder.authorities(getGrantedAuthorities((Collection<Rol>) usuario));
        
    }else{
        throw new UsernameNotFoundException("Nombre Usuario" + usuarionombre+" No encontrado ");
    }
    
    return builder.build();
    
}

@Configuration @EnableWebSecurity public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter{

@Autowired
private AuthenticationEntryPoint entryPoint;



private Usuario usuario;


 @Bean
  public UserDetailsService userDetailsService() {
    return new MyUsuarioDetalleService();
  };

  @Bean
  public BCryptPasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
  };
  
@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
    auth
        .userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
    
    

}

@Override protected void configure(final HttpSecurity http) throws Exception { final CustomFilter tokenFilter = new CustomFilter(); http.addFilterBefore(tokenFilter, BasicAuthenticationFilter.class); http
.authorizeRequests() .antMatchers("/**").hasRole("admin") .anyRequest().hasRole("admin")

        .and()
        .httpBasic()
        .authenticationEntryPoint(entryPoint);
    
    http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class);
    
}

}

@Override
public UserDetails loadUserByUsername(String usuarionombre) throws UsernameNotFoundException {
    
    Usuario usuario=usuarioRepository.findByUsuarionombre(usuarionombre);
    
    UserBuilder  builder=null;
    
    if(usuario!=null){
        
        builder=org.springframework.security.core.userdetails.User.withUsername(usuario.getUsuarioNombre());
        builder.password(new BCryptPasswordEncoder().encode(usuario.getClave()));
        builder.roles(usuario.getRol().getNombre());
        //builder.authorities(getGrantedAuthorities((Collection<Rol>) usuario));
        
    }else{
        throw new UsernameNotFoundException("Nombre Usuario" + usuarionombre+" No encontrado ");
    }
    
    return builder.build();
    
}

Configuración de seguridad:

@Configuration
@EnableWebSecurity
public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter{
    
    @Autowired
    private AuthenticationEntryPoint entryPoint;
    
    
    
    private Usuario usuario;
    

     @Bean
      public UserDetailsService userDetailsService() {
        return new MyUsuarioDetalleService();
      };
    
      @Bean
      public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
      };
      
    @Override
    protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
        auth
            .userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
        
        
    
    }

@Override
    protected void configure(final HttpSecurity http) throws Exception {
        final CustomFilter tokenFilter = new CustomFilter();
        http.addFilterBefore(tokenFilter, BasicAuthenticationFilter.class);
        http            
            .authorizeRequests()
            .antMatchers("/**").hasRole("admin")
            .anyRequest().hasRole("admin")
            
            .and()
            .httpBasic()
            .authenticationEntryPoint(entryPoint);
        
        http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class);
        
    }
    

    
    
}
Origen Enlace
Libra2880
  • 437
  • 2
  • 8
  • 21

La solución esta en la siguientes lineas código

@Override public UserDetails loadUserByUsername(String usuarionombre) throws UsernameNotFoundException {

    Usuario usuario=usuarioRepository.findByUsuarionombre(usuarionombre);
    
    UserBuilder  builder=null;
    
    if(usuario!=null){
        
        builder=org.springframework.security.core.userdetails.User.withUsername(usuario.getUsuarioNombre());
        builder.password(new BCryptPasswordEncoder().encode(usuario.getClave()));
        builder.roles(usuario.getRol().getNombre());
        //builder.authorities(getGrantedAuthorities((Collection<Rol>) usuario));
        
    }else{
        throw new UsernameNotFoundException("Nombre Usuario" + usuarionombre+" No encontrado ");
    }
    
    return builder.build();
    
}

@Configuration @EnableWebSecurity public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter{

@Autowired
private AuthenticationEntryPoint entryPoint;



private Usuario usuario;


 @Bean
  public UserDetailsService userDetailsService() {
    return new MyUsuarioDetalleService();
  };

  @Bean
  public BCryptPasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
  };
  
@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
    auth
        .userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
    
    

}

@Override protected void configure(final HttpSecurity http) throws Exception { final CustomFilter tokenFilter = new CustomFilter(); http.addFilterBefore(tokenFilter, BasicAuthenticationFilter.class); http
.authorizeRequests() .antMatchers("/**").hasRole("admin") .anyRequest().hasRole("admin")

        .and()
        .httpBasic()
        .authenticationEntryPoint(entryPoint);
    
    http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class);
    
}

}