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

I have added remember me features that handles both regular login and social login at a time. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
@@ -1,10 +1,13 @@
package net.petrikainulainen.spring.social.signinmvc.config;

import javax.sql.DataSource;

import net.petrikainulainen.spring.social.signinmvc.security.service.CustomPersistentTokenBasedRememberMeServices;
import net.petrikainulainen.spring.social.signinmvc.security.service.RepositoryUserDetailsService;
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,6 +18,8 @@
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.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.social.security.SpringSocialConfigurer;

Expand All @@ -27,6 +32,10 @@ public class SecurityContext extends WebSecurityConfigurerAdapter {

@Autowired
private UserRepository userRepository;


@Autowired
DataSource dataSource;

@Override
public void configure(WebSecurity web) throws Exception {
Expand Down Expand Up @@ -63,6 +72,11 @@ protected void configure(HttpSecurity http) throws Exception {
).permitAll()
//The rest of the our application is protected.
.antMatchers("/**").hasRole("USER")
//Adds the CustomPersistentTokenBasedRememberMeServices.
.and()
.rememberMe()
.key("myRememberMeKey")
.rememberMeServices(customPersistentTokenBasedRememberMeServices())
//Adds the SocialAuthenticationFilter to Spring Security's filter chain.
.and()
.apply(new SpringSocialConfigurer());
Expand Down Expand Up @@ -103,4 +117,27 @@ public SocialUserDetailsService socialUserDetailsService() {
public UserDetailsService userDetailsService() {
return new RepositoryUserDetailsService(userRepository);
}

/**
* This bean is the custom persistent token-based remember me service which handles persistent remember
* using browser cookie for both regular login and social login.
*/
@Bean
public CustomPersistentTokenBasedRememberMeServices customPersistentTokenBasedRememberMeServices(){
CustomPersistentTokenBasedRememberMeServices rememberMeServices = new CustomPersistentTokenBasedRememberMeServices("myRememberMeKey", userDetailsService(), persistentTokenRepository());
rememberMeServices.setParameter("rememberme");
rememberMeServices.setTokenValiditySeconds(1209600);
return rememberMeServices;
}

/**
* This bean is the JDBC token repository for remember me services.
*/
@Bean
public PersistentTokenRepository persistentTokenRepository() {
JdbcTokenRepositoryImpl db = new JdbcTokenRepositoryImpl();
db.setCreateTableOnStartup(false);
db.setDataSource(dataSource);
return db;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.petrikainulainen.spring.social.signinmvc.security.service;

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;

/**
* This class handles rememberMeRequested decision only.
* This rememberMeRequested returns original results for regular(id/password) login but returns always 'true' for social login.
* @author Hosang Jeon
*/
public class CustomPersistentTokenBasedRememberMeServices extends
PersistentTokenBasedRememberMeServices {

public CustomPersistentTokenBasedRememberMeServices(String key,
UserDetailsService userDetailsService,
PersistentTokenRepository tokenRepository) {
super(key, userDetailsService, tokenRepository);
}

@Override
protected boolean rememberMeRequested(HttpServletRequest request,
String parameter) {

String isRegularLogin = request.getParameter("isRegularLogin");

// Regular Login
if (isRegularLogin != null && "true".equals(isRegularLogin)) {
return super.rememberMeRequested(request, parameter);
}
// Social Login
else{
// returns always 'true' for social login.
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@

<!-- The rest of our application is protected. -->
<security:intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>

<!-- Enable Remember Me Services -->
<security:remember-me key="myRememberMeKey" services-ref="customPersistentTokenBasedRememberMeServices" />

<!-- Adds social authentication filter to the Spring Security filter chain. -->
<security:custom-filter ref="socialAuthenticationFilter" before="PRE_AUTH_FILTER" />
Expand Down Expand Up @@ -67,7 +70,30 @@

<!-- Sets the url of the registration form. -->
<property name="signupUrl" value="/user/register"/>
<!-- Define remember-me services for social authentication filter -->
<property name="rememberMeServices" ref="customPersistentTokenBasedRememberMeServices" />
</bean>

<!--
Configures the custom persistent token-based remember me service which handles persistent remember
using browser cookie for both regular login and social login.
-->
<bean id="customPersistentTokenBasedRememberMeServices" class="net.petrikainulainen.spring.social.signinmvc.security.service.CustomPersistentTokenBasedRememberMeServices">
<constructor-arg value="myRememberMeKey"/>
<constructor-arg ref="userDetailsService"/>
<constructor-arg ref="tokenRepository"/>
<property name="parameter" value="rememberme"/>
<property name="tokenValiditySeconds" value="1209600"/>
</bean>

<!--
Configures the JDBC token repository for remember me services.
-->
<bean id="tokenRepository"
class="org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl">
<property name="createTableOnStartup" value="false" />
<property name="dataSource" ref="dataSource" />
</bean>

<!--
Configures the social authentication provider which processes authentication requests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</c:if>
<form action="/login/authenticate" method="POST" role="form">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<input type="hidden" name="isRegularLogin" value="true" />
<div class="row">
<div id="form-group-email" class="form-group col-lg-4">
<label class="control-label" for="user-email"><spring:message code="label.user.email"/>:</label>
Expand All @@ -37,6 +38,11 @@
<input id="user-password" name="password" type="password" class="form-control"/>
</div>
</div>
<div class="row">
<div id="form-group-rememberme" class="col-lg-4">
<input type="checkbox" name="rememberme" value="true" checked="checked"> Remember me
</div>
</div>
<div class="row">
<div class="form-group col-lg-4">
<button type="submit" class="btn btn-default"><spring:message code="label.user.login.submit.button"/></button>
Expand Down