Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remember me login #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import net.petrikainulainen.spring.social.signinmvc.security.service.SimpleSocialUserDetailsService;
import net.petrikainulainen.spring.social.signinmvc.user.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
Expand All @@ -15,16 +14,25 @@
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.social.security.SpringSocialConfigurer;

import javax.sql.DataSource;

/**
* @author Petri Kainulainen
*/
@Configuration
@EnableWebSecurity
public class SecurityContext extends WebSecurityConfigurerAdapter {

@Autowired
private DataSource dataSource;

@Autowired
private UserRepository userRepository;

Expand All @@ -47,9 +55,14 @@ protected void configure(HttpSecurity http) throws Exception {
//Configures the logout function
.and()
.logout()
.deleteCookies("JSESSIONID")
.deleteCookies("JSESSIONID", "SPRING_SECURITY_REMEMBER_ME_COOKIE")
.logoutUrl("/logout")
.logoutSuccessUrl("/login")
.and()
.rememberMe()
.key("uniqueSecret")
.rememberMeServices(rememberMeServices())
.tokenValiditySeconds(172800)
//Configures url based authorization
.and()
.authorizeRequests()
Expand Down Expand Up @@ -87,6 +100,24 @@ public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(10);
}

@Bean
public PersistentTokenRepository persistentTokenRepository() {
JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
tokenRepository.setDataSource(dataSource);
return tokenRepository;
}

@Bean
public RememberMeServices rememberMeServices() {
PersistentTokenBasedRememberMeServices rememberMeServices = new PersistentTokenBasedRememberMeServices(
"uniqueSecret",
userDetailsService(),
persistentTokenRepository()
);
rememberMeServices.setAlwaysRemember(true);
return rememberMeServices;
}

/**
* This bean is used to load the user specific data when social sign in
* is used.
Expand Down