Browse Source

Prubas

master
Cristobal Bernal Mayordomo 1 year ago
parent
commit
9890c8a803
10 changed files with 44 additions and 44 deletions
  1. +5
    -0
      pom.xml
  2. +15
    -0
      src/main/java/com/cristobalbernal/foro/Entidades/UsersEntity.java
  3. +1
    -1
      src/main/java/com/cristobalbernal/foro/controlador/Controlador.java
  4. +0
    -18
      src/main/java/com/cristobalbernal/foro/seguridad/McvConfig.java
  5. +12
    -12
      src/main/java/com/cristobalbernal/foro/seguridad/SegurityConfig.java
  6. +3
    -4
      src/main/java/com/cristobalbernal/foro/seguridad/models/UserDetailsImpl.java
  7. +2
    -4
      src/main/java/com/cristobalbernal/foro/seguridad/models/UserDetailsServiceImpl.java
  8. +3
    -3
      src/main/resources/templates/index.html
  9. +1
    -1
      src/main/resources/templates/login/login.html
  10. +2
    -1
      src/main/resources/templates/menu/menu.html

+ 5
- 0
pom.xml View File

@ -109,6 +109,11 @@
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity5 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
</dependencies>


+ 15
- 0
src/main/java/com/cristobalbernal/foro/Entidades/UsersEntity.java View File

@ -34,6 +34,10 @@ public class UsersEntity {
@Column(name = "tipoPrivilegios", nullable = true)
private Byte tipoPrivilegios;
public UsersEntity() {
}
public int getId() {
return id;
}
@ -140,4 +144,15 @@ public class UsersEntity {
result = 31 * result + (tipoPrivilegios != null ? tipoPrivilegios.hashCode() : 0);
return result;
}
public UsersEntity(String name, String firstname, String secondname, String password, String email, String username, String imgen, Byte tipoPrivilegios) {
this.name = name;
this.firstname = firstname;
this.secondname = secondname;
this.password = password;
this.email = email;
this.username = username;
this.imgen = imgen;
this.tipoPrivilegios = tipoPrivilegios;
}
}

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

@ -65,7 +65,7 @@ public class Controlador {
String encodedPassword = passwordEncoder.encode(user.getPassword());
user.setPassword(encodedPassword);
iUsers.save(user);
return "index";
return "redirect:/home";
}
}

+ 0
- 18
src/main/java/com/cristobalbernal/foro/seguridad/McvConfig.java View File

@ -1,18 +0,0 @@
package com.cristobalbernal.foro.seguridad;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
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");
}
}

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

@ -1,23 +1,24 @@
package com.cristobalbernal.foro.seguridad;
import com.cristobalbernal.foro.seguridad.models.CustomUserDetailService;
import com.cristobalbernal.foro.seguridad.models.UserDetailsImpl;
import com.cristobalbernal.foro.seguridad.models.UserDetailsServiceImpl;
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();
public UserDetailsServiceImpl userDetailsService(){
return new UserDetailsServiceImpl();
}
@Bean
public PasswordEncoder passwordEncoder() {
@ -39,21 +40,20 @@ public class SegurityConfig {
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests()
.requestMatchers("/crearPregunta","/miperfil").authenticated()
.anyRequest().permitAll()
.requestMatchers("/crearPregunta","/miperfil").authenticated()
.anyRequest().permitAll()
.and()
.formLogin()
.usernameParameter("email")
.loginPage("/login")
.defaultSuccessUrl("/home")
.permitAll()
.usernameParameter("email")
.loginPage("/login")
.defaultSuccessUrl("/") //TODO:
.permitAll()
.and()
.logout(LogoutConfigurer::permitAll);
.logout().logoutSuccessUrl("/").permitAll();
return http.build();
}
}

src/main/java/com/cristobalbernal/foro/seguridad/models/CustomUserDetails.java → src/main/java/com/cristobalbernal/foro/seguridad/models/UserDetailsImpl.java View File

@ -1,18 +1,17 @@
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 {
public class UserDetailsImpl implements UserDetails {
private final UsersEntity user;
private UsersEntity user;
public CustomUserDetails(UsersEntity user) {
public UserDetailsImpl(UsersEntity user) {
this.user = user;
}

src/main/java/com/cristobalbernal/foro/seguridad/models/CustomUserDetailService.java → src/main/java/com/cristobalbernal/foro/seguridad/models/UserDetailsServiceImpl.java View File

@ -7,9 +7,7 @@ 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 {
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private IUsers iUsers;
@ -17,7 +15,7 @@ public class CustomUserDetailService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UsersEntity users = iUsers.findByEmail(username);
return new CustomUserDetails(users);
return new UserDetailsImpl(users);
}
}

+ 3
- 3
src/main/resources/templates/index.html View File

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="es">
<html lang="es" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset="utf-8">
@ -18,7 +18,7 @@
</head>
<body>
<header th:replace="menu/menu :: mimenu"></header>
<header th:replace="~{menu/menu :: mimenu}"></header>
<!--======= welcome section on top background=====-->
<section class="welcome-part-one">
@ -213,7 +213,7 @@
<script src="js/jquery-3.1.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/npm.js"></script>
<footer th:replace="footer/footer"></footer>
<footer th:replace="~{footer/footer}"></footer>
</body>
</html>

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

@ -25,7 +25,7 @@
<div class="title">Log In</div>
<div class="description">Hello there, Log In</div>
<form th:action="@{/login}" method="post">
<input type="email" name="username" placeholder="Name"/>
<input type="email" name="email" placeholder="Name"/>
<input type="password" name="password" placeholder="Password"/>
<div class="text-center">
<input class="button" type="submit" value="Login In">


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

@ -1,4 +1,4 @@
<nav th:fragment="mimenu">
<nav th:fragment="mimenu" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<div class="top-menu-bottom932">
<nav class="navbar navbar-default">
<div class="container">
@ -14,6 +14,7 @@
<li><a href="/home">Home</a></li>
<li><a href="/crearPregunta">Ask Question</a></li>
<li><a href="/miperfil">Acceder</a></li>
<!--<span sec:authentication="name">Sin autenticar</span> -->
</ul>
</div>
<!-- /.navbar-collapse -->


Loading…
Cancel
Save