Dealing with OpenId(4)Spring Security 3.1.M2

Dealing with OpenId(4)Spring Security 3.1.M2

Only configure openid in spring security.
1. Security configuration file security-context.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

















invalidate-session="true"
logout-success-url="/openidlogin.jsp"
logout-url="/j_spring_security_logout"/>
user-service-ref="registeringUserService"
login-page="/openidlogin.jsp"
authentication-failure-url="/openidlogin.jsp?login_error=true"
default-target-url="/index.jsp">





















is debug switch in my configuration file.

2. My openidlogin.jsp file:


Open ID Login





Your principal object is....: <%= request.getUserPrincipal() %>



Please Enter Your OpenID Identity









OpenID Identity:







Please Enter Your System User Name














User Name:
Password:





3. pom.xml dependency:

junit
junit
4.10
test


org.expressme
JOpenId
1.08


javax.servlet
servlet-api
2.5
provided


org.openid4java
openid4java-nodeps
0.9.6


org.springframework
spring-core
3.1.1.RELEASE


org.springframework
spring-context
3.1.1.RELEASE


org.springframework
spring-web
3.1.1.RELEASE


org.springframework
spring-webmvc
3.1.1.RELEASE


org.codehaus.groovy
groovy
2.0.0-beta-2


org.apache.velocity
velocity
1.7


org.apache.velocity
velocity-tools
2.0



org.springframework.security
spring-security-core
3.1.0.M2


org.springframework.security
spring-security-web
3.1.0.M2


org.springframework.security
spring-security-taglibs
3.1.0.M2


org.springframework.security
spring-security-config
3.1.0.M2


org.springframework.security
spring-security-acl
3.1.0.M2


org.springframework.security
spring-security-openid
3.1.0.M2


org.apache.httpcomponents
httpclient
4.2-beta1


log4j
log4j
1.2.16


...snip...

org.springframework.maven.milestone
Spring Maven Milestone Repository
http://maven.springframework.org/milestone

...snip...


easyopenidgoogle


org.mortbay.jetty
maven-jetty-plugin
6.1.10

10


8080
60000


/easyopenidgoogle





4. My javqa class are as follow:
package com.sillycat.easyopenidgoogle.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.openid.OpenIDAttribute;
import org.springframework.security.openid.OpenIDAuthenticationToken;

import com.sillycat.easyopenidgoogle.model.GoogleUser;
import com.sillycat.easyopenidgoogle.model.UserAuthority;
import com.sillycat.easyopenidgoogle.model.UserRole;

public class OpenIdUserDetailsService implements UserDetailsService,
AuthenticationUserDetailsService {

private final Map registeredUsers = new HashMap();

//private static final List DEFAULT_AUTHORITIES = AuthorityUtils
// .createAuthorityList("ROLE_USER");

public UserDetails loadUserDetails(OpenIDAuthenticationToken openIDToken)
throws UsernameNotFoundException {
String id = openIDToken.getIdentityUrl();
System.out.println("identy = " + id);
String email = null;
String firstName = null;
String lastName = null;
String fullName = null;
List attributes = openIDToken.getAttributes();
for (OpenIDAttribute attribute : attributes) {
if (attribute.getName().equals("email")) {
email = attribute.getValues().get(0);
System.out.println("email = " + email);
}
if (attribute.getName().equals("firstName")) {
firstName = attribute.getValues().get(0);
System.out.println("firstName = " + firstName);
}
if (attribute.getName().equals("lastName")) {
lastName = attribute.getValues().get(0);
System.out.println("lastName = " + lastName);
}
if (attribute.getName().equals("fullname")) {
fullName = attribute.getValues().get(0);
System.out.println("fullName = " + fullName);
}
}
GoogleUser user = new GoogleUser();
user.setUsername(email);

UserRole userRole = new UserRole();
UserAuthority userAuthority = new UserAuthority();
userAuthority.setAuthorityAlias("Access the main page!");
userAuthority.setAuthorityName("ROLE_USER");
userRole.getRoleAuthorities().add(userAuthority);
user.getUserRoles().add(userRole);

registeredUsers.put(id, user);
return user;
}

public UserDetails loadUserByUsername(String id)
throws UsernameNotFoundException {
UserDetails user = registeredUsers.get(id);

if (user == null) {
throw new UsernameNotFoundException(id);
}

return user;
}

}

references:
http://www.packtpub.com/article/opening-up-to-openid-with-spring-security
http://repo1.maven.org/maven2/org/springframework/security/spring-security-samples-openid/3.0.7.RELEASE/
http://bsgdev.wordpress.com/2011/01/18/exploring-google-and-openid-login-with-spring-security-and-spring-roo/
http://www.springsource.org/download/community
http://static.springsource.org/spring-security/site/docs/3.0.x/reference/sample-apps.html
https://fisheye.springsource.org/browse/spring-security/samples/openid
http://stackoverflow.com/questions/7309133/spring-security-with-openid-and-database-integration
http://krams915.blogspot.com/2011/02/spring-security-3-openid-login-with_13.html
http://forum.springsource.org/showthread.php?53230-Multiple-providers-with-AuthenticationManager
http://stackoverflow.com/questions/8306063/multiple-login-forms-different-authentication-managers-latest-spring-security
https://open.jira.com/svn/PEBBLE/trunk/src/main/webapp/WEB-INF/applicationContext-security.xml
http://forum.springsource.org/showthread.php?123956-Multiple-authentication-managers-Defined-But-Only-the-Last-One-Is-Applied
https://github.com/monger/Spring-Security-Multi-Auth-Manager-Test
http://lengyun3566.iteye.com/blog/1356124
https://open.jira.com/svn/PEBBLE/trunk/
http://my.safaribooksonline.com/book/programming/java/9781847199744/additional-reference-material/ch13lvl1sec08

你可能感兴趣的:(Summary)