diff --git a/src/main/java/fr/geoffrey/medical_training_tracker/config/SecSecurityConfig.java b/src/main/java/fr/geoffrey/medical_training_tracker/config/SecSecurityConfig.java index 3132e85..38496ed 100644 --- a/src/main/java/fr/geoffrey/medical_training_tracker/config/SecSecurityConfig.java +++ b/src/main/java/fr/geoffrey/medical_training_tracker/config/SecSecurityConfig.java @@ -30,6 +30,8 @@ import fr.geoffrey.medical_training_tracker.config.encoder.Encoder; import fr.geoffrey.medical_training_tracker.controller.IndexController; import fr.geoffrey.medical_training_tracker.dao.bean.User; import fr.geoffrey.medical_training_tracker.dao.IUserDao; +import org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl; +import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository; @Configuration @EnableWebSecurity @@ -103,16 +105,7 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter { public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { - HttpSession httpSession = request.getSession(); - httpSession.setAttribute(IndexController.SESSION_ATTRIBUTE_AUTHENTICATION, authentication); - -// Object oUserDetails = authentication.getPrincipal(); -// if (oUserDetails instanceof UserDetails) { -// UserDetails userDetails = (UserDetails) oUserDetails; -// } - - response.setStatus(HttpServletResponse.SC_FOUND); - response.setHeader("Location", request.getRequestURL().toString().replaceAll(IndexController.URL_LOGIN, IndexController.URL_ANIMAL_LIST)); + _onAuthenticationSuccess(request, response, authentication); } }) .failureHandler(new AuthenticationFailureHandler() { @@ -131,6 +124,7 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter { .and() // Logout authentification interne .logout() + .deleteCookies("JSESSIONID") .logoutUrl(IndexController.URL_LOGOUT) .logoutSuccessHandler(new LogoutSuccessHandler() { @Override @@ -138,13 +132,23 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter { throws IOException, ServletException { // HttpSession httpSession = request.getSession(); - response.setStatus(HttpServletResponse.SC_FOUND); response.setHeader("Location", request.getRequestURL().toString().replaceAll(IndexController.URL_LOGOUT.substring(1), "")); } }) .invalidateHttpSession(true) .and() + .rememberMe().key("uniqueAndSecret") + .tokenValiditySeconds(86400) + .authenticationSuccessHandler(new AuthenticationSuccessHandler() { + @Override + public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, + Authentication authentication) throws IOException, ServletException { + + _onAuthenticationSuccess(request, response, authentication); + } + }) + .and() .exceptionHandling(); } @@ -152,4 +156,14 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter { public PasswordEncoder passwordEncoder() { return Encoder.getInstance(); } + + private void _onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, + Authentication authentication) { + + HttpSession httpSession = request.getSession(); + httpSession.setAttribute(IndexController.SESSION_ATTRIBUTE_AUTHENTICATION, authentication); + + response.setStatus(HttpServletResponse.SC_FOUND); + response.setHeader("Location", request.getRequestURL().toString().replaceAll(IndexController.URL_LOGIN, IndexController.URL_ANIMAL_LIST)); + } } diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index b65624e..5aa4adf 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -113,3 +113,4 @@ register.placeholder.password=Password register.placeholder.password_check=Password register.title=Sign up progression.value.NOT_USED=Not usefull +authentication.remember_me=Remember me diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 7e9ebc7..40fc821 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -113,3 +113,4 @@ register.placeholder.password=Password register.placeholder.password_check=Password register.title=Sign up progression.value.NOT_USED=Not usefull +authentication.remember_me=Remember me diff --git a/src/main/resources/messages_fr.properties b/src/main/resources/messages_fr.properties index 751d149..d27f7d6 100644 --- a/src/main/resources/messages_fr.properties +++ b/src/main/resources/messages_fr.properties @@ -113,3 +113,4 @@ register.placeholder.password=Mot de passe register.placeholder.password_check=Mot de passe register.title=Cr\u00e9ation du compte progression.value.NOT_USED=Non utile +authentication.remember_me=Se souvenir de moi diff --git a/src/main/webapp/vue/base/navbar.jsp b/src/main/webapp/vue/base/navbar.jsp index b518e6c..06d3015 100644 --- a/src/main/webapp/vue/base/navbar.jsp +++ b/src/main/webapp/vue/base/navbar.jsp @@ -84,6 +84,12 @@ " required /> + +