Social Login in Spring MVC with Spring Social Security
1. Tables
Firstly you need to create the tables:
** ORACLE **
-- Create table
create table USERCONNECTION
(
USERID VARCHAR2(255) not null,
PROVIDERID VARCHAR2(255) not null,
PROVIDERUSERID VARCHAR2(255) not null,
RANK INTEGER not null,
DISPLAYNAME VARCHAR2(255),
PROFILEURL VARCHAR2(512),
IMAGEURL VARCHAR2(512),
ACCESSTOKEN VARCHAR2(255) not null,
SECRET VARCHAR2(255),
REFRESHTOKEN VARCHAR2(255),
EXPIRETIME NUMBER(19)
) ;
-- Create/Recreate primary, unique and foreign key constraints
alter table USERCONNECTION
add primary key (USERID, PROVIDERID, PROVIDERUSERID) ;
-- Create/Recreate indexes
create unique index USERCONNECTIONRANK on USERCONNECTION (USERID, PROVIDERID, RANK) ;
----------------------------------------- Create table
create table USER_ACCOUNTS
(
ID VARCHAR2(255) not null,
EMAIL VARCHAR2(100) not null,
USER_NAME VARCHAR2(100) not null,
FIRST_NAME VARCHAR2(100) not null,
LAST_NAME VARCHAR2(100) not null,
PASSWORD VARCHAR2(255),
ROLE VARCHAR2(20) not null,
ENABLED VARCHAR2(1) default 'Y' not null
) ;
-- Create/Recreate primary, unique and foreign key constraints
alter table USER_ACCOUNTS
add primary key (ID) ;
alter table USER_ACCOUNTS
add constraint User_Account_UK1 unique (EMAIL);
alter table USER_ACCOUNTS
add constraint User_Account_UK2 unique (USER_NAME);
** MYSQL/SQL SERVER **
-- Create table
create table USERCONNECTION
(
USERID VARCHAR(255) not null,
PROVIDERID VARCHAR(255) not null,
PROVIDERUSERID VARCHAR(255) not null,
RANK INTEGER not null,
DISPLAYNAME VARCHAR(255),
PROFILEURL VARCHAR(512),
IMAGEURL VARCHAR(512),
ACCESSTOKEN VARCHAR(255) not null,
SECRET VARCHAR(255),
REFRESHTOKEN VARCHAR(255),
EXPIRETIME BIGINT
) ;
-- Create/Recreate primary, unique and foreign key constraints
alter table USERCONNECTION
add primary key (USERID, PROVIDERID, PROVIDERUSERID) ;
-- Create/Recreate indexes
create unique index USERCONNECTIONRANK on USERCONNECTION (USERID, PROVIDERID, RANK) ;
-----------------------------------------
-- Create table
create table USER_ACCOUNTS
(
ID VARCHAR(255) not null,
EMAIL VARCHAR(100) not null,
USER_NAME VARCHAR(100) not null,
FIRST_NAME VARCHAR(100) not null,
LAST_NAME VARCHAR(100) not null,
PASSWORD VARCHAR(255),
ROLE VARCHAR(20) not null,
ENABLED VARCHAR(1) default 'Y' not null
) ;
-- Create/Recreate primary, unique and foreign key constraints
alter table USER_ACCOUNTS
add primary key (ID) ;
alter table USER_ACCOUNTS
add constraint User_Account_UK1 unique (EMAIL);
alter table USER_ACCOUNTS
add constraint User_Account_UK2 unique (USER_NAME);

USERCONNECTION & USER_ACCOUNTS
If you have a Spring Web application, and log in the apps via the accounts stored in a table, such as USER_ACCOUNTS. (Note: The structure of users table may be different from this table structure).
You can integrate the login function via social network into your Website. You need to create an USERCONNECTION table. Let's note that the table structure is supplied by Spring Social Security API, so inserting and updating data of the table are also supported by this API. However, you can customize its structure.
Note: Inserting and updating of the USERCONNECTION table are carried out by the JdbcUsersConnectionRepository of Spring Social Security API.
Data example:

2. Login Models
In case users login with social network, your application requires users to allow access to the basic information. (name, email...). If the users do not allow, the program will be redirected to signin page in order to login in the normal way.

In case users allow the application to be accessible to their basic information, there are 2 models illustrated as below:
Model 1: Display the signup page after logging in with social network
After users login with social network, you have UserProfile, the UserProfile is a object for storing the users' basic information and social network information, you can find the userID in USERCONNECTION table, corresponding to the UserProfile. And find records User_Accounts corresponding to userID. If there is no corresponding record in USER_ACCOUNTS table, it will redirect to the signup page.
After signing up, an USER_ACCOUNTS record is created while an USERCONNECTION record is also created then login to website automatically.
After signing up, an USER_ACCOUNTS record is created while an USERCONNECTION record is also created then login to website automatically.

Model 2: Create an account automatically after logging in with social network.
This Model is similar to Model 1, if the system can find out USERCONNECTION record that corresponds to UserProfile, a User_Accounts record will be created, and create a USERCONNECTION record.

3. Register OAuth2
If you want to log in using Google you need a Client ID and Password and declared the 'Redirect URLs' that your application can redirect to, after the user has successfully logged Google. See more at:
Similar to Facebook you need to create 'Facebook Application Developers' and App ID & Secret, as well as declaring the 'Redirect URLs' that your application can redirect to, after the user has successfully logged in to Facebook. See more at:
4. Configure web.xml and Maven
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Spring Social Security</display-name>
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.o7planning</groupId>
<artifactId>SpringMVCSocialJdbc</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVCSocialJdbc Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- Generic properties -->
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<repositories>
<!-- Repository for ORACLE JDBC Driver -->
<repository>
<id>codelds</id>
<url>https://code.lds.org/nexus/content/groups/main-repo</url>
</repository>
</repositories>
<dependencies>
<!-- Spring framework START -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!-- Spring framework END -->
<!-- Spring Security Artifacts - START -->
<!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-config -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-taglibs -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<!-- Spring Security Artifacts - END -->
<!-- Jstl for jsp page -->
<!-- http://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Servlet API -->
<!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- JSP API -->
<!-- http://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- MySQL JDBC driver -->
<!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<!-- Oracle JDBC driver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<!-- SQLServer JDBC driver (JTDS) -->
<!-- http://mvnrepository.com/artifact/net.sourceforge.jtds/jtds -->
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework.social/spring-social-config -->
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-config</artifactId>
<version>1.1.4.RELEASE</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework.social/spring-social-core -->
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-core</artifactId>
<version>1.1.4.RELEASE</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework.social/spring-social-security -->
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-security</artifactId>
<version>1.1.4.RELEASE</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework.social/spring-social-web -->
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-web</artifactId>
<version>1.1.4.RELEASE</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework.social/spring-social-facebook -->
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-facebook</artifactId>
<version>2.0.3.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>spring-social-core</artifactId>
<groupId>org.springframework.social</groupId>
</exclusion>
<exclusion>
<artifactId>spring-social-config</artifactId>
<groupId>org.springframework.social</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework.social/spring-social-google -->
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-google</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework.social/spring-social-twitter -->
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-twitter</artifactId>
<version>1.1.2.RELEASE</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework.social/spring-social-linkedin -->
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-linkedin</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
<!-- Email validator,... -->
<!-- http://mvnrepository.com/artifact/commons-validator/commons-validator -->
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMVCSocialJdbc</finalName>
<plugins>
<!-- Config: Maven Tomcat Plugin -->
<!-- http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!-- Config: contextPath and Port (Default: /SpringMVCSocialJdbc : 8080) -->
<!--
<configuration>
<path>/</path>
<port>8899</port>
</configuration>
-->
</plugin>
</plugins>
</build>
</project>
5. Configure Spring MVC and Security

ApplicationContextConfig.java
package org.o7planning.socialsecurity.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@ComponentScan("org.o7planning.socialsecurity.*")
@EnableTransactionManagement
// Load to Environment.
@PropertySource("classpath:datasource-cfg.properties")
public class ApplicationContextConfig {
// The Environment class serves as the property holder
// and stores all the properties loaded by the @PropertySource
// Lưu trữ các giá thuộc tính load bởi @PropertySource.
@Autowired
private Environment env;
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource rb = new ResourceBundleMessageSource();
// Load property in message/validator.properties
rb.setBasenames(new String[] { "messages/validator" });
return rb;
}
@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean(name = "dataSource")
public DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
// See: datasouce-cfg.properties
// Xem: datasouce-cfg.properties
dataSource.setDriverClassName(env.getProperty("ds.database-driver"));
dataSource.setUrl(env.getProperty("ds.url"));
dataSource.setUsername(env.getProperty("ds.username"));
dataSource.setPassword(env.getProperty("ds.password"));
System.out.println("## getDataSource: " + dataSource);
return dataSource;
}
// Transaction Manager
@Autowired
@Bean(name = "transactionManager")
public DataSourceTransactionManager getTransactionManager(DataSource dataSource) {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
return transactionManager;
}
}
SpringWebAppInitializer.java
package org.o7planning.socialsecurity.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class SpringWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(ApplicationContextConfig.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher",
new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
WebMvcConfig.java
package org.o7planning.socialsecurity.config;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
private static final Charset UTF8 = Charset.forName("UTF-8");
// Config UTF-8 Encoding.
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("text", "plain", UTF8)));
converters.add(stringConverter);
// Add other converters ...
}
// Static Resource Config
// equivalents for <mvc:resources/> tags
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(31556926);
registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(31556926);
registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(31556926);
}
// Equivalent for <mvc:default-servlet-handler/> tag
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Database info:
datasource-cfg.properties (** For ORACLE DB)
# DataSource
ds.database-driver=oracle.jdbc.driver.OracleDriver
ds.url=jdbc:oracle:thin:@localhost:1521:db12c
ds.username=oauth2
ds.password=oauth2
datasource-cfg.properties (** For MYSQL DB)
# DataSource
ds.database-driver=com.mysql.jdbc.Driver
ds.url=jdbc:mysql://localhost:3306/oauth2
ds.username=root
ds.password=12345
datasource-cfg.properties (** For SQL SERVER DB)
# DataSource
ds.database-driver=net.sourceforge.jtds.jdbc.Driver
ds.url=jdbc:jtds:sqlserver://localhost:1433/simplehr;instance=SQLEXPRESS
ds.username=oauth2
ds.password=12345
Configuring security:
SpringSecurityInitializer.java
package org.o7planning.socialsecurity.config;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
// Config: Spring Security Filter.
public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer {
// Do nothing
}
WebSecurityConfig.java
package org.o7planning.socialsecurity.config;
import org.o7planning.socialsecurity.service.MyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.social.security.SpringSocialConfigurer;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private MyUserDetailsService myUserDetailsService;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
// Set Service load Users in Database
auth.userDetailsService(myUserDetailsService);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
// The pages does not require login
http.authorizeRequests().antMatchers("/", "/signup", "/login", "/logout").permitAll();
// /userInfo page requires login as ROLE_USER
// If no login, it will redirect to /login page.
http.authorizeRequests().antMatchers("/userInfo").access("hasRole('ROLE_USER')");
// Form Login config
http.authorizeRequests().and().formLogin()//
// Submit URL of login page.
.loginProcessingUrl("/j_spring_security_check") // Submit URL
.loginPage("/login")//
.defaultSuccessUrl("/userInfo")//
.failureUrl("/login?error=true")//
.usernameParameter("username")//
.passwordParameter("password");
// Logout Config
http.authorizeRequests().and().logout().logoutUrl("/logout").logoutSuccessUrl("/");
// Spring Social Config.
http.apply(new SpringSocialConfigurer())
//
.signupUrl("/signup");
}
// This bean is load the user specific data when form login is used.
@Override
public UserDetailsService userDetailsService() {
return myUserDetailsService;
}
}
Configuring Spring Social Security:
SocialConfig.java
package org.o7planning.socialsecurity.config;
import javax.sql.DataSource;
import org.o7planning.socialsecurity.dao.MyUserAccountDAO;
import org.o7planning.socialsecurity.signup.MyConnectionSignUp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.social.UserIdSource;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurer;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.connect.web.ConnectController;
import org.springframework.social.facebook.connect.FacebookConnectionFactory;
import org.springframework.social.google.connect.GoogleConnectionFactory;
import org.springframework.social.linkedin.connect.LinkedInConnectionFactory;
import org.springframework.social.security.AuthenticationNameUserIdSource;
import org.springframework.social.twitter.connect.TwitterConnectionFactory;
@Configuration
@EnableSocial
// Load to Environment.
@PropertySource("classpath:social-cfg.properties")
public class SocialConfig implements SocialConfigurer {
private boolean autoSignUp = false;
@Autowired
private DataSource dataSource;
@Autowired
private MyUserAccountDAO myUserAccountDAO;
// @env: Read properties file config by @PropertySource.
@Override
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
// Twitter
TwitterConnectionFactory tfactory = new TwitterConnectionFactory(//
env.getProperty("twitter.consumer.key"), //
env.getProperty("twitter.consumer.secret"));
// tfactory.setScope(env.getProperty("twitter.scope"));
cfConfig.addConnectionFactory(tfactory);
// Facebook
FacebookConnectionFactory ffactory = new FacebookConnectionFactory(//
env.getProperty("facebook.app.id"), //
env.getProperty("facebook.app.secret"));
ffactory.setScope(env.getProperty("facebook.scope"));
// auth_type=reauthenticate
cfConfig.addConnectionFactory(ffactory);
// Linkedin
LinkedInConnectionFactory lfactory = new LinkedInConnectionFactory(//
env.getProperty("linkedin.consumer.key"), //
env.getProperty("linkedin.consumer.secret"));
lfactory.setScope(env.getProperty("linkedin.scope"));
cfConfig.addConnectionFactory(lfactory);
// Google
GoogleConnectionFactory gfactory = new GoogleConnectionFactory(//
env.getProperty("google.client.id"), //
env.getProperty("google.client.secret"));
gfactory.setScope(env.getProperty("google.scope"));
cfConfig.addConnectionFactory(gfactory);
}
// The UserIdSource determines the userID of the user.
@Override
public UserIdSource getUserIdSource() {
return new AuthenticationNameUserIdSource();
}
// Read and insert to USERCONNECTION.
@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
// org.springframework.social.security.SocialAuthenticationServiceRegistry
JdbcUsersConnectionRepository usersConnectionRepository = new JdbcUsersConnectionRepository(dataSource,
connectionFactoryLocator,
Encryptors.noOpText());
if (autoSignUp) {
// Config to:
// After login to social.
// Automatically create corresponding USER_ACCOUNT if not already.
ConnectionSignUp connectionSignUp = new MyConnectionSignUp(myUserAccountDAO);
usersConnectionRepository.setConnectionSignUp(connectionSignUp);
} else {
// Config to:
// After login to social.
// If USER_ACCOUNTS does not exists
// Redirect to register page.
usersConnectionRepository.setConnectionSignUp(null);
}
return usersConnectionRepository;
}
// This bean manages the connection flow between the account provider and
// the example application.
@Bean
public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator, //
ConnectionRepository connectionRepository) {
return new ConnectController(connectionFactoryLocator, connectionRepository);
}
}
Configuring social:

social-cfg.properties
# Twitter
# http://localhost:8080/SpringMVCSocialJdbc/auth/twitter
twitter.consumer.key=
twitter.consumer.secret=
twitter.scope=
# Facebook
# http://localhost:8080/SpringMVCSocialJdbc/auth/facebook
facebook.app.id=1084911261562762
facebook.app.secret=81a324fdbc4cade1ee25523c7bff58b3
facebook.scope=public_profile,email
# Linkedin
# http://localhost:8080/SpringMVCSocialJdbc/auth/linkedin
linkedin.consumer.key=...
linkedin.consumer.secret=...
linkedin.scope=
# Google
# http://localhost:8080/SpringMVCSocialJdbc/auth/google
google.client.id=160790488111-he5fn6rq0foqg05te70dk25gifeoum9s.apps.googleusercontent.com
google.client.secret=VILTmX1UjOnyw2_meYvDQEdl
google.scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile
Note: To login a Google social network, you must enable Google+ APIs:
6. User, User Serivce, User DAO

MyUserAccount that describes a record of USER_ACCOUNTS table.
MyUserAccount.java
package org.o7planning.socialsecurity.model;
public class MyUserAccount {
public static final String ROLE_USER = "ROLE_USER";
private String id;
private String email;
private String userName;
private String firstName;
private String lastName;
private String password;
private String role;
public MyUserAccount() {
}
public MyUserAccount(String id, String email,String userName, String firstName, //
String lastName, String password, //
String role) {
this.id = id;
this.email = email;
this.userName= userName;
this.firstName = firstName;
this.lastName = lastName;
this.password = password;
this.role = role;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
MyUserAccountMapper is class helps mapping between fields of MyUserAccount class with columns of USER_ACCOUNTS table.
MyUserAccountMapper.java
package org.o7planning.socialsecurity.mapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.o7planning.socialsecurity.model.MyUserAccount;
import org.springframework.jdbc.core.RowMapper;
public class MyUserAccountMapper implements RowMapper<MyUserAccount> {
@Override
public MyUserAccount mapRow(ResultSet rs, int rowNum) throws SQLException {
String id = rs.getString("id");
String email = rs.getString("email");
String userName= rs.getString("user_name");
String firstName = rs.getString("first_name");
String lastName = rs.getString("last_name");
String password = rs.getString("password");
String role = rs.getString("role");
return new MyUserAccount(id, email,userName, firstName, //
lastName, password, //
role );
}
}
MyUserAccountDAO.java
package org.o7planning.socialsecurity.dao;
import java.util.UUID;
import javax.sql.DataSource;
import org.o7planning.socialsecurity.form.MyUserAccountForm;
import org.o7planning.socialsecurity.mapper.MyUserAccountMapper;
import org.o7planning.socialsecurity.model.MyUserAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionKey;
import org.springframework.social.connect.UserProfile;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional
public class MyUserAccountDAO extends JdbcDaoSupport {
@Autowired
public MyUserAccountDAO(DataSource dataSource) {
this.setDataSource(dataSource);
}
public MyUserAccount findById(String id) {
String sql = "Select id,email,user_name, first_name,last_name,"//
+ " password,role"//
+ " from User_Accounts u "//
+ " where id = ? ";
Object[] params = new Object[] { id };
MyUserAccountMapper mapper = new MyUserAccountMapper();
try {
MyUserAccount userInfo = this.getJdbcTemplate().queryForObject(sql, params, mapper);
return userInfo;
} catch (EmptyResultDataAccessException e) {
return null;
}
}
public MyUserAccount findByEmail(String email) {
String sql = "Select id, email,user_name,first_name,last_name,"//
+ " password,role"//
+ " from User_Accounts u "//
+ " where email = ? ";
Object[] params = new Object[] { email };
MyUserAccountMapper mapper = new MyUserAccountMapper();
try {
MyUserAccount userInfo = this.getJdbcTemplate().queryForObject(sql, params, mapper);
return userInfo;
} catch (EmptyResultDataAccessException e) {
return null;
}
}
public MyUserAccount findByUserName(String userName) {
String sql = "Select id, email,user_name,first_name,last_name,"//
+ " password,role"//
+ " from User_Accounts u "//
+ " where user_name = ? ";
Object[] params = new Object[] { userName };
MyUserAccountMapper mapper = new MyUserAccountMapper();
try {
MyUserAccount userInfo = this.getJdbcTemplate().queryForObject(sql, params, mapper);
return userInfo;
} catch (EmptyResultDataAccessException e) {
return null;
}
}
public MyUserAccount registerNewUserAccount(MyUserAccountForm accountForm) {
String sql = "Insert into User_Accounts "//
+ " (id, email,user_name,first_name,last_name,password,role) "//
+ " values (?,?,?,?,?,?,?) ";
// Random string with 36 characters.
String id = UUID.randomUUID().toString();
this.getJdbcTemplate().update(sql, id, accountForm.getEmail(), //
accountForm.getUserName(), //
accountForm.getFirstName(), accountForm.getLastName(), //
accountForm.getPassword(), MyUserAccount.ROLE_USER);
return findById(id);
}
// Auto Create USER_ACCOUNTS.
public MyUserAccount createUserAccount(Connection<?> connection) {
ConnectionKey key = connection.getKey();
// (facebook,12345), (google,123) ...
System.out.println("key= (" + key.getProviderId() + "," + key.getProviderUserId() + ")");
UserProfile userProfile = connection.fetchUserProfile();
String email = userProfile.getEmail();
MyUserAccount account = this.findByEmail(email);
if (account != null) {
return account;
}
// Create User_Account.
String sql = "Insert into User_Accounts "//
+ " (id, email,user_name,first_name,last_name,password,role) "//
+ " values (?,?,?,?,?,?,?) ";
// Random string with 36 characters.
String id = UUID.randomUUID().toString();
String userName_prefix = userProfile.getFirstName().trim().toLowerCase()//
+"_"+ userProfile.getLastName().trim().toLowerCase();
String userName = this.findAvailableUserName(userName_prefix);
this.getJdbcTemplate().update(sql, id, email, userName, //
userProfile.getFirstName(), userProfile.getLastName(), //
"123", MyUserAccount.ROLE_USER);
return findById(id);
}
private String findAvailableUserName(String userName_prefix) {
MyUserAccount account = this.findByUserName(userName_prefix);
if (account == null) {
return userName_prefix;
}
int i = 0;
while (true) {
String userName = userName_prefix + "_" + i++;
account = this.findByUserName(userName);
if (account == null) {
return userName;
}
}
}
}
MySocialUserDetails is a class implements SocialUserDetails, note: SocialUserDetails is a interface extends UserDetails interface. The login via social networks using Spring Social Security API is essentially based on Spring Security API.
MySocialUserDetails.java
package org.o7planning.socialsecurity.user;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.o7planning.socialsecurity.model.MyUserAccount;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.social.security.SocialUserDetails;
public class MySocialUserDetails implements SocialUserDetails {
private static final long serialVersionUID = -5246117266247684905L;
private List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
private MyUserAccount myUserAccount;
public MySocialUserDetails(MyUserAccount myUserAccount) {
this.myUserAccount = myUserAccount;
String role = myUserAccount.getRole();
GrantedAuthority grant = new SimpleGrantedAuthority(role);
this.list.add(grant);
}
@Override
public String getUserId() {
return this.myUserAccount.getId();
}
@Override
public String getUsername() {
return myUserAccount.getUserName();
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return list;
}
@Override
public String getPassword() {
return myUserAccount.getPassword();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
MyUserDetailsService is a service is used by Spring Security API, to load user information from Database.
MyUserDetailsService.java
package org.o7planning.socialsecurity.service;
import org.o7planning.socialsecurity.dao.MyUserAccountDAO;
import org.o7planning.socialsecurity.model.MyUserAccount;
import org.o7planning.socialsecurity.user.MySocialUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.stereotype.Service;
// Service to Get user info from Database.
@Service
public class MyUserDetailsService implements UserDetailsService {
@Autowired
private MyUserAccountDAO myUserAccountDAO;
public MyUserDetailsService() {
}
// (This method is used by Spring Security API).
@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
MyUserAccount myUserAccount = myUserAccountDAO.findByUserName(userName);
if (myUserAccount == null) {
throw new UsernameNotFoundException("No user found with userName: " + userName);
}
// Note: SocialUserDetails extends UserDetails.
SocialUserDetails principal = new MySocialUserDetails(myUserAccount);
return principal;
}
}
SocialUserDetailsService service is used by Spring Social Security API, to load user information (login by social networks) from Database.
Spring Social Security API is an API based on the Spring Security API, it does not replace the Spring Security API.
MySocialUserDetailsService.java
package org.o7planning.socialsecurity.service;
import org.o7planning.socialsecurity.dao.MyUserAccountDAO;
import org.o7planning.socialsecurity.model.MyUserAccount;
import org.o7planning.socialsecurity.user.MySocialUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Service;
@Service
public class MySocialUserDetailsService implements SocialUserDetailsService {
@Autowired
private MyUserAccountDAO myUserAccountDAO;
// Loads the UserDetails by using the userID of the user.
// (This method Is used by Spring Security API).
@Override
public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException, DataAccessException {
MyUserAccount account= myUserAccountDAO.findById(userId);
MySocialUserDetails userDetails= new MySocialUserDetails(account);
return userDetails;
}
}
7. Classes for SignUp

As I mentioned above. After users logging in with social network, if the users' information do not already exist in the Dabase of application, it will be automatically created or redirected to a page allowing the users to enter account information.
In case you want to automatically create USER_ACCOUNTS & USERCONNECTION records, you must write class in order to implement interface ConnectionSignUp.
MyConnectionSignUp.java
package org.o7planning.socialsecurity.signup;
import org.o7planning.socialsecurity.dao.MyUserAccountDAO;
import org.o7planning.socialsecurity.model.MyUserAccount;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionSignUp;
public class MyConnectionSignUp implements ConnectionSignUp {
private MyUserAccountDAO myUserAccountDAO;
public MyConnectionSignUp(MyUserAccountDAO myUserAccountDAO) {
this.myUserAccountDAO = myUserAccountDAO;
}
// After login Social.
// This method is called to create a USER_ACCOUNTS record
// if it does not exists.
@Override
public String execute(Connection<?> connection) {
MyUserAccount account= myUserAccountDAO.createUserAccount(connection);
return account.getId();
}
}
..
MyUserAccountValidator.java
package org.o7planning.socialsecurity.validator;
import org.apache.commons.validator.routines.EmailValidator;
import org.o7planning.socialsecurity.dao.MyUserAccountDAO;
import org.o7planning.socialsecurity.form.MyUserAccountForm;
import org.o7planning.socialsecurity.model.MyUserAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
@Component
public class MyUserAccountValidator implements Validator {
// common-validator library.
private EmailValidator emailValidator = EmailValidator.getInstance();
@Autowired
private MyUserAccountDAO myUserAccountDAO;
@Override
public boolean supports(Class<?> clazz) {
return clazz == MyUserAccountForm.class;
}
@Override
public void validate(Object target, Errors errors) {
MyUserAccountForm form = (MyUserAccountForm) target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "", "Email is required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName", "", "User name is required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "", "First name is required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "", "Last name is required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "", "Password is required");
if (errors.hasErrors()) {
return;
}
if (!emailValidator.isValid(form.getEmail())) {
errors.rejectValue("email", "", "Email is not valid");
return;
}
MyUserAccount userAccount = myUserAccountDAO.findByUserName(form.getUserName());
if (userAccount != null) {
if (form.getId() == null) {
errors.rejectValue("userName", "", "User name is not available");
return;
} else if (!form.getId().equals(userAccount.getId())) {
errors.rejectValue("userName", "", "User name is not available");
return;
}
}
userAccount = myUserAccountDAO.findByEmail(form.getEmail());
if (userAccount != null) {
if (form.getId() == null) {
errors.rejectValue("email", "", "Email is not available");
return;
} else if (!form.getId().equals(userAccount.getId())) {
errors.rejectValue("email", "", "Email is not available");
return;
}
}
}
}
MyUserAccountForm.java
package org.o7planning.socialsecurity.form;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionKey;
import org.springframework.social.connect.UserProfile;
public class MyUserAccountForm {
private String id;
private String email;
private String userName;
private String firstName;
private String lastName;
private String password;
private String role;
private String signInProvider;
private String providerUserId;
public MyUserAccountForm() {
}
public MyUserAccountForm(Connection<?> connection) {
UserProfile socialUserProfile = connection.fetchUserProfile();
this.id = null;
this.email = socialUserProfile.getEmail();
this.userName = socialUserProfile.getUsername();
this.firstName = socialUserProfile.getFirstName();
this.lastName = socialUserProfile.getLastName();
ConnectionKey key = connection.getKey();
// google, facebook, twitter
this.signInProvider = key.getProviderId();
// ID of User on google, facebook, twitter.
// ID của User trên google, facebook, twitter.
this.providerUserId = key.getProviderUserId();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getSignInProvider() {
return signInProvider;
}
public void setSignInProvider(String signInProvider) {
this.signInProvider = signInProvider;
}
public String getProviderUserId() {
return providerUserId;
}
public void setProviderUserId(String providerUserId) {
this.providerUserId = providerUserId;
}
}
SecurityUtil.java
package org.o7planning.socialsecurity.util;
import org.o7planning.socialsecurity.model.MyUserAccount;
import org.o7planning.socialsecurity.user.MySocialUserDetails;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
public class SecurityUtil {
// Auto login.
public static void logInUser(MyUserAccount user) {
MySocialUserDetails userDetails = new MySocialUserDetails(user);
Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, null,
userDetails.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
}
}
8. Controller

MainController.java
package org.o7planning.socialsecurity.controller;
import org.o7planning.socialsecurity.dao.MyUserAccountDAO;
import org.o7planning.socialsecurity.form.MyUserAccountForm;
import org.o7planning.socialsecurity.model.MyUserAccount;
import org.o7planning.socialsecurity.util.SecurityUtil;
import org.o7planning.socialsecurity.validator.MyUserAccountValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@Controller
// Need to use RedirectAttributes
@EnableWebMvc
public class MainController {
@Autowired
private MyUserAccountDAO myUserAccountDAO;
@Autowired
private ConnectionFactoryLocator connectionFactoryLocator;
@Autowired
private UsersConnectionRepository connectionRepository;
@Autowired
private MyUserAccountValidator myUserAccountValidator;
// Set a form validator
@InitBinder
protected void initBinder(WebDataBinder dataBinder) {
Object target = dataBinder.getTarget();
if (target == null) {
return;
}
System.out.println("Target=" + target);
if (target.getClass() == MyUserAccountForm.class) {
dataBinder.setValidator(myUserAccountValidator);
}
}
@RequestMapping(value = { "/" }, method = RequestMethod.GET)
public String homePage(Model model) {
try {
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication()
.getPrincipal();
if (userDetails != null) {
System.out.println(userDetails.getPassword());
System.out.println(userDetails.getUsername());
System.out.println(userDetails.isEnabled());
model.addAttribute("userDetails", userDetails);
}
} catch (Exception e) {
}
return "index";
}
@RequestMapping(value = { "/login" }, method = RequestMethod.GET)
public String welcomePage(Model model) {
return "login";
}
// User login via Social,
// but not allow access basic info.
// webapp will redirect to /signin.
@RequestMapping(value = { "/signin" }, method = RequestMethod.GET)
public String signInPage(Model model) {
return "redirect:/login";
}
@RequestMapping(value = { "/signup" }, method = RequestMethod.GET)
public String signupPage(WebRequest request, Model model) {
ProviderSignInUtils providerSignInUtils //
= new ProviderSignInUtils(connectionFactoryLocator, connectionRepository);
Connection<?> connection = providerSignInUtils.getConnectionFromSession(request);
//
MyUserAccountForm myForm = null;
//
if (connection != null) {
myForm = new MyUserAccountForm(connection);
} else {
myForm = new MyUserAccountForm();
}
model.addAttribute("myForm", myForm);
return "signup";
}
@RequestMapping(value = { "/signup" }, method = RequestMethod.POST)
public String signupSave(WebRequest request, //
Model model, //
@ModelAttribute("myForm") @Validated MyUserAccountForm accountForm, //
BindingResult result, //
final RedirectAttributes redirectAttributes) {
// If validation has error.
if (result.hasErrors()) {
return "signup";
}
MyUserAccount registered = null;
try {
registered = myUserAccountDAO.registerNewUserAccount(accountForm);
} catch (Exception ex) {
ex.printStackTrace();
model.addAttribute("errorMessage", "Error " + ex.getMessage());
return "signup";
}
if (accountForm.getSignInProvider() != null) {
ProviderSignInUtils providerSignInUtils //
= new ProviderSignInUtils(connectionFactoryLocator, connectionRepository);
// If the user is signing in by using a social provider, this method
// call stores the connection to the UserConnection table.
// Otherwise, this method does not do anything.
providerSignInUtils.doPostSignUp(registered.getId(), request);
}
// After register, Logs the user in.
SecurityUtil.logInUser(registered);
return "redirect:/userInfo";
}
@RequestMapping(value = { "/userInfo" }, method = RequestMethod.GET)
public String userInfoPage(WebRequest request, Model model) {
return "userInfo";
}
}
9. JSP Pages

_menu.jsp
<%@ taglib uri="http://www.springframework.org/security/tags"
prefix="security"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<div style="padding: 5px; border: 1px solid #ccc;">
<a href="${pageContext.request.contextPath}/">Home</a>
||
<a href="${pageContext.request.contextPath}/userInfo">User Info</a>
<security:authorize access="isAuthenticated()">
||
<a href="${pageContext.request.contextPath}/logout">Logout</a>
</security:authorize>
<security:authorize access="!isAuthenticated()">
||
<a href="${pageContext.request.contextPath}/login">Login</a>
</security:authorize>
<c:if test="${not empty pageContext.request.userPrincipal.name}">
||
<span>Welcome : ${pageContext.request.userPrincipal.name}</span>
<ul>
<c:forEach items="${userDetails.authorities}" var="auth">
<li>${auth.authority }</li>
</c:forEach>
</ul>
</c:if>
</div>
index.jsp
<%@ taglib uri="http://www.springframework.org/security/tags"
prefix="security"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<jsp:include page="_menu.jsp"/>
<h2>Home Page</h2>
<c:if test="${empty pageContext.request.userPrincipal.name}">
Click to 'User Info' to show user info (It will redirect to login page)
</c:if>
</body>
</html>
login.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<jsp:include page="_menu.jsp" />
<h2>Social Login</h2>
<a href="${pageContext.request.contextPath}/auth/facebook">Face
Book</a>
<br />
<a href="${pageContext.request.contextPath}/auth/google">Google</a>
<br />
<h2>Normal Login</h2>
<c:if test="${param.error == 'true'}">
<div style="color:red;margin:10px 0px;">
Login Failed!!!<br />
Reason : ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
</div>
</c:if>
<form action="${pageContext.request.contextPath}/j_spring_security_check" method='POST'>
<table>
<tr>
<td>User:</td>
<td><input type='text' name='username' value=''></td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='password' /></td>
</tr>
<tr>
<td><input name="submit" type="submit" value="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
signup.jsp
<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="sec"
uri="http://www.springframework.org/security/tags"%>
<html>
<head>
<meta charset="UTF-8">
<title>Sign Up</title>
<style>
.error-message {
font-size:90%;
font-style:italic;
color:red;
}
</style>
</head>
<body>
<jsp:include page="_menu.jsp" />
<h1>Register</h1>
<c:if test="${not empty myForm.signInProvider}">
<h2 style="color:blue;">Signup with ${myForm.signInProvider}</h2>
</c:if>
<form:form modelAttribute="myForm" method="POST">
<form:hidden path="id" />
<form:hidden path="signInProvider" />
<table border="0">
<tr>
<td>User Name</td>
<td><form:input path="userName" /></td>
<td><form:errors path="userName" class="error-message" /></td>
</tr>
<tr>
<td>Email</td>
<td><form:input path="email" /></td>
<td><form:errors path="email" class="error-message" /></td>
</tr>
<tr>
<td>First Name</td>
<td><form:input path="firstName" /></td>
<td><form:errors path="firstName" class="error-message" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><form:input path="lastName" /></td>
<td><form:errors path="lastName" class="error-message" /></td>
</tr>
<tr>
<td>Password</td>
<td><form:input path="password" /></td>
<td><form:errors path="password" class="error-message" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
<td></td>
</tr>
</table>
</form:form>
<div class="error-message">${errorMessage}</div>
</body>
</html>
userInfo.jsp
<%@ taglib uri="http://www.springframework.org/security/tags"
prefix="security"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta charset="UTF-8">
<title>User Info</title>
</head>
<body>
<jsp:include page="_menu.jsp" />
<h1>User Info Page</h1>
<h3>Welcome : ${pageContext.request.userPrincipal.name}</h3>
<a href="${pageContext.request.contextPath}/logout">Logout</a>
</body>
</html>
10. Running application



Note: In order to test the application, you can delete the entire User data in Database as well as delete the Cache of browser.Delete from User_Accounts; Delete From Userconnection;
Automatically create an account:
In case you want users to login with social network, the USERCONNECTION & USER_ACCOUNTS records will be automatically created. You need to edit class SocialConfig:
** SocialConfig.java **
@Configuration
@EnableSocial
// Load to Environment.
@PropertySource("classpath:social-cfg.properties")
public class SocialConfig implements SocialConfigurer {
// .....
private boolean autoSignUp = true;
.......
}

Show registration page
In case, when users login through social networks, if no corresponding USERCONNECTION record, the application will redirect to the registration page. You need to edit SocialConfig class:
** SocialConfig.java **
@Configuration
@EnableSocial
// Load to Environment.
@PropertySource("classpath:social-cfg.properties")
public class SocialConfig implements SocialConfigurer {
// .....
private boolean autoSignUp = false;
.......
}

Spring MVC Tutorials
- Spring Tutorial for Beginners
- Install Spring Tool Suite for Eclipse
- Spring MVC Tutorial for Beginners - Hello Spring 4 MVC
- Configure Static Resources in Spring MVC
- Spring MVC Interceptors Tutorial with Examples
- Create a Multiple Languages web application with Spring MVC
- Spring MVC File Upload Tutorial with Examples
- Simple Login Java Web Application using Spring MVC, Spring Security and Spring JDBC
- Spring MVC Security with Hibernate Tutorial with Examples
- Spring MVC Security and Spring JDBC Tutorial (XML Config)
- Social Login in Spring MVC with Spring Social Security
- Spring MVC and Velocity Tutorial with Examples
- Spring MVC and FreeMarker Tutorial with Examples
- Use Template in Spring MVC with Apache Tiles
- Spring MVC and Spring JDBC Transaction Tutorial with Examples
- Use Multiple DataSources in Spring MVC
- Spring MVC and Hibernate Transaction Tutorial with Examples
- Spring MVC Form Handling and Hibernate Tutorial with Examples
- Run background scheduled tasks in Spring
- Create a Java Shopping Cart Web Application using Spring MVC and Hibernate
- Simple CRUD example with Spring MVC RESTful Web Service
- Deploy Spring MVC on Oracle WebLogic Server
Show More