Professor, estou desenvolvendo um projeto, nele vou ter duas roles, usuario padrao que podera somente visualizar tarefas direcionadas a ele, e o admin porém estou com problemas ao fazer securityFilterChain coloquei essa linha, mas não sei sé é exatamente com isso, ou tenho que configurar no Cors tbm.
Minha classe Configurations:
package com.tarefas.Tarefas.security;
@Configuration
@EnableWebSecurity
public class Configurations {
@Autowired
private SecurityFilter securityFilter;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
System.out.println("Entrou no configurations");
return httpSecurity
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(authorize -> authorize
.requestMatchers(HttpMethod.POST,"/auth/register").hasRole("ADMIN")
.requestMatchers(HttpMethod.POST,"/auth/login").permitAll()
.requestMatchers("/clientes").hasRole("ADMIN")
.requestMatchers("/funcionarios").hasRole("ADMIN")
.requestMatchers("/tarefas").permitAll()
.anyRequest().authenticated()
)
.csrf(csrf -> csrf.disable())
.cors(Customizer.withDefaults())
.addFilterBefore(securityFilter, UsernamePasswordAuthenticationFilter.class)
.build();
}
@Bean
public AuthenticationManager authenticationManager
(AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("*"));
configuration.setAllowedMethods(Arrays.asList("*"));
configuration.setAllowedHeaders(Arrays.asList("*"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
Minha classe usuario:
package com.tarefas.Tarefas.domain.usuario;
@Entity(name = "Usuario")
@Table(name = "Usuario")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Usuario implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id_usuario;
@Column(name = "nome")
private String nome;
@Column(name = "login")
private String login;
@Column(name = "senha")
private String senha;
@Column(name = "role")
private UsuarioRole role;
private Usuario(DadosUsuario dados){
this.nome = dados.nome();
this.login = dados.login();
this.senha = dados.senha();
this.role = dados.role();
}
private Usuario(DadosLogin login){
this.login = login.login();
this.senha = login.senha();
}
public Usuario(String nome, String login, String encryptedPassword, UsuarioRole role) {
this.nome = nome;
this.login = login;
this.senha = encryptedPassword;
this.role = role;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
if(this.role == UsuarioRole.ADMIN) return List.of(new SimpleGrantedAuthority("ROLE_ADMIN"), new SimpleGrantedAuthority("ROLE_USER"));
if(this.role == UsuarioRole.USER) return List.of( new SimpleGrantedAuthority("ROLE_USER"));
else return List.of(new SimpleGrantedAuthority("ROLE_USER"));
}
@Override
public String getPassword() {
return senha;
}
@Override
public String getUsername() {
return login;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}