Controller:
package com.example.Name.Spring.Security.studies.controller;
import com.example.Name.Spring.Security.studies.entities.user.AuthenticationDTO; import com.example.Name.Spring.Security.studies.entities.user.User; import com.example.Name.Spring.Security.studies.entities.user.registerDTO; import com.example.Name.Spring.Security.studies.repository.UserRepository; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@RestController @RequestMapping("/auth") public class AuthenticationController { @Autowired private AuthenticationManager authenticationManager;
@Autowired
private UserRepository userRepository;
@PostMapping("/login")
public ResponseEntity login(@RequestBody @Valid AuthenticationDTO data){
var token = new UsernamePasswordAuthenticationToken(data.login(),data.password());
var authentication = this.authenticationManager.authenticate(token);
return ResponseEntity.ok().build();
}
@PostMapping("/register")
public ResponseEntity register(@RequestBody @Valid registerDTO data){
if(this.userRepository.findByLogin(data.login()) != null) return ResponseEntity.badRequest().build();
String encryptedPassword = new BCryptPasswordEncoder().encode(data.password());
User newUser = new User(data.login(), encryptedPassword, data.role());
this.userRepository.save(newUser);
return ResponseEntity.ok().build();
}
} SecurityConfiguration:
package com.example.Name.Spring.Security.studies.infra.security;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.SecurityFilterChain;
@Configuration @EnableWebSecurity public class SecurityConfiguration { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { return httpSecurity .csrf(csrf -> csrf.disable()) .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .authorizeHttpRequests(authorize -> authorize .requestMatchers(HttpMethod.POST, "/auth/login").permitAll() .requestMatchers(HttpMethod.POST,"/auth/register").permitAll() .requestMatchers(HttpMethod.POST,"/api/products").hasRole("ADMIN") .anyRequest().authenticated() ) .build(); } @Bean public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception { return authenticationConfiguration.getAuthenticationManager(); } @Bean public PasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } }