Changeset 19398ad for jobvista-backend/src/main/java/mk/ukim/finki
- Timestamp:
- 05/16/24 23:09:21 (6 months ago)
- Branches:
- main
- Children:
- 28b3398
- Parents:
- d8b6c91
- Location:
- jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend
- Files:
-
- 12 added
- 18 edited
Legend:
- Unmodified
- Added
- Removed
-
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/config/JwtAuthFilter.java
rd8b6c91 r19398ad 44 44 token.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); 45 45 46 // SecurityContext securityContext = SecurityContextHolder.createEmptyContext();47 // securityContext.setAuthentication(token);48 // SecurityContextHolder.setContext(securityContext);49 50 46 SecurityContextHolder.getContext().setAuthentication(token); 51 47 } -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/config/SecurityConfiguration.java
rd8b6c91 r19398ad 2 2 3 3 import lombok.RequiredArgsConstructor; 4 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.enumerations.Role;5 4 import org.springframework.context.annotation.Bean; 6 5 import org.springframework.context.annotation.Configuration; … … 25 24 26 25 private final JwtAuthFilter jwtAuthFilter; 27 //private final UserService userService;28 26 private final UserDetailsService userDetailsService; 29 27 … … 31 29 public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { 32 30 http.csrf(AbstractHttpConfigurer::disable) 33 .authorizeHttpRequests(request -> request.requestMatchers("/api/auth/**") 34 .permitAll() 35 .requestMatchers("/api/admin").hasAnyAuthority(Role.ROLE_ADMIN.name()) 36 .requestMatchers("/api/job-seeker").hasAnyAuthority(Role.ROLE_JOBSEEKER.name()) 37 .requestMatchers("/api/recruiter").hasAnyAuthority(Role.ROLE_RECRUITER.name()) 31 .authorizeHttpRequests(request -> request 32 .requestMatchers("/api/job-advertisements/all","/api/job-advertisements/view/**","/api/recruiter/info/**", "/api/auth/**").permitAll() 33 //.requestMatchers("/api/job-advertisements/**").hasAnyAuthority(Role.ROLE_RECRUITER.name()) 38 34 .anyRequest().authenticated()) 39 35 .sessionManagement(manager -> manager.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/controllers/AdminController.java
rd8b6c91 r19398ad 2 2 3 3 import lombok.RequiredArgsConstructor; 4 import org.springframework.http.ResponseEntity;5 import org.springframework.web.bind.annotation.GetMapping;6 4 import org.springframework.web.bind.annotation.RequestMapping; 7 5 import org.springframework.web.bind.annotation.RestController; … … 12 10 public class AdminController { 13 11 14 15 @GetMapping16 public ResponseEntity<String> sayHello() {17 return ResponseEntity.ok("Hi Admin");18 }19 12 } -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/controllers/AuthController.java
rd8b6c91 r19398ad 12 12 import org.springframework.web.bind.annotation.*; 13 13 14 @CrossOrigin(origins = "*")15 14 @RestController 16 15 @RequestMapping("/api/auth") 17 16 @AllArgsConstructor 17 @CrossOrigin(origins = "*") 18 18 public class AuthController { 19 19 -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/controllers/JobSeekerController.java
rd8b6c91 r19398ad 2 2 3 3 4 import lombok.RequiredArgsConstructor; 4 import lombok.AllArgsConstructor; 5 import org.springframework.web.bind.annotation.CrossOrigin; 5 6 import org.springframework.web.bind.annotation.RequestMapping; 6 7 import org.springframework.web.bind.annotation.RestController; … … 8 9 @RestController 9 10 @RequestMapping("/api/job-seeker") 10 @RequiredArgsConstructor 11 @AllArgsConstructor 12 @CrossOrigin(origins = "*") 11 13 public class JobSeekerController { 12 14 } -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/controllers/RecruiterController.java
rd8b6c91 r19398ad 1 1 package mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.controllers; 2 2 3 import lombok.RequiredArgsConstructor; 3 import lombok.AllArgsConstructor; 4 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.DTO.RecruiterDetailsDTO; 5 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.service.intef.RecruiterService; 6 import org.springframework.http.HttpStatus; 4 7 import org.springframework.http.ResponseEntity; 5 import org.springframework.web.bind.annotation.GetMapping; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RestController; 8 import org.springframework.web.bind.annotation.*; 8 9 9 10 @RestController 10 11 @RequestMapping("/api/recruiter") 11 @RequiredArgsConstructor 12 @AllArgsConstructor 13 @CrossOrigin(origins = "*") 12 14 public class RecruiterController { 13 15 14 @GetMapping 15 public ResponseEntity<String> sayHello() { 16 return ResponseEntity.ok("Hi Recruiter"); 16 private final RecruiterService recruiterService; 17 18 @GetMapping("/info/{id}") 19 public ResponseEntity<?> getRecruiterDetailsById(@PathVariable("id") Long id) { 20 RecruiterDetailsDTO recruiterDetailsDTO = recruiterService.getRecruiterDetailsById(id); 21 return new ResponseEntity<>(recruiterDetailsDTO, HttpStatus.OK); 17 22 } 18 23 } -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/models/users/Admin.java
rd8b6c91 r19398ad 4 4 import jakarta.persistence.Table; 5 5 import lombok.AllArgsConstructor; 6 import lombok.Builder;7 6 import lombok.Data; 8 7 import lombok.NoArgsConstructor; 9 8 10 9 @Entity 11 //@Data 12 //@NoArgsConstructor 13 //@AllArgsConstructor 14 //@Builder 10 @Data 11 @NoArgsConstructor 15 12 @Table(name = "admins") 16 13 public class Admin extends User { 14 15 // private String name; 16 // private String surname; 17 17 18 @Override 18 19 public String getName() { 19 20 return "Admin"; 20 21 } 21 // private String name;22 // private String surname;23 22 } -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/models/users/DTO/JwtAuthResponse.java
rd8b6c91 r19398ad 10 10 @NoArgsConstructor 11 11 public class JwtAuthResponse { 12 private Long id; 12 13 private String email; 13 14 private String name; -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/models/users/JobSeeker.java
rd8b6c91 r19398ad 5 5 import jakarta.persistence.Table; 6 6 import lombok.AllArgsConstructor; 7 import lombok.Builder;8 7 import lombok.Data; 9 8 import lombok.NoArgsConstructor; … … 14 13 @NoArgsConstructor 15 14 @AllArgsConstructor 16 @Builder17 15 @Table(name = "job_seekers") 18 16 public class JobSeeker extends User { -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/models/users/User.java
rd8b6c91 r19398ad 15 15 16 16 @Entity 17 @Inheritance(strategy = InheritanceType.JOINED)18 @Table(name = "users")19 17 @Data 20 18 @NoArgsConstructor 21 19 @AllArgsConstructor 20 @Inheritance(strategy = InheritanceType.JOINED) 21 @Table(name = "users") 22 22 public abstract class User implements UserDetails { 23 23 -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/models/users/mappers/JobSeekerMapper.java
rd8b6c91 r19398ad 26 26 } 27 27 28 29 28 } -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/models/users/mappers/RecruiterMapper.java
rd8b6c91 r19398ad 2 2 3 3 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.DTO.RecruiterDTO; 4 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.DTO.RecruiterDetailsDTO; 4 5 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.Recruiter; 5 6 … … 11 12 recruiter.getPassword(), 12 13 recruiter.getCompanyName(), 14 recruiter.getPhoneNumber() 15 ); 16 } 17 18 public static RecruiterDetailsDTO mapToRecruiterDetailsDTO(Recruiter recruiter) { 19 return new RecruiterDetailsDTO( 20 recruiter.getEmail(), 21 recruiter.getCompanyName(), 22 recruiter.getCompanyDescription(), 13 23 recruiter.getPhoneNumber() 14 24 ); -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/repositories/RecruiterRepository.java
rd8b6c91 r19398ad 2 2 3 3 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.Recruiter; 4 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.mappers.RecruiterMapper; 4 5 import org.springframework.data.jpa.repository.JpaRepository; 5 6 6 7 public interface RecruiterRepository extends JpaRepository<Recruiter, Long> { 8 Recruiter findRecruiterByEmail(String email); 7 9 } -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/service/impl/AuthServiceImpl.java
rd8b6c91 r19398ad 31 31 private final JwtService jwtService; 32 32 33 34 33 @Override 35 34 public User signUpJobSeeker(JobSeeker jobSeeker) { … … 50 49 String refreshJwt = jwtService.generateRefreshToken(new HashMap<>(), user); 51 50 52 return new JwtAuthResponse(user.get Email(), user.getName(), user.getRole().name(), jwt, refreshJwt);51 return new JwtAuthResponse(user.getId(), user.getEmail(), user.getName(), user.getRole().name(), jwt, refreshJwt); 53 52 } 54 53 … … 59 58 String jwt = jwtService.generateToken(user); 60 59 61 return new JwtAuthResponse(user.get Email(), user.getName(), user.getRole().name(), jwt, refreshTokenRequest.getToken());60 return new JwtAuthResponse(user.getId(), user.getEmail(), user.getName(), user.getRole().name(), jwt, refreshTokenRequest.getToken()); 62 61 } 63 62 return null; -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/service/impl/JwtServiceImpl.java
rd8b6c91 r19398ad 41 41 } 42 42 43 public String extractUsername(String token) {44 return extractClaim(token, Claims::getSubject);45 }46 47 43 private Claims extractAllClaims(String token) { 48 44 return Jwts.parserBuilder().setSigningKey(getSigninKey()).build().parseClaimsJws(token).getBody(); … … 54 50 } 55 51 52 @Override 53 public String extractUsername(String token) { 54 return extractClaim(token, Claims::getSubject); 55 } 56 57 @Override 56 58 public boolean isTokenValid(String token, UserDetails userDetails) { 57 59 final String username = extractUsername(token); … … 59 61 } 60 62 61 62 p rivateboolean isTokenExpired(String token) {63 @Override 64 public boolean isTokenExpired(String token) { 63 65 return extractClaim(token, Claims::getExpiration).before(new Date()); 64 66 } -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/service/impl/UserServiceImpl.java
rd8b6c91 r19398ad 1 1 package mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.service.impl; 2 2 3 import lombok.AllArgsConstructor; 4 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.repositories.JobSeekerRepository; 5 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.repositories.RecruiterRepository; 6 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.repositories.UserRepository; 7 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.service.intef.JwtService; 8 import org.springframework.security.authentication.AuthenticationManager; 9 import org.springframework.security.crypto.password.PasswordEncoder; 3 import lombok.RequiredArgsConstructor; 10 4 import org.springframework.stereotype.Service; 5 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.service.intef.UserService; 11 6 12 7 @Service 13 @AllArgsConstructor 8 @RequiredArgsConstructor 9 public class UserServiceImpl implements UserService { 14 10 15 public class UserServiceImpl {16 private final UserRepository userRepository;17 private final RecruiterRepository recruiterRepository;18 private final JobSeekerRepository jobSeekerRepository;19 private final PasswordEncoder passwordEncoder;20 private final AuthenticationManager authenticationManager;21 private final JwtService jwtService;22 23 24 // @Override25 // public User registerJobSeeker(JobSeeker jobSeeker) {26 // jobSeeker.setPassword(passwordEncoder.encode(jobSeeker.getPassword()));27 // return jobSeekerRepository.save(jobSeeker);28 // }29 //30 // @Override31 // public User registerRecruiter(Recruiter recruiter) {32 // recruiter.setPassword(passwordEncoder.encode(recruiter.getPassword()));33 // return recruiterRepository.save(recruiter);34 // }35 //36 // @Override37 // public JwtAuthenticationResponse login(JwtAuthenticationRequest jwtAuthenticationRequest) {38 // authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(jwtAuthenticationRequest.getEmail(), jwtAuthenticationRequest.getPassword()));39 //40 // var user = userRepository.findByEmail(jwtAuthenticationRequest.getEmail()).orElseThrow(() -> new IllegalArgumentException("Email or password is incorrect"));41 // var jwt = jwtService.generateToken(user);42 // var refreshToken = jwtService.generateRefreshToken(new HashMap<>(), user);43 //44 // JwtAuthenticationResponse jwtAuthenticationResponse = new JwtAuthenticationResponse();45 // jwtAuthenticationResponse.setToken(jwt);46 // jwtAuthenticationResponse.setRefreshToken(refreshToken);47 // return jwtAuthenticationResponse;48 // }49 //50 // @Override51 // public JwtAuthenticationResponse refreshToken(RefreshTokenRequest refreshTokenRequest) {52 // String userEmail = jwtService.extractUsername(refreshTokenRequest.getToken());53 // User user = userRepository.findByEmail(userEmail).orElseThrow();54 // if(jwtService.isTokenValid(refreshTokenRequest.getToken(), user)) {55 // var jwt = jwtService.generateToken(user);56 //57 // JwtAuthenticationResponse jwtAuthenticationResponse = new JwtAuthenticationResponse();58 // jwtAuthenticationResponse.setToken(jwt);59 // jwtAuthenticationResponse.setRefreshToken(refreshTokenRequest.getToken());60 // return jwtAuthenticationResponse;61 // }62 // return null;63 // }64 11 } -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/service/intef/JwtService.java
rd8b6c91 r19398ad 6 6 7 7 public interface JwtService { 8 9 8 String generateToken(UserDetails userDetails); 9 String generateRefreshToken(Map<String, Object> extraClaims, UserDetails userDetails); 10 10 String extractUsername(String token); 11 11 boolean isTokenValid(String token, UserDetails userDetails); 12 String generateRefreshToken(Map<String, Object> extraClaims, UserDetails userDetails);12 boolean isTokenExpired(String token); 13 13 14 14 } -
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/service/intef/UserService.java
rd8b6c91 r19398ad 1 1 package mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.service.intef; 2 2 3 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.DTO.SignInDTO;4 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.DTO.JwtAuthResponse;5 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.DTO.RefreshTokenRequest;6 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.JobSeeker;7 3 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.Recruiter; 8 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.User;9 4 10 5 public interface UserService { 11 User registerJobSeeker(JobSeeker jobSeeker); 12 User registerRecruiter(Recruiter recruiter); 13 JwtAuthResponse login(SignInDTO signInDTO); 14 JwtAuthResponse refreshToken(RefreshTokenRequest refreshTokenRequest); 6 15 7 }
Note:
See TracChangeset
for help on using the changeset viewer.