File tree Expand file tree Collapse file tree 4 files changed +10
-1
lines changed
src/main/java/com/estudo/springsecurity Expand file tree Collapse file tree 4 files changed +10
-1
lines changed Original file line number Diff line number Diff line change 66import org .springframework .http .HttpMethod ;
77import org .springframework .security .authentication .AuthenticationManager ;
88import org .springframework .security .config .annotation .authentication .configuration .AuthenticationConfiguration ;
9+ import org .springframework .security .config .annotation .method .configuration .EnableMethodSecurity ;
910import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
1011import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
1112import org .springframework .security .config .http .SessionCreationPolicy ;
1617
1718@ Configuration
1819@ EnableWebSecurity // Habilita para essa classe as configuraçoes do Spring Security
20+ @ EnableMethodSecurity (securedEnabled = true ) // Habilita a segurança baseada em anotações @PreAuthorize e @Secured
1921public class SecurityConfig {
2022
2123 @ SuppressWarnings ("unused" )
@@ -36,7 +38,6 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
3638 .authorizeHttpRequests (authorize -> authorize
3739 .requestMatchers (HttpMethod .POST , "/auth/register" ).hasRole ("ADMIN" )
3840 .requestMatchers (HttpMethod .POST , "/auth/login" ).permitAll ()
39- .requestMatchers (HttpMethod .GET , "/user" ).hasRole ("ADMIN" )
4041 // So deve ser descomentado para cadastro do primeiro usuário ADMIN no sistema.
4142 // .requestMatchers(HttpMethod.POST, "/auth/register").permitAll()
4243 .anyRequest ().authenticated ())
Original file line number Diff line number Diff line change @@ -59,6 +59,7 @@ public AuthResponseDTO login(LoginRequestDTO loginRequestDTO) {
5959 return new AuthResponseDTO (user .getName (), token , "Bem Vindo de Volta" );
6060 }
6161
62+ // Apenas ADMIN pode registrar novos usuários configurando no SecurityConfig
6263 public AuthResponseDTO register (UserDTO userDTO ) {
6364 Optional <User > user = userRepository .findByEmail (userDTO .getEmail ());
6465
Original file line number Diff line number Diff line change 33import org .springframework .beans .factory .annotation .Autowired ;
44import org .springframework .data .domain .Page ;
55import org .springframework .data .domain .Pageable ;
6+ import org .springframework .security .access .annotation .Secured ;
67import org .springframework .stereotype .Service ;
78
89import com .estudo .springsecurity .dtos .ProductDTO ;
@@ -16,12 +17,14 @@ public class ProductService {
1617 @ Autowired
1718 private ProductRepository productRepository ;
1819
20+ @ Secured ({ "ROLE_ADMIN" , "ROLE_USER" })
1921 public Page <ProductDTO > getAll (Pageable pageable ) {
2022 Page <Product > result = productRepository .findAll (pageable );
2123 Page <ProductDTO > dto = result .map (ProductDTO ::new );
2224 return dto ;
2325 }
2426
27+ @ Secured ({ "ROLE_ADMIN" , "ROLE_USER" })
2528 public ProductDTO getOne (Long id ) {
2629 Product result = productRepository .findById (id )
2730 .orElseThrow (() -> new EntityNotFoundException ("Produto com id: " + id + " não encontrado." ));
@@ -30,6 +33,7 @@ public ProductDTO getOne(Long id) {
3033 return dto ;
3134 }
3235
36+ @ Secured ({ "ROLE_ADMIN" , "ROLE_USER" })
3337 public ProductDTO createProduct (ProductDTO productDTO ) {
3438 if (productDTO .getName ().isBlank () || productDTO .getName () == null ) {
3539 throw new IllegalArgumentException ("Nome do produto não pode ser vazio ou 'null': " + productDTO .getName ());
Original file line number Diff line number Diff line change 33import org .springframework .beans .factory .annotation .Autowired ;
44import org .springframework .data .domain .Page ;
55import org .springframework .data .domain .Pageable ;
6+ import org .springframework .security .access .prepost .PreAuthorize ;
67import org .springframework .stereotype .Service ;
78import org .springframework .transaction .annotation .Transactional ;
89
@@ -16,13 +17,15 @@ public class UserService {
1617 private UserRepository userRepository ;
1718
1819 @ Transactional (readOnly = true )
20+ @ PreAuthorize ("hasRole('ADMIN')" )
1921 public Page <UserDTO > getAll (Pageable pageable ) {
2022 Page <User > result = userRepository .findAll (pageable );
2123 Page <UserDTO > dto = result .map (UserDTO ::new );
2224 return dto ;
2325 }
2426
2527 @ Transactional (readOnly = true )
28+ @ PreAuthorize ("hasRole('ADMIN')" )
2629 public UserDTO getOne (String id ) {
2730 User result = userRepository .findById (id )
2831 .orElseThrow (() -> new RuntimeException ("Usuario com id: " + id + " não encontrado" ));
You can’t perform that action at this time.
0 commit comments