Remember me functionnality.
This commit is contained in:
parent
8897a67c03
commit
b437c8e68f
|
|
@ -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.controller.IndexController;
|
||||||
import fr.geoffrey.medical_training_tracker.dao.bean.User;
|
import fr.geoffrey.medical_training_tracker.dao.bean.User;
|
||||||
import fr.geoffrey.medical_training_tracker.dao.IUserDao;
|
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
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
|
|
@ -103,16 +105,7 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
|
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
|
||||||
Authentication authentication) throws IOException, ServletException {
|
Authentication authentication) throws IOException, ServletException {
|
||||||
|
|
||||||
HttpSession httpSession = request.getSession();
|
_onAuthenticationSuccess(request, response, authentication);
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.failureHandler(new AuthenticationFailureHandler() {
|
.failureHandler(new AuthenticationFailureHandler() {
|
||||||
|
|
@ -131,6 +124,7 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
.and()
|
.and()
|
||||||
// Logout authentification interne
|
// Logout authentification interne
|
||||||
.logout()
|
.logout()
|
||||||
|
.deleteCookies("JSESSIONID")
|
||||||
.logoutUrl(IndexController.URL_LOGOUT)
|
.logoutUrl(IndexController.URL_LOGOUT)
|
||||||
.logoutSuccessHandler(new LogoutSuccessHandler() {
|
.logoutSuccessHandler(new LogoutSuccessHandler() {
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -138,13 +132,23 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
throws IOException, ServletException {
|
throws IOException, ServletException {
|
||||||
|
|
||||||
// HttpSession httpSession = request.getSession();
|
// HttpSession httpSession = request.getSession();
|
||||||
|
|
||||||
response.setStatus(HttpServletResponse.SC_FOUND);
|
response.setStatus(HttpServletResponse.SC_FOUND);
|
||||||
response.setHeader("Location", request.getRequestURL().toString().replaceAll(IndexController.URL_LOGOUT.substring(1), ""));
|
response.setHeader("Location", request.getRequestURL().toString().replaceAll(IndexController.URL_LOGOUT.substring(1), ""));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.invalidateHttpSession(true)
|
.invalidateHttpSession(true)
|
||||||
.and()
|
.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();
|
.exceptionHandling();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -152,4 +156,14 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
public PasswordEncoder passwordEncoder() {
|
public PasswordEncoder passwordEncoder() {
|
||||||
return Encoder.getInstance();
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -113,3 +113,4 @@ register.placeholder.password=Password
|
||||||
register.placeholder.password_check=Password
|
register.placeholder.password_check=Password
|
||||||
register.title=Sign up
|
register.title=Sign up
|
||||||
progression.value.NOT_USED=Not usefull
|
progression.value.NOT_USED=Not usefull
|
||||||
|
authentication.remember_me=Remember me
|
||||||
|
|
|
||||||
|
|
@ -113,3 +113,4 @@ register.placeholder.password=Password
|
||||||
register.placeholder.password_check=Password
|
register.placeholder.password_check=Password
|
||||||
register.title=Sign up
|
register.title=Sign up
|
||||||
progression.value.NOT_USED=Not usefull
|
progression.value.NOT_USED=Not usefull
|
||||||
|
authentication.remember_me=Remember me
|
||||||
|
|
|
||||||
|
|
@ -113,3 +113,4 @@ register.placeholder.password=Mot de passe
|
||||||
register.placeholder.password_check=Mot de passe
|
register.placeholder.password_check=Mot de passe
|
||||||
register.title=Cr\u00e9ation du compte
|
register.title=Cr\u00e9ation du compte
|
||||||
progression.value.NOT_USED=Non utile
|
progression.value.NOT_USED=Non utile
|
||||||
|
authentication.remember_me=Se souvenir de moi
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,12 @@
|
||||||
|
|
||||||
<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 />
|
<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>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
|
|
@ -101,11 +107,11 @@
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
try {
|
try {
|
||||||
$('#navbar_button_signin').click(function() {
|
$('#navbar_button_signin').click(function () {
|
||||||
$('#modal_navbar_signin').modal({
|
$('#modal_navbar_signin').modal({
|
||||||
show: true
|
show: true
|
||||||
});
|
});
|
||||||
setTimeout(function() {
|
setTimeout(function () {
|
||||||
$('#username').focus();
|
$('#username').focus();
|
||||||
}, 200);
|
}, 200);
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -20,20 +20,26 @@
|
||||||
<form id="loginForm" class="form-signin" action="${loginURL_POST}" method="POST">
|
<form id="loginForm" class="form-signin" action="${loginURL_POST}" method="POST">
|
||||||
<h2 class="form-signin-heading"><fmt:message key="authentication.title"></fmt:message></h2>
|
<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 />
|
<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 />
|
<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/>
|
<button class="btn btn-lg btn-primary btn-block" type="submit"><fmt:message key="authentication.button.submit"></fmt:message></button>
|
||||||
<p id="errorAuth" style="color: red;" hidden>Erreur lors de l'authentification</p>
|
|
||||||
<p id="errorBDD" style="color: red;" hidden></p>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
</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"%>
|
<%@ include file="base/footer.jsp"%>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue