Changeset 6eba109 for springapp/src
- Timestamp:
- 08/17/22 16:21:10 (2 years ago)
- Branches:
- main
- Children:
- 702ca77
- Parents:
- 800779d
- Location:
- springapp/src/main
- Files:
-
- 1 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
springapp/src/main/java/mk/profesori/springapp/Controller/PublicController.java
r800779d r6eba109 1 1 package mk.profesori.springapp.Controller; 2 2 3 import java.util.Collections; 3 4 import java.util.List; 5 import java.util.Map; 4 6 import java.util.Optional; 5 7 … … 21 23 @RestController 22 24 @RequestMapping("/public") 23 @CrossOrigin(origins = { "http://192.168.0.1 7:3000", "http://192.168.0.24:3000" })25 @CrossOrigin(origins = { "http://192.168.0.18:3000", "http://192.168.0.24:3000" }) 24 26 public class PublicController { 25 27 … … 95 97 return mainService.getCityById(cityId); // vrakja grad spored id 96 98 } 99 100 @RequestMapping(value = "/loginSuccessRegular", method = RequestMethod.GET) 101 public Map<String, String> loginSuccessRegular(@RequestParam String sessionId) { 102 return Collections.singletonMap("sessionId", sessionId); 103 } 104 105 @RequestMapping(value = "/loginSuccessModerator", method = RequestMethod.GET) 106 public Map<String, String> loginSuccessModerator(@RequestParam String sessionId) { 107 return Collections.singletonMap("sessionId", sessionId); 108 } 97 109 } -
springapp/src/main/java/mk/profesori/springapp/Controller/SecureController.java
r800779d r6eba109 1 1 package mk.profesori.springapp.Controller; 2 3 import java.util.Collections; 4 import java.util.Map; 2 5 3 6 import org.springframework.beans.factory.annotation.Autowired; … … 5 8 import org.springframework.security.core.annotation.CurrentSecurityContext; 6 9 import org.springframework.security.core.context.SecurityContext; 10 import org.springframework.security.core.userdetails.UserDetails; 7 11 import org.springframework.web.bind.annotation.CrossOrigin; 8 12 import org.springframework.web.bind.annotation.PathVariable; … … 10 14 import org.springframework.web.bind.annotation.RequestMapping; 11 15 import org.springframework.web.bind.annotation.RequestMethod; 16 import org.springframework.web.bind.annotation.RequestParam; 12 17 import org.springframework.web.bind.annotation.RestController; 13 18 … … 15 20 16 21 import mk.profesori.springapp.Model.CustomUserDetails; 22 import mk.profesori.springapp.Service.CustomUserDetailsService; 17 23 import mk.profesori.springapp.Service.MainService; 18 24 19 25 @RestController 20 26 @RequestMapping("/secure") 21 @CrossOrigin(origins = { "http://192.168.0.1 7:3000", "http://192.168.0.24:3000" })27 @CrossOrigin(origins = { "http://192.168.0.18:3000", "http://192.168.0.24:3000" }) 22 28 public class SecureController { 23 29 24 30 @Autowired 25 31 private MainService mainService; 32 @Autowired 33 CustomUserDetailsService customUserDetailsService; 26 34 27 35 @RequestMapping(value = "/professor/{professorId}/addOpinion", method = RequestMethod.POST) … … 52 60 } 53 61 62 @RequestMapping(value = "/user", method = RequestMethod.GET) 63 public UserDetails getUserDetails(@CurrentSecurityContext SecurityContext context) { 64 65 Authentication authentication = context.getAuthentication(); 66 if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails) { 67 CustomUserDetails currentUser = (CustomUserDetails) authentication.getPrincipal(); 68 return customUserDetailsService.loadUserByUsername(currentUser.getEmail()); 69 } 70 71 return null; 72 } 73 54 74 } -
springapp/src/main/java/mk/profesori/springapp/Model/CustomUserDetails.java
r800779d r6eba109 53 53 private Set<ConfirmationToken> confirmationTokens = new HashSet<>(); 54 54 @OneToMany(mappedBy = "author", cascade = CascadeType.ALL) 55 private List<Post> authoredPosts = new ArrayList<>();55 private Set<Post> authoredPosts = new HashSet<>(); 56 56 57 57 public CustomUserDetails(String fullName, String username, String email, String password, UserRole userRole) { … … 99 99 } 100 100 101 List<Post> getAuthoredPosts() {101 public Set<Post> getAuthoredPosts() { 102 102 return this.authoredPosts; 103 103 } -
springapp/src/main/java/mk/profesori/springapp/Security/SecurityConfiguration.java
r800779d r6eba109 10 10 import org.springframework.security.crypto.password.PasswordEncoder; 11 11 import org.springframework.security.web.SecurityFilterChain; 12 import org.springframework.security.web.authentication.AuthenticationSuccessHandler; 12 13 import org.springframework.web.servlet.config.annotation.CorsRegistry; 13 14 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; … … 36 37 @Override 37 38 public void addCorsMappings(CorsRegistry registry) { 38 registry.addMapping("/**").allowedOrigins("http://192.168.0.17:3000", "http://192.168.0.24:3000"); 39 registry.addMapping("/**").allowedOrigins("http://192.168.0.18:3000", "http://192.168.0.24:3000") 40 .allowCredentials(true); 39 41 } 40 42 }; 43 } 44 45 @Bean 46 public AuthenticationSuccessHandler customAuthenticationSuccessHandler() { 47 return new CustomAuthenticationSuccessHandler(); 41 48 } 42 49 … … 54 61 .antMatchers("/registration/**").permitAll() 55 62 .and() 56 .formLogin() ;63 .formLogin().successHandler(customAuthenticationSuccessHandler()); 57 64 58 65 return http.build(); -
springapp/src/main/java/mk/profesori/springapp/Service/RegistrationService.java
r800779d r6eba109 25 25 private final EmailSender emailSender; 26 26 private final UserRepository userRepository; 27 27 28 28 public String register(RegistrationRequest request) { 29 29 30 30 boolean isValidEmail = emailValidator.test(request.getEmail()); 31 if(!isValidEmail) throw new IllegalStateException("Invalid email"); 31 if (!isValidEmail) 32 throw new IllegalStateException("Invalid email"); 32 33 33 34 boolean isValidPassword = passwordValidator.test(request.getPassword()); 34 if(!isValidPassword) throw new IllegalStateException("Invalid password"); 35 if (!isValidPassword) 36 throw new IllegalStateException("Invalid password"); 35 37 36 38 boolean isValidUsername = usernameValidator.test(request.getUsername()); 37 if(!isValidUsername) throw new IllegalStateException("Invalid username"); 39 if (!isValidUsername) 40 throw new IllegalStateException("Invalid username"); 38 41 39 42 boolean emailExists = userRepository.findByEmail(request.getEmail()).isPresent(); 40 if(emailExists) { 41 if(!userRepository.findByEmail(request.getEmail()).get().isEnabled()) { 42 String tokenToResend = customUserDetailsService.createToken(userRepository.findByEmail(request.getEmail()).get()); 43 String link = "http://192.168.0.17:8080/registration/confirm?token=" + tokenToResend; 43 if (emailExists) { 44 if (!userRepository.findByEmail(request.getEmail()).get().isEnabled()) { 45 String tokenToResend = customUserDetailsService 46 .createToken(userRepository.findByEmail(request.getEmail()).get()); 47 String link = "http://192.168.0.18:8080/registration/confirm?token=" + tokenToResend; 44 48 emailSender.send(request.getEmail(), emailSender.buildEmail(request.getUsername(), link)); 45 return tokenToResend; 49 return tokenToResend; 46 50 } else { 47 throw new IllegalStateException("Email is taken");51 throw new IllegalStateException("Email is taken"); 48 52 } 49 53 } 50 54 51 55 boolean usernameExists = userRepository.findByUsername(request.getUsername()).isPresent(); 52 if (usernameExists) {56 if (usernameExists) { 53 57 throw new IllegalStateException("Username is taken"); 54 58 } 55 59 56 60 String token = customUserDetailsService.signUp( 57 new CustomUserDetails( 58 request.getFullName(), 59 request.getUsername(), 60 request.getEmail(), 61 request.getPassword(), 62 UserRole.REGULAR 63 ) 64 ); 65 66 String link = "http://192.168.0.17:8080/registration/confirm?token=" + token; 67 61 new CustomUserDetails( 62 request.getFullName(), 63 request.getUsername(), 64 request.getEmail(), 65 request.getPassword(), 66 UserRole.REGULAR)); 67 68 String link = "http://192.168.0.18:8080/registration/confirm?token=" + token; 69 68 70 emailSender.send(request.getEmail(), emailSender.buildEmail(request.getUsername(), link)); 69 71 70 72 return token; 71 73 } … … 75 77 ConfirmationToken confirmationToken = confirmationTokenService 76 78 .getToken(token) 77 .orElseThrow(() -> 78 new IllegalStateException("Token not found")); 79 .orElseThrow(() -> new IllegalStateException("Token not found")); 79 80 80 81 if (confirmationToken.getConfirmedAt() != null) { -
springapp/src/main/resources/application.properties
r800779d r6eba109 7 7 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect 8 8 spring.jpa.properties.hibernate.format_sql=true 9 server.address=192.168.0.1 79 server.address=192.168.0.18 10 10 spring.mail.host=192.168.0.24 11 11 spring.mail.username=mailuser
Note:
See TracChangeset
for help on using the changeset viewer.