셋팅이 완료되었으니 이제 controller를 만들어봅시다.
src폴더에 openidtest.controller라는 package를 만듭시다.
그리고, OpenIDController클래스를 생성합니다.
OpendIDController.java
[code]
package openidtest.controller;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.openid4java.OpenIDException;
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;
import org.openid4java.server.RealmVerifier;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
public class OpenIDController
{
private ConsumerManager manager;
@RequestMapping(value="/index.do", method=RequestMethod.GET)
public String indexGetcontroller(ModelMap model)
{
return "index";
}
@SuppressWarnings("unchecked")
@RequestMapping(value="/index.do", method=RequestMethod.POST)
public String indexPostController(String openId, ModelMap model,
HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
try
{
manager = new ConsumerManager();
String returnToUrl = "http://localhost:8080/OpenIdTest/verify.do";
List discoveries = manager.discover(openId);
if (discoveries.size() == 0)
{
model.addAttribute("noopenid", openId);
return "index";
}
DiscoveryInformation discovered = manager.associate(discoveries);
request.getSession().setAttribute("openid-disc", discovered);
RealmVerifier rv = new RealmVerifier();
rv.setEnforceRpId(false);
manager.setRealmVerifier(rv);
AuthRequest authReq = manager.authenticate(discovered, returnToUrl);
if (!discovered.isVersion2())
{
// Option 1: GET HTTP-redirect to the OpenID Provider endpoint
// The only method supported in OpenID 1.x
// redirect-URL usually limited ~2048 bytes
response.sendRedirect(authReq.getDestinationUrl(true));
return null;
} else {
// Option 2: HTML FORM Redirection (Allows payloads >2048 bytes)
// RequestDispatcher dispatcher =
// getServletContext().getRequestDispatcher("formredirection.jsp");
// httpReq.setAttribute("prameterMap",
// response.getParameterMap());
// httpReq.setAttribute("destinationUrl",
// response.getDestinationUrl(false));
// dispatcher.forward(request, response);
}
}
catch (OpenIDException e)
{
}
return null;
//model.addAttribute("openId", openId);
//return "index";
}
@RequestMapping(value="/verify.do", method=RequestMethod.GET)
public String verifyController(String openId, ModelMap model,
HttpServletRequest request,
HttpServletResponse response) throws ServletException
{
try
{
ParameterList paramList = new ParameterList(request.getParameterMap());
DiscoveryInformation discovered = (DiscoveryInformation) request
.getSession().getAttribute("openid-disc");
// extract the receiving URL from the HTTP request
StringBuffer receivingURL = request.getRequestURL();
String queryString = request.getQueryString();
if (queryString != null && queryString.length() > 0)
receivingURL.append("?").append(request.getQueryString());
// verify the response; ConsumerManager needs to be the same
// (static) instance used to place the authentication request
VerificationResult verification = manager.verify(receivingURL.toString(),
paramList, discovered);
// examine the verification result and extract the verified
// identifier
Identifier verified = verification.getVerifiedId();
if (verified != null)
{
request.getSession()
.setAttribute("openid", verified.getIdentifier());
}
}
catch (OpenIDException e)
{
}
return "redirect:index.do";
}
@RequestMapping(value="/logout.do", method=RequestMethod.POST)
public String logoutController(String openId, ModelMap model,
HttpServletRequest request,
HttpServletResponse response) throws ServletException
{
request.getSession().removeAttribute("openid");
return "redirect:index.do";
}
}
[/code]
WEB-IINF/jsp/index.jsp
[code]
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>오픈아이디 테스트</title>
</head>
<body>
<c:choose>
<c:when test="${sessionScope.openid != null}">
${sessionScope.openid}님 환영합니다.
<form action="logout.do" method="POST">
<input type="submit" value="로그아웃"/>
</form>
</c:when>
<c:otherwise>
<form action="index.do" method="POST">
<input type="text" id="openId" name="openId"/>
<input type="submit" value="로그인"/>
</form>
</c:otherwise>
</c:choose>
<c:if test="${noopenid != null}">
${noopenid}는 없는 아이디입니다.
</c:if>
</body>
</html>
[/code]
view페이지에서 아이디를 치고, post요청을 하게 되면 indexPostController메소드가 호출이 됩니다. 여기서는 인증할 수 있는 URL을 redirect하게 됩니다. 그러면 OpenID를 제공하는 Provider에서 인증을 받고, returnURL로 이동을 해서 인증이 되었는지 확인 후 인증이 되면
Identifier verified = verification.getVerifiedId();
에서 Identifier 객체를 받을 수 있는데요. 이곳에서 오픈아이디를 구할 수 있습니다.
아 졸려-_-
src폴더에 openidtest.controller라는 package를 만듭시다.
그리고, OpenIDController클래스를 생성합니다.
OpendIDController.java
[code]
package openidtest.controller;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.openid4java.OpenIDException;
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;
import org.openid4java.server.RealmVerifier;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
public class OpenIDController
{
private ConsumerManager manager;
@RequestMapping(value="/index.do", method=RequestMethod.GET)
public String indexGetcontroller(ModelMap model)
{
return "index";
}
@SuppressWarnings("unchecked")
@RequestMapping(value="/index.do", method=RequestMethod.POST)
public String indexPostController(String openId, ModelMap model,
HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
try
{
manager = new ConsumerManager();
String returnToUrl = "http://localhost:8080/OpenIdTest/verify.do";
List discoveries = manager.discover(openId);
if (discoveries.size() == 0)
{
model.addAttribute("noopenid", openId);
return "index";
}
DiscoveryInformation discovered = manager.associate(discoveries);
request.getSession().setAttribute("openid-disc", discovered);
RealmVerifier rv = new RealmVerifier();
rv.setEnforceRpId(false);
manager.setRealmVerifier(rv);
AuthRequest authReq = manager.authenticate(discovered, returnToUrl);
if (!discovered.isVersion2())
{
// Option 1: GET HTTP-redirect to the OpenID Provider endpoint
// The only method supported in OpenID 1.x
// redirect-URL usually limited ~2048 bytes
response.sendRedirect(authReq.getDestinationUrl(true));
return null;
} else {
// Option 2: HTML FORM Redirection (Allows payloads >2048 bytes)
// RequestDispatcher dispatcher =
// getServletContext().getRequestDispatcher("formredirection.jsp");
// httpReq.setAttribute("prameterMap",
// response.getParameterMap());
// httpReq.setAttribute("destinationUrl",
// response.getDestinationUrl(false));
// dispatcher.forward(request, response);
}
}
catch (OpenIDException e)
{
}
return null;
//model.addAttribute("openId", openId);
//return "index";
}
@RequestMapping(value="/verify.do", method=RequestMethod.GET)
public String verifyController(String openId, ModelMap model,
HttpServletRequest request,
HttpServletResponse response) throws ServletException
{
try
{
ParameterList paramList = new ParameterList(request.getParameterMap());
DiscoveryInformation discovered = (DiscoveryInformation) request
.getSession().getAttribute("openid-disc");
// extract the receiving URL from the HTTP request
StringBuffer receivingURL = request.getRequestURL();
String queryString = request.getQueryString();
if (queryString != null && queryString.length() > 0)
receivingURL.append("?").append(request.getQueryString());
// verify the response; ConsumerManager needs to be the same
// (static) instance used to place the authentication request
VerificationResult verification = manager.verify(receivingURL.toString(),
paramList, discovered);
// examine the verification result and extract the verified
// identifier
Identifier verified = verification.getVerifiedId();
if (verified != null)
{
request.getSession()
.setAttribute("openid", verified.getIdentifier());
}
}
catch (OpenIDException e)
{
}
return "redirect:index.do";
}
@RequestMapping(value="/logout.do", method=RequestMethod.POST)
public String logoutController(String openId, ModelMap model,
HttpServletRequest request,
HttpServletResponse response) throws ServletException
{
request.getSession().removeAttribute("openid");
return "redirect:index.do";
}
}
[/code]
WEB-IINF/jsp/index.jsp
[code]
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>오픈아이디 테스트</title>
</head>
<body>
<c:choose>
<c:when test="${sessionScope.openid != null}">
${sessionScope.openid}님 환영합니다.
<form action="logout.do" method="POST">
<input type="submit" value="로그아웃"/>
</form>
</c:when>
<c:otherwise>
<form action="index.do" method="POST">
<input type="text" id="openId" name="openId"/>
<input type="submit" value="로그인"/>
</form>
</c:otherwise>
</c:choose>
<c:if test="${noopenid != null}">
${noopenid}는 없는 아이디입니다.
</c:if>
</body>
</html>
[/code]
view페이지에서 아이디를 치고, post요청을 하게 되면 indexPostController메소드가 호출이 됩니다. 여기서는 인증할 수 있는 URL을 redirect하게 됩니다. 그러면 OpenID를 제공하는 Provider에서 인증을 받고, returnURL로 이동을 해서 인증이 되었는지 확인 후 인증이 되면
Identifier verified = verification.getVerifiedId();
에서 Identifier 객체를 받을 수 있는데요. 이곳에서 오픈아이디를 구할 수 있습니다.
아 졸려-_-