Browse Source

nos consigo el puto login

master
Cristobal Bernal Mayordomo 1 year ago
parent
commit
4304387fe8
13 changed files with 213 additions and 78 deletions
  1. +17
    -15
      pom.xml
  2. +5
    -1
      src/main/java/com/cristobalbernal/foro/Respositorios/ICategoriaForo.java
  3. +3
    -1
      src/main/java/com/cristobalbernal/foro/Respositorios/IUsers.java
  4. +0
    -6
      src/main/java/com/cristobalbernal/foro/Servicios/ServicioRespuestas.java
  5. +28
    -0
      src/main/java/com/cristobalbernal/foro/Servicios/ServicioUser.java
  6. +13
    -2
      src/main/java/com/cristobalbernal/foro/controlador/Controlador.java
  7. +0
    -44
      src/main/java/com/cristobalbernal/foro/seguridad/ConfigSeguridad.java
  8. +8
    -6
      src/main/java/com/cristobalbernal/foro/seguridad/McvConfig.java
  9. +59
    -0
      src/main/java/com/cristobalbernal/foro/seguridad/SegurityConfig.java
  10. +23
    -0
      src/main/java/com/cristobalbernal/foro/seguridad/models/CustomUserDetailService.java
  11. +53
    -0
      src/main/java/com/cristobalbernal/foro/seguridad/models/CustomUserDetails.java
  12. +2
    -1
      src/main/resources/application.properties
  13. +2
    -2
      src/main/resources/templates/login/login.html

+ 17
- 15
pom.xml View File

@ -37,6 +37,12 @@
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core -->
<dependency>
@ -57,24 +63,13 @@
<artifactId>bootstrap</artifactId>
<version>5.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity5 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars.bower/jquery -->
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>jquery</artifactId>
<version>3.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars/webjars-locator -->
<dependency>
<groupId>org.webjars</groupId>
@ -104,12 +99,19 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
<artifactId>spring-security-web</artifactId>
</dependency>
</dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-config -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>


+ 5
- 1
src/main/java/com/cristobalbernal/foro/Respositorios/ICategoriaForo.java View File

@ -1,4 +1,8 @@
package com.cristobalbernal.foro.Respositorios;
public interface ICategoriaForo {
import com.cristobalbernal.foro.Entidades.CategoriaForoEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ICategoriaForo extends JpaRepository<CategoriaForoEntity,Integer> {
}

+ 3
- 1
src/main/java/com/cristobalbernal/foro/Respositorios/IUsers.java View File

@ -2,7 +2,9 @@ package com.cristobalbernal.foro.Respositorios;
import com.cristobalbernal.foro.Entidades.UsersEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.web.bind.annotation.PostMapping;
public interface IUsers extends JpaRepository<UsersEntity,Integer> {
public interface IUsers extends JpaRepository<UsersEntity,Long> {
UsersEntity findByEmail(String email);
}

+ 0
- 6
src/main/java/com/cristobalbernal/foro/Servicios/ServicioRespuestas.java View File

@ -16,12 +16,6 @@ public class ServicioRespuestas {
@PostConstruct
public void init(){
/*
respuestas = new RespuestasEntity[6];
respuestas[0] = new RespuestasEntity(0, "TITULO 1", "aslkjbdakjdbkajwdbjakwdbkawjdbawkd.bawdawd" +
"adaw,dbakwjdbawkjd,abjwkd,awk,dad" +
"awda,wbjdwakjdawkjdawdblaw");
*/
}
}

+ 28
- 0
src/main/java/com/cristobalbernal/foro/Servicios/ServicioUser.java View File

@ -0,0 +1,28 @@
package com.cristobalbernal.foro.Servicios;
import com.cristobalbernal.foro.Entidades.Foro;
import com.cristobalbernal.foro.Entidades.UsersEntity;
import com.cristobalbernal.foro.Respositorios.IForo;
import com.cristobalbernal.foro.Respositorios.IUsers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ServicioUser {
@Autowired
private IUsers iUsers;
public List<UsersEntity> init(){
return iUsers.findAll();
}
/*
public UsersEntity findById(long id){
return iUsers.findById((int) id).orElse(null);
}
*/
}

+ 13
- 2
src/main/java/com/cristobalbernal/foro/controlador/Controlador.java View File

@ -7,10 +7,12 @@ import com.cristobalbernal.foro.Respositorios.IUsers;
import com.cristobalbernal.foro.Servicios.ServicioForo;
import com.cristobalbernal.foro.Servicios.ServicioRespuestas;
import org.springframework.beans.factory.annotation.Autowired;
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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@ -34,12 +36,12 @@ public class Controlador {
return "index";
}
@GetMapping("/login")
public String login(){
public String login(Model model){
return "login/login";
}
@GetMapping("/registrar")
public String registrar(){
return "Registrar/registrar";
return "Registrar/Registrar";
}
@GetMapping("/crearPregunta")
public String pregunta(){
@ -56,5 +58,14 @@ public class Controlador {
public @ResponseBody List<UsersEntity> getAllUsers(){
return iUsers.findAll();
}
@PostMapping("/process_register")
public String processRegister(UsersEntity user) {
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encodedPassword = passwordEncoder.encode(user.getPassword());
user.setPassword(encodedPassword);
iUsers.save(user);
return "index";
}
}

+ 0
- 44
src/main/java/com/cristobalbernal/foro/seguridad/ConfigSeguridad.java View File

@ -1,44 +0,0 @@
package com.cristobalbernal.foro.seguridad;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.configurers.LogoutConfigurer;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
public class ConfigSeguridad {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((requests) -> requests
.requestMatchers("","/home","/","/registrar","/css/**","/image/**","/fonts/**","js/**","/postdetall/{id}").permitAll()
.anyRequest().authenticated()
)
.formLogin((form) -> form
.loginPage("/login")
.permitAll()
)
.logout(LogoutConfigurer::permitAll);
return http.build();
}
@Bean
public UserDetailsService userDetailsService() {
UserDetails user =
User.withDefaultPasswordEncoder()
.username("tobal")
.password("1234")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
}

src/main/java/com/cristobalbernal/foro/seguridad/MvcConfig.java → src/main/java/com/cristobalbernal/foro/seguridad/McvConfig.java View File

@ -5,12 +5,14 @@ import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registration){
registration.addViewController("/").setViewName("inicio");
registration.addViewController("/crearPregunta").setViewName("crearPregunta");
registration.addViewController("/login").setViewName("login");
public class McvConfig implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("").setViewName("index");
registry.addViewController("/index").setViewName("index");
registry.addViewController("/login").setViewName("login");
}
}

+ 59
- 0
src/main/java/com/cristobalbernal/foro/seguridad/SegurityConfig.java View File

@ -0,0 +1,59 @@
package com.cristobalbernal.foro.seguridad;
import com.cristobalbernal.foro.seguridad.models.CustomUserDetailService;
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.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.LogoutConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
public class SegurityConfig {
@Bean
public CustomUserDetailService userDetailsService(){
return new CustomUserDetailService();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(userDetailsService());
authProvider.setPasswordEncoder(passwordEncoder());
return authProvider;
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests()
.requestMatchers("/crearPregunta","/miperfil").authenticated()
.anyRequest().permitAll()
.and()
.formLogin()
.usernameParameter("email")
.loginPage("/login")
.defaultSuccessUrl("/home")
.permitAll()
.and()
.logout(LogoutConfigurer::permitAll);
return http.build();
}
}

+ 23
- 0
src/main/java/com/cristobalbernal/foro/seguridad/models/CustomUserDetailService.java View File

@ -0,0 +1,23 @@
package com.cristobalbernal.foro.seguridad.models;
import com.cristobalbernal.foro.Entidades.UsersEntity;
import com.cristobalbernal.foro.Respositorios.IUsers;
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;
import java.util.ArrayList;
public class CustomUserDetailService implements UserDetailsService {
@Autowired
private IUsers iUsers;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UsersEntity users = iUsers.findByEmail(username);
return new CustomUserDetails(users);
}
}

+ 53
- 0
src/main/java/com/cristobalbernal/foro/seguridad/models/CustomUserDetails.java View File

@ -0,0 +1,53 @@
package com.cristobalbernal.foro.seguridad.models;
import com.cristobalbernal.foro.Entidades.UsersEntity;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
public class CustomUserDetails implements UserDetails {
private final UsersEntity user;
public CustomUserDetails(UsersEntity user) {
this.user = user;
}
@Override
public Collection<? extends GrantedAuthority> 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;
}
}

+ 2
- 1
src/main/resources/application.properties View File

@ -6,4 +6,5 @@ spring.datasource.url=jdbc:mariadb://localhost:3306/forogruas
spring.datasource.username=root
spring.datasource.password=
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
spring.data.jpa.repositories.enabled=true
spring.data.jpa.repositories.enabled=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

+ 2
- 2
src/main/resources/templates/login/login.html View File

@ -25,11 +25,11 @@
<div class="title">Log In</div>
<div class="description">Hello there, Log In</div>
<form th:action="@{/login}" method="post">
<input type="text" name="username" placeholder="Name"/>
<input type="email" name="username" placeholder="Name"/>
<input type="password" name="password" placeholder="Password"/>
<div class="text-center">
<input class="button" type="submit" value="Login In">
<a href="signup.html"><div class="button">Create an account</div></a>
<a href="/registrar"><div class="button">Create an account</div></a>
</div>
</form>
</div>


Loading…
Cancel
Save