## this step by step guide is based on the following excellent article:
http://bsgdev.wordpress.com/2011/01/18/exploring-google-and-openid-login-with-spring-security-and-spring-roo/
# Project
project --topLevelPackage za.co.bsg.rnd.trf.openidlogin
# Persistence
persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY --databaseName openidlogin --hostName localhost
# Dependencies
dependency add --groupId org.springframework.security --artifactId spring-security-openid --version 3.0.5.RELEASE
dependency add --groupId org.openid4java --artifactId openid4java --version 0.9.5
# Domain model
enum type --class ~.domain.UserRole
enum constant --name ROLE_ADMIN
enum constant --name ROLE_USER
enum type --class ~.domain.EmployeeStatus
enum constant --name ACTIVE
enum constant --name DORMANT
enum constant --name RESIGNED
enum constant --name TERMINATED
entity --class ~.domain.Employee --table employee --identifierColumn employee_id
field string --fieldName username --column username --notNull
field string --fieldName password --column password --notNull
field enum --type ~.domain.UserRole --fieldName userRole --notNull --enumType STRING
field string --fieldName openIdIdentifier --column openid_identifier
field string --fieldName firstName --column first_name
field string --fieldName lastName --column last_name
field string --fieldName emailAddress --column email_address
field enum --type ~.domain.EmployeeStatus --fieldName status --notNull --enumType STRING
# we will also need a finder
finder add --finderName findEmployeesByOpenIdIdentifier --class ~.domain.Employee
# Scaffold the web frontend
controller all --package ~.web
# Spring Security
security setup
# Other classes
class --class ~.InsertTestData
class --class ~.OpenIdUserDetailsService
Now implement these two java classes InsertTestData.java simply populates an initial db with data.
OpenIdUserDetailsService is our implementation of spring UserDetailsService that uses our finder to query the db using openid and return the right employee which we've augmented to be a fully fledged UserDetails Object by implementing that interface.
@Component
@Configurable
{
public static final String PASSWORD = "password";
@Override
{
init();
}
{
if (!Employee.findAllEmployees().isEmpty()) {
return;
}
Employee employeeAdminActive = new Employee();
employeeAdminActive.setUsername("user1");
employeeAdminActive.setPassword(hexSha256(PASSWORD));
employeeAdminActive.setUserRole(UserRole.ROLE_ADMIN);
employeeAdminActive.setStatus(EmployeeStatus.ACTIVE);
employeeAdminActive.setOpenIdIdentifier("https://you.myopenid.com/");
employeeAdminActive.persist();
Employee employee2 = new Employee();
employee2.setUsername("user2");
employee2.setPassword(hexSha256(PASSWORD));
employee2.setUserRole(UserRole.ROLE_USER);
employee2.setFirstName("Peter");
employee2.setLastName("Jones");
employee2.setStatus(EmployeeStatus.ACTIVE);
employee2.setOpenIdIdentifier("https://www.google.com/accounts/o8/id?id=your_id_goes_here");
employee2.persist();
Employee employee3 = new Employee();
employee3.setUsername("user3");
employee3.setPassword(hexSha256(PASSWORD));
employee3.setUserRole(UserRole.ROLE_USER);
employee3.setFirstName("Christina");
employee3.setLastName("Applegate");
employee3.setStatus(EmployeeStatus.RESIGNED);
employee3.persist();
}
{
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-256");
md.update(password2.getBytes());
byte byteData[] = md.digest();
return new String(Hex.encodeHex(byteData));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return password2;
}
}
{
{
List<employee> employeeList =
Employee.findEmployeesByOpenIdIdentifier(openIdIdentifier).getResultList();
Employee employee = employeeList.size() == 0 ? null : employeeList.get(0);
if (employee == null) {
throw new UsernameNotFoundException("User not found for OpenID: " + openIdIdentifier);
} else {
if (!employee.isEnabled()) {
throw new DisabledException("User is disabled");
}
return employee;
}
}
}
/*Now make sure Employee.java implements the UserDetails Interface.
public class Employee implements UserDetails
and add the following interface method implementations.
(you need to leave roo running to correct the aspect j getters for username and password.)
*/
@Override
{
return this.username;
}
@Override
{
return this.password;
}
@Override
{
return true;
}
@Override
{
return true;
}
@Override
{
return true;
}
@Override
{
return this.status == EmployeeStatus.ACTIVE;
}
@Override
{
Collection grantedAuthorities = new HashSet();
grantedAuthorities.add(
new GrantedAuthorityImpl(this.userRole.name()));
return grantedAuthorities;
}
in applicationContext-securtity
change
to
and change
to
after add:
add the google and open id form actions to the autogenerated login.jspx after
OpenID
## Just change you.myopenid.com to whatever provider you prefer for that user in the insert test data method and you are good to go.
No comments:
Post a Comment