From bb86bbc6bab7973fce3ee1fa2b096bdced2c0c2f Mon Sep 17 00:00:00 2001 From: David Date: Wed, 24 Mar 2021 21:03:28 +0100 Subject: [PATCH 1/2] Cambio de vista del login --- .../configuration/SecurityConfiguration.java | 15 +- .../cheapy/system/LoginController.java | 32 ++ .../cheapy/system/WelcomeController.java | 2 + src/main/webapp/WEB-INF/jsp/login.jsp | 300 ++++++++++++++++++ src/main/webapp/WEB-INF/tags/menu.tag | 5 - 5 files changed, 342 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/springframework/cheapy/system/LoginController.java create mode 100644 src/main/webapp/WEB-INF/jsp/login.jsp diff --git a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java index 2fbdc84ad..677bb736d 100644 --- a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java +++ b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java @@ -20,9 +20,7 @@ import org.springframework.security.crypto.password.PasswordEncoder; * and open the template in the editor. */ -/** - * @author japarejo - */ + @Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @@ -37,19 +35,22 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter { http.authorizeRequests().antMatchers("/resources/**", "/webjars/**", "/h2-console/**").permitAll() .antMatchers(HttpMethod.GET, "/", "/oups").permitAll() .antMatchers("/users/new").permitAll() + .antMatchers("/login/**").anonymous() .antMatchers("/usuarios/new").permitAll() .antMatchers("/admin/**").hasAnyAuthority("admin") .antMatchers("/owners/**").hasAnyAuthority("owner", "admin") - .antMatchers("/vets/**").authenticated().anyRequest().denyAll() + .antMatchers("/vets/**").authenticated().anyRequest().anonymous() .and().formLogin() - /* .loginPage("/login") */ - .failureUrl("/login-error").and().logout().logoutSuccessUrl("/"); + .loginPage("/login") + .successForwardUrl("/") + .failureUrl("/login?error") + .and().logout().logoutUrl("/login?logout"); // Configuración para que funcione la consola de administración // de la BD H2 (deshabilitar las cabeceras de protección contra // ataques de tipo csrf y habilitar los framesets si su contenido // se sirve desde esta misma página. - http.csrf().ignoringAntMatchers("/h2-console/**"); + //http.csrf().ignoringAntMatchers("/h2-console/**"); http.headers().frameOptions().sameOrigin(); } diff --git a/src/main/java/org/springframework/cheapy/system/LoginController.java b/src/main/java/org/springframework/cheapy/system/LoginController.java new file mode 100644 index 000000000..e0e0fa7a9 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/system/LoginController.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cheapy.system; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +class LoginController { + + @GetMapping("/login") + public String login() { + return "login"; + } + + + +} diff --git a/src/main/java/org/springframework/cheapy/system/WelcomeController.java b/src/main/java/org/springframework/cheapy/system/WelcomeController.java index 85782e967..1f3b04637 100644 --- a/src/main/java/org/springframework/cheapy/system/WelcomeController.java +++ b/src/main/java/org/springframework/cheapy/system/WelcomeController.java @@ -27,4 +27,6 @@ class WelcomeController { return "welcome"; } + + } diff --git a/src/main/webapp/WEB-INF/jsp/login.jsp b/src/main/webapp/WEB-INF/jsp/login.jsp new file mode 100644 index 000000000..bf718d6e4 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/login.jsp @@ -0,0 +1,300 @@ +<%@ page session="false" trimDirectiveWhitespaces="true" %> +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> + + + + + + + + + + +
+
+ + + +
+ +
+ + +
+
+ +
diff --git a/src/main/webapp/WEB-INF/tags/menu.tag b/src/main/webapp/WEB-INF/tags/menu.tag index 144904a60..d398ef39b 100644 --- a/src/main/webapp/WEB-INF/tags/menu.tag +++ b/src/main/webapp/WEB-INF/tags/menu.tag @@ -41,11 +41,6 @@ - - - Login - From fbd5813f863f8b879603c4aabb1bc245b05efbd0 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 25 Mar 2021 20:15:34 +0100 Subject: [PATCH 2/2] 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" %> - +