Remember me functionnality.

This commit is contained in:
Geoffrey POUZET 2020-10-30 13:09:15 +01:00
parent 8897a67c03
commit b437c8e68f
6 changed files with 50 additions and 21 deletions

View File

@ -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));
}
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -84,6 +84,12 @@
<label for="username"><fmt:message key="authentication.label.password"></fmt:message></label>
<input type="password" id="inputPassword" name="password" class="form-control" placeholder="<fmt:message key="authentication.placeholder.password"></fmt:message>" required />
<div class="checkbox">
<label>
<input type="checkbox" name="remember-me" /> <fmt:message key="authentication.remember_me"></fmt:message>
</label>
</div>
</form>
</div>
<div class="modal-footer">
@ -101,11 +107,11 @@
<script type="text/javascript">
$(document).ready(function () {
try {
$('#navbar_button_signin').click(function() {
$('#navbar_button_signin').click(function () {
$('#modal_navbar_signin').modal({
show: true
});
setTimeout(function() {
setTimeout(function () {
$('#username').focus();
}, 200);
return false;

View File

@ -20,20 +20,26 @@
<form id="loginForm" class="form-signin" action="${loginURL_POST}" method="POST">
<h2 class="form-signin-heading"><fmt:message key="authentication.title"></fmt:message></h2>
<label for="username"><fmt:message key="authentication.label.login"></fmt:message></label>
<label for="username"><fmt:message key="authentication.label.login"></fmt:message></label>
<input type="text" id="username" name="username" class="form-control" placeholder="<fmt:message key="authentication.placeholder.login"></fmt:message>" required autofocus />
<label for="username"><fmt:message key="authentication.label.password"></fmt:message></label>
<label for="username"><fmt:message key="authentication.label.password"></fmt:message></label>
<input type="password" id="inputPassword" name="password" class="form-control" placeholder="<fmt:message key="authentication.placeholder.password"></fmt:message>" required />
<button class="btn btn-lg btn-primary btn-block" type="submit"><fmt:message key="authentication.button.submit"></fmt:message></button>
<div class="checkbox">
<label>
<input type="checkbox" name="remember-me" /> <fmt:message key="authentication.remember_me"></fmt:message>
</label>
</div>
<br/>
<p id="errorAuth" style="color: red;" hidden>Erreur lors de l'authentification</p>
<p id="errorBDD" style="color: red;" hidden></p>
</form>
<button class="btn btn-lg btn-primary btn-block" type="submit"><fmt:message key="authentication.button.submit"></fmt:message></button>
</div>
<br/>
<p id="errorAuth" style="color: red;" hidden>Erreur lors de l'authentification</p>
<p id="errorBDD" style="color: red;" hidden></p>
</form>
</div>
<%@ include file="base/footer.jsp"%>