From fbd5813f863f8b879603c4aabb1bc245b05efbd0 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 25 Mar 2021 20:15:34 +0100 Subject: [PATCH] Vista de login --- pom.xml | 3 +++ .../cheapy/configuration/SecurityConfiguration.java | 8 ++++---- .../cheapy/system/LoginController.java | 9 ++++++++- src/main/webapp/WEB-INF/jsp/login.jsp | 12 +++++++----- .../WEB-INF/jsp/owners/createOrUpdateOwnerForm.jsp | 3 +-- src/main/webapp/WEB-INF/jsp/owners/findOwners.jsp | 3 +-- src/main/webapp/WEB-INF/jsp/owners/ownerDetails.jsp | 3 +-- src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp | 3 +-- .../webapp/WEB-INF/jsp/users/createOwnerForm.jsp | 3 +-- src/main/webapp/WEB-INF/tags/menu.tag | 8 ++++---- src/main/webapp/WEB-INF/tags/menuItem.tag | 3 +-- 11 files changed, 32 insertions(+), 26 deletions(-) diff --git a/pom.xml b/pom.xml index 60be53522..38e0cd40b 100644 --- a/pom.xml +++ b/pom.xml @@ -135,7 +135,10 @@ spring-boot-devtools true + + + diff --git a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java index 677bb736d..673ad7a35 100644 --- a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java +++ b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java @@ -36,15 +36,15 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter { .antMatchers(HttpMethod.GET, "/", "/oups").permitAll() .antMatchers("/users/new").permitAll() .antMatchers("/login/**").anonymous() + .antMatchers("/logout").permitAll() .antMatchers("/usuarios/new").permitAll() .antMatchers("/admin/**").hasAnyAuthority("admin") .antMatchers("/owners/**").hasAnyAuthority("owner", "admin") .antMatchers("/vets/**").authenticated().anyRequest().anonymous() .and().formLogin() - .loginPage("/login") - .successForwardUrl("/") - .failureUrl("/login?error") - .and().logout().logoutUrl("/login?logout"); + .loginPage("/login").permitAll() + .failureUrl("/login?error") + .and().logout().logoutSuccessUrl("/login"); // Configuración para que funcione la consola de administración // de la BD H2 (deshabilitar las cabeceras de protección contra diff --git a/src/main/java/org/springframework/cheapy/system/LoginController.java b/src/main/java/org/springframework/cheapy/system/LoginController.java index e0e0fa7a9..e26025570 100644 --- a/src/main/java/org/springframework/cheapy/system/LoginController.java +++ b/src/main/java/org/springframework/cheapy/system/LoginController.java @@ -16,6 +16,9 @@ package org.springframework.cheapy.system; +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @@ -24,7 +27,11 @@ class LoginController { @GetMapping("/login") public String login() { - return "login"; + Authentication authentication= SecurityContextHolder.getContext().getAuthentication(); + if(authentication==null || authentication instanceof AnonymousAuthenticationToken) { + return "login"; + } + return "redirect:/"; } diff --git a/src/main/webapp/WEB-INF/jsp/login.jsp b/src/main/webapp/WEB-INF/jsp/login.jsp index bf718d6e4..796cdceeb 100644 --- a/src/main/webapp/WEB-INF/jsp/login.jsp +++ b/src/main/webapp/WEB-INF/jsp/login.jsp @@ -4,9 +4,9 @@ <%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %> <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> - +