rememberme

This commit is contained in:
_Bastler 2021-10-04 10:35:35 +02:00
parent 6d6f21d4f2
commit 1ba9ca32e4
4 changed files with 63 additions and 2 deletions

View File

@ -10,7 +10,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version> <java.version>11</java.version>
<revision>0.2.2-SNAPSHOT</revision> <revision>0.3.0-SNAPSHOT</revision>
</properties> </properties>
<parent> <parent>

View File

@ -0,0 +1,49 @@
/**
*
*/
package de.bstly.board.security;
import javax.servlet.http.HttpServletRequest;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
/**
*
* @author _bastler@bstly.de
*
*/
public class LocalRememberMeServices extends PersistentTokenBasedRememberMeServices {
/**
* @param key
* @param userDetailsService
* @param tokenRepository
*/
public LocalRememberMeServices(String key, UserDetailsService userDetailsService,
PersistentTokenRepository tokenRepository) {
super(key, userDetailsService, tokenRepository);
}
/*
*
* @see org.springframework.security.web.authentication.rememberme.
* AbstractRememberMeServices#rememberMeRequested(javax.servlet.http.
* HttpServletRequest, java.lang.String)
*/
@Override
protected boolean rememberMeRequested(HttpServletRequest request, String parameter) {
Object value = request.getAttribute(parameter);
if (value != null) {
String paramValue = value.toString();
if (paramValue.equalsIgnoreCase("true") || paramValue.equalsIgnoreCase("on")
|| paramValue.equalsIgnoreCase("yes") || paramValue.equals("1")) {
return true;
}
}
return super.rememberMeRequested(request, parameter);
}
}

View File

@ -14,6 +14,7 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -30,6 +31,7 @@ public class OAuth2AuthenticationSuccessHandler
@Autowired @Autowired
private UserManager localUserManager; private UserManager localUserManager;
private RememberMeServices rememberMeServices;
@Override @Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
@ -43,8 +45,17 @@ public class OAuth2AuthenticationSuccessHandler
SecurityContextHolder.getContext().setAuthentication(newAuthentication); SecurityContextHolder.getContext().setAuthentication(newAuthentication);
if (rememberMeServices != null) {
request.setAttribute("remember-me", "true");
rememberMeServices.loginSuccess(request, response, newAuthentication);
}
handle(request, response, newAuthentication); handle(request, response, newAuthentication);
clearAuthenticationAttributes(request); clearAuthenticationAttributes(request);
} }
public void setRememberMeServices(RememberMeServices rememberMeServices) {
this.rememberMeServices = rememberMeServices;
}
} }

View File

@ -55,6 +55,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
oAuth2AuthenticationSuccessHandler.setDefaultTargetUrl(loginTargetUrl); oAuth2AuthenticationSuccessHandler.setDefaultTargetUrl(loginTargetUrl);
oAuth2AuthenticationSuccessHandler.setRememberMeServices(rememberMeServices());
http http
// crsf // crsf
@ -103,7 +104,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean @Bean
public RememberMeServices rememberMeServices() { public RememberMeServices rememberMeServices() {
PersistentTokenBasedRememberMeServices rememberMeServices = new PersistentTokenBasedRememberMeServices( PersistentTokenBasedRememberMeServices rememberMeServices = new LocalRememberMeServices(
"remember-me", localUserManager, persistentTokenRepository()); "remember-me", localUserManager, persistentTokenRepository());
return rememberMeServices; return rememberMeServices;
} }