diff --git a/curriculumsearch/pom.xml b/curriculumsearch/pom.xml index 40c2bd1..716be53 100644 --- a/curriculumsearch/pom.xml +++ b/curriculumsearch/pom.xml @@ -63,6 +63,11 @@ jstl 1.2 + + + org.springframework.boot + spring-boot-starter-security + diff --git a/curriculumsearch/src/main/java/com/roshka/configuration/CustomUserDetails.java b/curriculumsearch/src/main/java/com/roshka/configuration/CustomUserDetails.java new file mode 100644 index 0000000..d8ad7b4 --- /dev/null +++ b/curriculumsearch/src/main/java/com/roshka/configuration/CustomUserDetails.java @@ -0,0 +1,56 @@ +package com.roshka.configuration; + +import java.util.Collection; + +import com.roshka.modelo.RRHHUser; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; + +public class CustomUserDetails implements UserDetails { + + private RRHHUser user; + + public CustomUserDetails(RRHHUser user) { + this.user = user; + } + + @Override + public Collection getAuthorities() { + return null; + } + + @Override + public String getPassword() { + return user.getPassword(); + } + + @Override + public String getUsername() { + return user.getEmail(); + } + + @Override + public boolean isAccountNonExpired() { + return true; + } + + @Override + public boolean isAccountNonLocked() { + return true; + } + + @Override + public boolean isCredentialsNonExpired() { + return true; + } + + @Override + public boolean isEnabled() { + return true; + } + + public String getFullName() { + return user.getFirstName() + " " + user.getLastName(); + } + +} \ No newline at end of file diff --git a/curriculumsearch/src/main/java/com/roshka/configuration/CustomUserDetailsService.java b/curriculumsearch/src/main/java/com/roshka/configuration/CustomUserDetailsService.java new file mode 100644 index 0000000..cd1f7cb --- /dev/null +++ b/curriculumsearch/src/main/java/com/roshka/configuration/CustomUserDetailsService.java @@ -0,0 +1,24 @@ +package com.roshka.configuration; + +import com.roshka.modelo.RRHHUser; +import com.roshka.repositorio.RRHHUserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; + +public class CustomUserDetailsService implements UserDetailsService { + + @Autowired + private RRHHUserRepository userRepo; + + @Override + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + RRHHUser user = userRepo.findByEmail(username); + if (user == null) { + throw new UsernameNotFoundException("User not found"); + } + return new CustomUserDetails(user); + } + +} diff --git a/curriculumsearch/src/main/java/com/roshka/configuration/WebSecurityConfig.java b/curriculumsearch/src/main/java/com/roshka/configuration/WebSecurityConfig.java new file mode 100644 index 0000000..df23eb4 --- /dev/null +++ b/curriculumsearch/src/main/java/com/roshka/configuration/WebSecurityConfig.java @@ -0,0 +1,61 @@ +package com.roshka.configuration; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.dao.DaoAuthenticationProvider; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; + +import javax.sql.DataSource; + +@Configuration +@EnableWebSecurity +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + @Autowired + private DataSource dataSource; + + @Bean + public UserDetailsService userDetailsService() { + return new CustomUserDetailsService(); + } + + @Bean + public BCryptPasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + + @Bean + public DaoAuthenticationProvider authenticationProvider() { + DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); + authProvider.setUserDetailsService(userDetailsService()); + authProvider.setPasswordEncoder(passwordEncoder()); + + return authProvider; + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.authenticationProvider(authenticationProvider()); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.authorizeRequests() + .antMatchers("/").authenticated() + .antMatchers("/home").authenticated() + .anyRequest().permitAll() + .and() + .formLogin() + .usernameParameter("email") + .defaultSuccessUrl("/home") + .permitAll() + .and() + .logout().logoutSuccessUrl("/").permitAll(); + } + +} \ No newline at end of file diff --git a/curriculumsearch/src/main/java/com/roshka/controller/PostulanteController.java b/curriculumsearch/src/main/java/com/roshka/controller/PostulanteController.java index 1d0e158..fae38c2 100644 --- a/curriculumsearch/src/main/java/com/roshka/controller/PostulanteController.java +++ b/curriculumsearch/src/main/java/com/roshka/controller/PostulanteController.java @@ -1,7 +1,6 @@ package com.roshka.controller; -import java.util.List; import javax.validation.ConstraintViolationException; @@ -13,12 +12,7 @@ import com.roshka.modelo.EstadoCivil; import com.roshka.modelo.Nacionalidad; import com.roshka.modelo.Postulante; import com.roshka.modelo.TipoExperiencia; -import com.roshka.repositorio.CiudadRepository; -import com.roshka.repositorio.DepartamentoRepository; -import com.roshka.repositorio.ExperienciaRepository; -import com.roshka.repositorio.InstitucionRepository; -import com.roshka.repositorio.PostulanteRepository; -import com.roshka.repositorio.TecnologiaRepository; +import com.roshka.repositorio.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -31,6 +25,7 @@ import org.springframework.web.bind.annotation.*; @Controller +@RequestMapping("/") public class PostulanteController { PostulanteRepository post; TecnologiaRepository tecRepo; @@ -40,7 +35,10 @@ public class PostulanteController { CiudadRepository ciuRepo; @Autowired - public PostulanteController(PostulanteRepository post, TecnologiaRepository tecRepo, ExperienciaRepository expRepo, InstitucionRepository institucionRepository, DepartamentoRepository depRepo, CiudadRepository ciuRepo) { + public PostulanteController( + PostulanteRepository post, TecnologiaRepository tecRepo, ExperienciaRepository expRepo, + InstitucionRepository institucionRepository, DepartamentoRepository depRepo, + CiudadRepository ciuRepo) { this.post = post; this.tecRepo = tecRepo; this.expRepo = expRepo; @@ -49,8 +47,7 @@ public class PostulanteController { this.ciuRepo = ciuRepo; } - - @RequestMapping("/") + @RequestMapping("home") public String index() { diff --git a/curriculumsearch/src/main/java/com/roshka/controller/RRHHUserController.java b/curriculumsearch/src/main/java/com/roshka/controller/RRHHUserController.java new file mode 100644 index 0000000..98b26c7 --- /dev/null +++ b/curriculumsearch/src/main/java/com/roshka/controller/RRHHUserController.java @@ -0,0 +1,45 @@ +package com.roshka.controller; + +import com.roshka.modelo.RRHHUser; +import com.roshka.repositorio.RRHHUserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; + +import javax.servlet.http.HttpServletRequest; + +@Controller +public class RRHHUserController { + private static final long REGISTER_CODE = 1234; + RRHHUserRepository rrhhUserRepository; + + @Autowired + public RRHHUserController(RRHHUserRepository rrhhUserRepository){ + this.rrhhUserRepository = rrhhUserRepository; + } + + @GetMapping("/register") + public String showRegistrationForm(Model model) { + model.addAttribute("user", new RRHHUser()); + + return "registration"; + } + + @PostMapping("/process_register") + public String processRegister(HttpServletRequest request, RRHHUser user) { + if(Long.parseLong(request.getParameter("registrationCode")) != REGISTER_CODE){ + return "redirect:/register"; + } + BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); + String encodedPassword = passwordEncoder.encode(user.getPassword()); + user.setPassword(encodedPassword); + + rrhhUserRepository.save(user); + + return "register_success"; + } +} diff --git a/curriculumsearch/src/main/java/com/roshka/modelo/RRHHUser.java b/curriculumsearch/src/main/java/com/roshka/modelo/RRHHUser.java new file mode 100644 index 0000000..9a09c01 --- /dev/null +++ b/curriculumsearch/src/main/java/com/roshka/modelo/RRHHUser.java @@ -0,0 +1,63 @@ +package com.roshka.modelo; + +import javax.persistence.*; + +@Entity +@Table(name = "recursos_humanos_user") +public class RRHHUser { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column(nullable = false, unique = true, length = 45) + private String email; + + @Column(nullable = false, length = 64) + private String password; + + @Column(name = "first_name", nullable = false, length = 20) + private String firstName; + + @Column(name = "last_name", nullable = false, length = 20) + private String lastName; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } +} diff --git a/curriculumsearch/src/main/java/com/roshka/repositorio/RRHHUserRepository.java b/curriculumsearch/src/main/java/com/roshka/repositorio/RRHHUserRepository.java new file mode 100644 index 0000000..d13c336 --- /dev/null +++ b/curriculumsearch/src/main/java/com/roshka/repositorio/RRHHUserRepository.java @@ -0,0 +1,8 @@ +package com.roshka.repositorio; + +import com.roshka.modelo.RRHHUser; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface RRHHUserRepository extends JpaRepository { + RRHHUser findByEmail(String username); +} diff --git a/curriculumsearch/src/main/resources/application.properties b/curriculumsearch/src/main/resources/application.properties index 192b323..031335f 100644 --- a/curriculumsearch/src/main/resources/application.properties +++ b/curriculumsearch/src/main/resources/application.properties @@ -1,10 +1,10 @@ spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect -spring.jpa.show-sql=true +#spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true -logging.level.org.hibernate.SQL=DEBUG -logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE +#logging.level.org.hibernate.SQL=DEBUG +#logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE spring.sql.init.mode=always spring.sql.init.platform=postgres diff --git a/curriculumsearch/src/main/webapp/jsp/index.jsp b/curriculumsearch/src/main/webapp/jsp/index.jsp index 2f174d6..5817e9d 100644 --- a/curriculumsearch/src/main/webapp/jsp/index.jsp +++ b/curriculumsearch/src/main/webapp/jsp/index.jsp @@ -1,3 +1,4 @@ + @@ -8,5 +9,13 @@ Form postulante +
+
+

+ Welcome +

+ +
+
\ No newline at end of file diff --git a/curriculumsearch/src/main/webapp/jsp/register_success.jsp b/curriculumsearch/src/main/webapp/jsp/register_success.jsp new file mode 100644 index 0000000..7c28633 --- /dev/null +++ b/curriculumsearch/src/main/webapp/jsp/register_success.jsp @@ -0,0 +1,15 @@ + + + + + Registration Success + + + +
+

You have signed up successfully!

+

Click here to Login

+
+ + + \ No newline at end of file diff --git a/curriculumsearch/src/main/webapp/jsp/registration.jsp b/curriculumsearch/src/main/webapp/jsp/registration.jsp new file mode 100644 index 0000000..74b4217 --- /dev/null +++ b/curriculumsearch/src/main/webapp/jsp/registration.jsp @@ -0,0 +1,75 @@ +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> + + + + Registration Success + + + + + + + + + +
+
+
+
+
+ Sample photo +
+

Informacion de Registro

+ + +
+ Email + +
+
+
+
+ Nombre + +
+
+
+
+ Apellido + +
+
+
+ +
+ Contrasena + +
+
+
+ +
+ + +
+ +
+
+ + +
+ +
+
+
+
+
+
+ +