@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);
}
}
}