Changeset 84d0fbb for trip-planner/src/main/java/finki/diplomska
- Timestamp:
- 12/19/21 19:39:00 (3 years ago)
- Branches:
- master
- Children:
- bdd6491
- Parents:
- 1ad8e64
- Location:
- trip-planner/src/main/java/finki/diplomska/tripplanner
- Files:
-
- 6 added
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
trip-planner/src/main/java/finki/diplomska/tripplanner/TripPlannerApplication.java
r1ad8e64 r84d0fbb 6 6 import org.springframework.context.annotation.Bean; 7 7 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 8 import org.springframework.web.cors.CorsConfiguration; 9 import org.springframework.web.cors.CorsConfigurationSource; 10 import org.springframework.web.cors.UrlBasedCorsConfigurationSource; 11 12 import java.util.Arrays; 8 13 9 14 @SpringBootApplication … … 15 20 return new BCryptPasswordEncoder(); 16 21 } 22 23 17 24 public static void main(String[] args) { 18 25 SpringApplication.run(TripPlannerApplication.class, args); -
trip-planner/src/main/java/finki/diplomska/tripplanner/models/Planner.java
r1ad8e64 r84d0fbb 1 1 package finki.diplomska.tripplanner.models; 2 2 3 import com.fasterxml.jackson.annotation.JsonIgnore; 3 4 import lombok.*; 4 import reactor.util.annotation.Nullable;5 5 6 6 import javax.persistence.*; … … 10 10 @Data 11 11 @Table(name = "planners") 12 @NoArgsConstructor13 12 @AllArgsConstructor 14 13 @Getter … … 34 33 private List<Location> locationList; 35 34 35 @ManyToOne(fetch = FetchType.EAGER) 36 private User user; 36 37 37 public Planner(String name, String description, List<Location> locationList){ 38 public Planner(){ 39 } 40 41 public Planner(String name, String description, List<Location> locationList, User user){ 38 42 this.name = name; 39 43 this.description = description; 40 44 this.locationList = locationList; 45 this.user = user; 41 46 } 42 47 -
trip-planner/src/main/java/finki/diplomska/tripplanner/models/User.java
r1ad8e64 r84d0fbb 11 11 import javax.validation.constraints.Email; 12 12 import javax.validation.constraints.NotBlank; 13 import java.util.ArrayList; 13 14 import java.util.Collection; 14 15 import java.util.Date; 16 import java.util.List; 15 17 16 18 @Entity … … 38 40 private Date update_At; 39 41 40 //OneToMany with Project 42 //OneToMany with Planners 43 @OneToMany(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER, mappedBy = "user", orphanRemoval = true) 44 @JsonIgnore 45 private List<Planner> planners = new ArrayList<>(); 41 46 42 47 public User() { 43 48 } 44 49 50 public User(String username, String fullName, String password, String confirmPassword) { 51 this.username = username; 52 this.fullName = fullName; 53 this.password = password; 54 this.confirmPassword = confirmPassword; 55 } 45 56 46 57 @PrePersist -
trip-planner/src/main/java/finki/diplomska/tripplanner/models/dto/PlannerDto.java
r1ad8e64 r84d0fbb 9 9 private String name; 10 10 private String description; 11 private String user; 11 12 12 public PlannerDto(String name, String description ) {13 public PlannerDto(String name, String description, String user) { 13 14 this.name = name; 14 15 this.description = description; 16 this.user = user; 15 17 } 16 18 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/models/dto/UserDto.java
r1ad8e64 r84d0fbb 20 20 private Date update_At; 21 21 22 public UserDto(String username, String fullName, String password, String confirmPassword , Date create_At, Date update_At) {22 public UserDto(String username, String fullName, String password, String confirmPassword) { 23 23 this.username = username; 24 24 this.fullName = fullName; 25 25 this.password = password; 26 26 this.confirmPassword = confirmPassword; 27 this.create_At = create_At; 28 this.update_At = update_At; 27 29 28 } 30 29 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/repository/jpa/JpaPlannerRepository.java
r1ad8e64 r84d0fbb 4 4 import finki.diplomska.tripplanner.models.Planner; 5 5 import org.springframework.data.jpa.repository.JpaRepository; 6 import org.springframework.data.jpa.repository.Query; 7 import org.springframework.data.repository.query.Param; 6 8 import org.springframework.stereotype.Repository; 7 9 … … 11 13 public interface JpaPlannerRepository extends JpaRepository<Planner, Long> { 12 14 13 15 @Query(value = "SELECT * FROM planners AS p left JOIN users AS u ON p.user_id = u.id WHERE u.username like :username", nativeQuery = true) 16 List<Planner> getPlannersByUser(@Param("username") String username ); 14 17 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/repository/jpa/JpaUserRepository.java
r1ad8e64 r84d0fbb 7 7 @Repository 8 8 public interface JpaUserRepository extends CrudRepository<User, Long> { 9 User findByUsername(String username); 10 User getById(Long id); 9 11 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/security/SecurityConfig.java
r1ad8e64 r84d0fbb 2 2 3 3 4 import finki.diplomska.tripplanner.service.impl.CustomUserDetailsServiceImpl; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.context.annotation.Bean; 5 7 import org.springframework.context.annotation.Configuration; 8 import org.springframework.security.authentication.AuthenticationManager; 9 import org.springframework.security.config.BeanIds; 10 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 6 11 import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 7 12 import org.springframework.security.config.annotation.web.builders.HttpSecurity; … … 9 14 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 10 15 import org.springframework.security.config.http.SessionCreationPolicy; 16 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 17 import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; 18 19 import static finki.diplomska.tripplanner.security.SecurityConstants.MARIADB_URL; 20 import static finki.diplomska.tripplanner.security.SecurityConstants.SIGN_UP_URLS; 11 21 12 22 @Configuration … … 21 31 @Autowired 22 32 private JwtAuthenticationEntryPoint unauthorizedHandler; 33 34 @Autowired 35 private CustomUserDetailsServiceImpl customUserDetailsService; 36 37 @Bean 38 public JwtAuthenticationFilter jwtAuthenticationFilter() {return new JwtAuthenticationFilter();} 39 40 41 @Autowired 42 private BCryptPasswordEncoder bCryptPasswordEncoder; 43 44 @Override 45 protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception { 46 authenticationManagerBuilder.userDetailsService(customUserDetailsService).passwordEncoder(bCryptPasswordEncoder); 47 } 48 49 @Override 50 @Bean(BeanIds.AUTHENTICATION_MANAGER) 51 protected AuthenticationManager authenticationManager() throws Exception { 52 return super.authenticationManager(); 53 } 23 54 24 55 @Override … … 43 74 "/**/*.js" 44 75 ).permitAll() 45 .antMatchers("/api/users/**").permitAll() 76 .antMatchers(SIGN_UP_URLS).permitAll() 77 .antMatchers(MARIADB_URL).permitAll() 46 78 .anyRequest().authenticated(); 79 http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); 80 47 81 } 48 82 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/service/PlannerService.java
r1ad8e64 r84d0fbb 11 11 12 12 Planner createPlannerWithRequestParams(String description, String name, List<Location> locationList); 13 Optional<Planner> editPlanner(Long id, PlannerDto plannerDto );13 Optional<Planner> editPlanner(Long id, PlannerDto plannerDto, String username); 14 14 List<Planner> getAllPlaners(); 15 List<Planner> getPlannersByUser(String username); 15 16 Optional<Planner> findById(Long id); 16 17 Planner editPlannerWithRequestParams(Long id, String description, String name, List<Location> locationList); 17 Optional<Planner> newPlanner(PlannerDto plannerDto );18 Optional<Planner> newPlanner(PlannerDto plannerDto, String username); 18 19 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/service/UserService.java
r1ad8e64 r84d0fbb 2 2 3 3 import finki.diplomska.tripplanner.models.User; 4 import finki.diplomska.tripplanner.models.dto.UserDto; 4 5 6 import java.util.Optional; 5 7 6 8 -
trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/PlannerServiceImpl.java
r1ad8e64 r84d0fbb 3 3 import finki.diplomska.tripplanner.models.Location; 4 4 import finki.diplomska.tripplanner.models.Planner; 5 import finki.diplomska.tripplanner.models.User; 5 6 import finki.diplomska.tripplanner.models.dto.PlannerDto; 6 7 import finki.diplomska.tripplanner.models.exceptions.LocationNotFoundException; … … 8 9 import finki.diplomska.tripplanner.repository.jpa.JpaLocationRepository; 9 10 import finki.diplomska.tripplanner.repository.jpa.JpaPlannerRepository; 10 import finki.diplomska.tripplanner. service.LocationService;11 import finki.diplomska.tripplanner.repository.jpa.JpaUserRepository; 11 12 import finki.diplomska.tripplanner.service.PlannerService; 12 13 import org.springframework.stereotype.Service; 13 import reactor.util.annotation.Nullable;14 14 15 import java .util.ArrayList;15 import javax.jws.soap.SOAPBinding; 16 16 import java.util.List; 17 17 import java.util.Optional; … … 22 22 private final JpaPlannerRepository plannerRepository; 23 23 private final JpaLocationRepository locationRepository; 24 private final JpaUserRepository userRepository; 24 25 25 public PlannerServiceImpl(JpaPlannerRepository plannerRepository, JpaLocationRepository locationRepository ) {26 public PlannerServiceImpl(JpaPlannerRepository plannerRepository, JpaLocationRepository locationRepository, JpaUserRepository userRepository) { 26 27 this.plannerRepository = plannerRepository; 27 28 this.locationRepository = locationRepository; 29 this.userRepository = userRepository; 28 30 } 29 31 … … 31 33 public List<Planner> getAllPlaners() { 32 34 return this.plannerRepository.findAll(); 35 } 36 37 @Override 38 public List<Planner> getPlannersByUser(String username) { 39 User user = this.userRepository.findByUsername(username); 40 return this.plannerRepository.getPlannersByUser(user.getUsername()); 33 41 } 34 42 … … 40 48 41 49 @Override 42 public Optional<Planner> newPlanner(PlannerDto plannerDto ) {50 public Optional<Planner> newPlanner(PlannerDto plannerDto, String username) { 43 51 /* 44 52 List<Location> locationList = new ArrayList<>(); … … 48 56 locationList.add(loc); 49 57 } 50 51 58 */ 52 return Optional.of(this.plannerRepository.save(new Planner(plannerDto.getName(), plannerDto.getDescription(), null))); 59 User user = this.userRepository.findByUsername(username); 60 plannerDto.setUser(user.getUsername()); 61 return Optional.of(this.plannerRepository.save(new Planner(plannerDto.getName(), plannerDto.getDescription(), null, user))); 53 62 } 54 63 … … 69 78 70 79 @Override 71 public Optional<Planner> editPlanner(Long id, PlannerDto plannerDto ) {80 public Optional<Planner> editPlanner(Long id, PlannerDto plannerDto, String username) { 72 81 Planner planner = this.plannerRepository.findById(id).orElseThrow(() -> new PlannerNotFoundException(id)); 82 User user = this.userRepository.findByUsername(username); 73 83 74 84 planner.setName(plannerDto.getName()); 75 85 planner.setDescription(plannerDto.getDescription()); 86 plannerDto.setUser(user.getUsername()); 87 planner.setUser(user); 76 88 /* 77 89 List<Location> locationList = new ArrayList<>(); -
trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/UserServiceImpl.java
r1ad8e64 r84d0fbb 9 9 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 10 10 import org.springframework.stereotype.Service; 11 12 import java.util.Optional; 11 13 12 14 … … 28 30 // We don't persist or show the confirmPassword 29 31 newUser.setConfirmPassword(""); 30 return this.userRepository.save(newUser);32 return this.userRepository.save(newUser); 31 33 }catch(Exception e){ 32 34 throw new UsernameAlreadyExistsException("Username '"+newUser.getUsername()+ "' already exists"); -
trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/PlannerRestController.java
r1ad8e64 r84d0fbb 3 3 import finki.diplomska.tripplanner.models.Location; 4 4 import finki.diplomska.tripplanner.models.Planner; 5 import finki.diplomska.tripplanner.models.User; 5 6 import finki.diplomska.tripplanner.models.dto.PlannerDto; 6 7 import finki.diplomska.tripplanner.service.LocationService; … … 9 10 import org.springframework.http.MediaType; 10 11 import org.springframework.http.ResponseEntity; 12 import org.springframework.security.core.Authentication; 11 13 import org.springframework.web.bind.annotation.*; 12 14 import org.springframework.web.bind.annotation.RestController; 13 15 16 import javax.validation.Valid; 14 17 import java.util.ArrayList; 15 18 import java.util.List; … … 17 20 18 21 @RestController 19 @CrossOrigin(origins = "http://localhost:4200" )22 @CrossOrigin(origins = "http://localhost:4200", exposedHeaders = "token") 20 23 @RequestMapping(value = "/api") 21 24 public class PlannerRestController { … … 31 34 public List<Planner> getAllPlanners(){ 32 35 return this.plannerService.getAllPlaners(); 36 } 37 38 39 @GetMapping(value = "/planners/user") 40 public List<Planner> getPlannersByUser(Authentication authentication){ 41 User user = (User) authentication.getPrincipal(); 42 return this.plannerService.getPlannersByUser(user.getUsername()); 33 43 } 34 44 … … 57 67 @PostMapping(value = "/planner/new", consumes= MediaType.APPLICATION_JSON_VALUE) 58 68 @ResponseStatus(HttpStatus.CREATED) 59 public ResponseEntity<Planner> newPlanner(@RequestBody PlannerDto plannerDto) { 60 return this.plannerService.newPlanner(plannerDto) 69 public ResponseEntity<?> newPlanner(@Valid @RequestBody PlannerDto plannerDto, Authentication authentication) { 70 71 User user = (User) authentication.getPrincipal(); 72 return this.plannerService.newPlanner(plannerDto, user.getUsername()) 61 73 .map(planner -> ResponseEntity.ok().body(planner)) 62 74 .orElseGet(() -> ResponseEntity.badRequest().build()); … … 65 77 66 78 @PutMapping(value ="edit/planner/{id}", consumes= MediaType.APPLICATION_JSON_VALUE) 67 public ResponseEntity<Planner> editPlanner(@PathVariable Long id, @RequestBody PlannerDto plannerDto){ 68 return this.plannerService.editPlanner(id, plannerDto) 79 public ResponseEntity<Planner> editPlanner(@PathVariable Long id, @Valid @RequestBody PlannerDto plannerDto, Authentication authentication){ 80 User user = (User) authentication.getPrincipal(); 81 return this.plannerService.editPlanner(id, plannerDto, user.getUsername()) 69 82 .map(planner -> ResponseEntity.ok().body(planner)) 70 83 .orElseGet(()-> ResponseEntity.badRequest().build()); -
trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/UserController.java
r1ad8e64 r84d0fbb 3 3 import finki.diplomska.tripplanner.models.User; 4 4 import finki.diplomska.tripplanner.models.dto.UserDto; 5 import finki.diplomska.tripplanner.payload.JWTLoginSucessReponse; 6 import finki.diplomska.tripplanner.payload.LoginRequest; 7 import finki.diplomska.tripplanner.security.JwtTokenProvider; 8 import finki.diplomska.tripplanner.security.SecurityConstants; 5 9 import finki.diplomska.tripplanner.service.UserService; 6 10 import finki.diplomska.tripplanner.service.impl.MapValidationErrorService; … … 9 13 import org.springframework.http.HttpStatus; 10 14 import org.springframework.http.ResponseEntity; 15 import org.springframework.security.authentication.AuthenticationManager; 16 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 17 import org.springframework.security.core.context.SecurityContextHolder; 11 18 import org.springframework.validation.BindingResult; 12 19 import org.springframework.web.bind.annotation.*; 13 20 import org.springframework.security.core.Authentication; 14 21 import javax.validation.Valid; 15 22 … … 28 35 private UserValidator userValidator; 29 36 30 @PostMapping("/register") 37 @Autowired 38 private JwtTokenProvider tokenProvider; 39 40 @Autowired 41 private AuthenticationManager authenticationManager; 42 43 @PostMapping(value = "/login") 44 public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest, BindingResult result){ 45 ResponseEntity<?> errorMap = mapValidationErrorService.MapValidationService(result); 46 if(errorMap != null) return errorMap; 47 48 Authentication authentication = authenticationManager.authenticate( 49 new UsernamePasswordAuthenticationToken( 50 loginRequest.getUsername(), 51 loginRequest.getPassword() 52 ) 53 ); 54 55 SecurityContextHolder.getContext().setAuthentication(authentication); 56 String jwt = SecurityConstants.TOKEN_PREFIX + tokenProvider.generateToken(authentication); 57 58 return ResponseEntity.ok(new JWTLoginSucessReponse(true, jwt)); 59 } 60 61 @PostMapping(value = "/register") 31 62 public ResponseEntity<?> registerUser(@Valid @RequestBody User user, BindingResult result){ 32 63 // Validate passwords match … … 38 69 39 70 return new ResponseEntity<User>(newUser, HttpStatus.CREATED); 71 40 72 } 41 73 }
Note:
See TracChangeset
for help on using the changeset viewer.