Created
February 9, 2013 14:47
-
-
Save Kenji-H/4745524 to your computer and use it in GitHub Desktop.
openid4java with Struts DispatchAction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.http.HttpSession; | |
import org.apache.struts.action.ActionForm; | |
import org.apache.struts.action.ActionForward; | |
import org.apache.struts.action.ActionMapping; | |
import org.apache.struts.action.ActionMessage; | |
import org.apache.struts.action.ActionMessages; | |
import org.apache.struts.actions.DispatchAction; | |
import org.openid4java.consumer.ConsumerManager; | |
import org.openid4java.consumer.VerificationResult; | |
import org.openid4java.discovery.DiscoveryInformation; | |
import org.openid4java.discovery.Identifier; | |
import org.openid4java.message.AuthRequest; | |
import org.openid4java.message.ParameterList; | |
public class AuthAction extends DispatchAction { | |
private ConsumerManager manager; | |
private static final String RETURN_URL = "http://localhost:8080/OpenId/auth.do?event=verify"; | |
public AuthAction() { | |
manager = new ConsumerManager(); | |
} | |
@SuppressWarnings("rawtypes") | |
public ActionForward redirect(ActionMapping mapping, ActionForm form, | |
HttpServletRequest request, HttpServletResponse response) | |
throws Exception { | |
OpenIdInfoForm f = (OpenIdInfoForm) form; | |
String provider = f.getProvider(); | |
List discoveries = manager.discover(provider); | |
DiscoveryInformation discovered = manager.associate(discoveries); | |
HttpSession session = request.getSession(); | |
session.setAttribute("discovered", discovered); | |
AuthRequest authReq = manager.authenticate(discovered, RETURN_URL); | |
response.sendRedirect(authReq.getDestinationUrl(true)); | |
return null; | |
} | |
public ActionForward verify(ActionMapping mapping, ActionForm form, | |
HttpServletRequest request, HttpServletResponse response) | |
throws Exception { | |
ParameterList openidResp = new ParameterList(request.getParameterMap()); | |
HttpSession session = request.getSession(); | |
DiscoveryInformation discovered = (DiscoveryInformation) session | |
.getAttribute("discovered"); | |
StringBuffer receivingURL = request.getRequestURL(); | |
String queryString = request.getQueryString(); | |
if (queryString != null && queryString.length() > 0) | |
receivingURL.append("?").append(request.getQueryString()); | |
VerificationResult verification = manager.verify( | |
receivingURL.toString(), openidResp, discovered); | |
Identifier verified = verification.getVerifiedId(); | |
if (verified == null) { | |
ActionMessages errors = new ActionMessages(); | |
errors.add("authError", new ActionMessage("errors.authError")); | |
saveErrors(request, errors); | |
return mapping.findForward("failure"); | |
} | |
OpenIdInfoForm f = (OpenIdInfoForm)form; | |
f.setId(verified.getIdentifier()); | |
return mapping.findForward("success"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> | |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
<html:html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<title>Login Page</title> | |
</head> | |
<body> | |
<html:errors/> | |
Select An OpenID Provider: | |
<html:form action="/auth"> | |
<html:select property="provider"> | |
<html:option value="https://www.myopenid.com/">MyOpenID</html:option> | |
<html:option value="https://www.google.com/accounts/o8/id">Google</html:option> | |
<html:option value="https://me.yahoo.com">Yahoo</html:option> | |
</html:select> | |
<html:hidden property="event" value="redirect"/> | |
<html:submit value="login"/> | |
</html:form> | |
</body> | |
</html:html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
errors.header=<font color="red"> | |
errors.prefix= | |
errors.suffix=<br/> | |
errors.footer=</font><br/> | |
errors.authError=OpenID authentication failed. Try again. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.struts.action.ActionForm; | |
public class OpenIdInfoForm extends ActionForm { | |
private static final long serialVersionUID = -3445459939434016171L; | |
public String id; | |
public String provider; | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getProvider() { | |
return provider; | |
} | |
public void setProvider(String provider) { | |
this.provider = provider; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="ISO-8859-1" ?> | |
<!DOCTYPE struts-config PUBLIC | |
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" | |
"http://struts.apache.org/dtds/struts-config_1_3.dtd"> | |
<struts-config> | |
<form-beans> | |
<form-bean name="openIdInfo" type="OpenIdInfoForm" /> | |
</form-beans> | |
<action-mappings> | |
<action path="/auth" type="AuthAction" name="openIdInfo" | |
scope="session" parameter="event"> | |
<forward name="success" path="/pages/welcome.jsp"/> | |
<forward name="failure" path="/pages/login.jsp"/> | |
</action> | |
</action-mappings> | |
<message-resources parameter="MessageResources" /> | |
</struts-config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> | |
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> | |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
<html:html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<title>Welcome Page</title> | |
</head> | |
<body> | |
Welcome!<br/> | |
Your Open ID = <bean:write name="openIdInfo" property="id"/> | |
</body> | |
</html:html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment