Browse Source

comit

master
Cristobal Bernal Mayordomo 1 year ago
parent
commit
074018be5f
6 changed files with 81 additions and 17 deletions
  1. +11
    -3
      pom.xml
  2. +4
    -7
      src/main/java/com/cristobalbernal/foro/controlador/Controlador.java
  3. +44
    -0
      src/main/java/com/cristobalbernal/foro/seguridad/ConfigSeguridad.java
  4. +17
    -0
      src/main/java/com/cristobalbernal/foro/seguridad/MvcConfig.java
  5. +4
    -6
      src/main/resources/templates/login/login.html
  6. +1
    -1
      src/main/resources/templates/menu/menu.html

+ 11
- 3
pom.xml View File

@ -59,9 +59,6 @@
<version>6.2.0</version> <version>6.2.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId> <artifactId>spring-boot-devtools</artifactId>
@ -73,6 +70,17 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>


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

@ -15,15 +15,12 @@ public class Controlador {
@Autowired @Autowired
private ServicioRespuestas servicioRespuestas; private ServicioRespuestas servicioRespuestas;
@GetMapping({"/entrar/{usuario}", "/entrar", "/usuario/{usuario}", "/usuario"})
public String login(@PathVariable(name = "usuario", required = false) String usuario,
Model model) throws MalformedURLException {
model.addAttribute("titulo", "Panel de control");
model.addAttribute("usuario", usuario);
return "admin/login";
@GetMapping("/miperfil")
public String login(){
return "login/login";
} }
@GetMapping("/home")
@GetMapping({"","/home","/"})
public String indice(Model model){ public String indice(Model model){
model.addAttribute("listaRespuestas",servicioRespuestas.findAll()); model.addAttribute("listaRespuestas",servicioRespuestas.findAll());
return "index"; return "index";


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

@ -0,0 +1,44 @@
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").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);
}
}

+ 17
- 0
src/main/java/com/cristobalbernal/foro/seguridad/MvcConfig.java View File

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

src/main/resources/templates/admin/login.html → src/main/resources/templates/login/login.html View File

@ -24,17 +24,15 @@
<div class="modal-body modal-body-step-1 is-showing"> <div class="modal-body modal-body-step-1 is-showing">
<div class="title">Log In</div> <div class="title">Log In</div>
<div class="description">Hello there, Log In</div> <div class="description">Hello there, Log In</div>
<form>
<input type="text" placeholder="Name"/>
<input type="email" placeholder="Email"/>
<form th:action="@{/login}" method="post">
<input type="text" name="username" placeholder="Name"/>
<input type="password" name="password" placeholder="Password"/>
<div class="text-center"> <div class="text-center">
<div class="button">LOG IN</div>
<input class="button" type="submit" value="Login In">
<a href="signup.html"><div class="button">Create an account</div></a> <a href="signup.html"><div class="button">Create an account</div></a>
</div> </div>
</form> </form>
</div> </div>
</div> </div>
</div> </div>
<script src='https://code.jquery.com/jquery-2.2.4.min.js'></script> <script src='https://code.jquery.com/jquery-2.2.4.min.js'></script>

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

@ -13,7 +13,7 @@
<ul class="nav navbar-nav navbar-right"> <ul class="nav navbar-nav navbar-right">
<li><a href="/home">Home</a></li> <li><a href="/home">Home</a></li>
<li><a href="/crearPregunta">Ask Question</a></li> <li><a href="/crearPregunta">Ask Question</a></li>
<li><a href="/entrar">Acceder</a></li>
<li><a href="/miperfil">Acceder</a></li>
</ul> </ul>
</div> </div>
<!-- /.navbar-collapse --> <!-- /.navbar-collapse -->


Loading…
Cancel
Save