//現在はグーグルのみで動作しています//これを試してください-これはすべて1つです。
インポートjava.io.IOException;
インポートjava.net.MalformedURLException;
java.net.URLをインポートします。
インポートjava.util.List;
インポートjavax.servlet.ServletConfig;
インポートjavax.servlet.ServletContext;
インポートjavax.servlet.ServletException;
インポートjavax.servlet.http.HttpServletRequest;
インポートjavax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
// import org.jboss.web.tomcat.security.login.WebAuthentication;
import org.openid4java.OpenIDException;
import org.openid4java.consumer.ConsumerException;
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.AuthSuccess;
import org.openid4java.message.ParameterList;
import org.openid4java.message.ax.AxMessage;
import org.openid4java.message.ax.FetchRequest;
import org.openid4java.message.ax.FetchResponse;
パブリッククラスOpenAuthはjavax.servlet.http.HttpServlet{を拡張します
最終的な静的文字列YAHOO_ENDPOINT="https://me.yahoo.com";
最終的な静的文字列GOOGLE_ENDPOINT="https://www.google.com/
アカウント/o8/ id ";
//サンプルコードの更新バージョン:
https://crisdev.wordpress.com/2011/03/23/openid4java-login-example/
//ここにサーブレットスクリプトパスを追加します-したがって、認証が失敗した場合、または
成功すると、アクションが実行されます-以下のdoGetで確認してください
public String scr = "/ servlets / MyServlet";
プライベートServletContextコンテキスト。
プライベートConsumerManagerマネージャー。
プライベートConsumerManagermag;
//VahidHedayatiによって更新されたコードhttp://pro.org.uk
//configinitを削除しました-投稿をdoGetに移動しました-前のコード以降
投稿である必要がありますが、の一部として識別子を含める必要もあります
url
//識別子も識別子コードに使用されたものと同じ変数でした-
//クリーンアップして別の変数を作成し、混乱を減らします
// doGet identiferがopenid_identifierに変更され、見た目も変わりました
から返されるデフォルトの変数であるopenid_usernameの場合
openid-selector
//http://groups.google.com/group/openid4java/browse_thread/thread/
5e8f24f51f54dc2c
//上記の投稿を読んだ後-マネージャーをセッションオブジェクトに保存します
Yahoo認証で失敗しましたマネージャーのコードを変更しました
//管理
public void doPost(HttpServletRequest req、HttpServletResponse
応答)は、ServletException、IOException{をスローします
doGet(req、response);
}
保護されたvoiddoGet(HttpServletRequest req、HttpServletResponse
resp)は、ServletException、IOException{をスローします
//新しい変数
String ouser =(String)req.getParameter( "openid_username");
if(ouser == null){ouser = "";}
// Mageは、openid_consumer_managerのセッション値です。
nullそれは一度それを生成します
//そして、コード内でマネージャーが呼び出されると、最初に戻ります
マネージャーはセッション値を調べることで価値を評価します
mag =(ConsumerManager)req.getSession()。getAttribute( "open_id_consumer_manager");
if(mag == null){
this.manager = new ConsumerManager();
req.getSession()。setAttribute( "open_id_consumer_manager"、manager);
}
文字列identify=(String)req.getParameter( "openid_identifier");
if(identify == null){identify = "";}
if(!identify.equals( "")){
this.authRequest(identify、ouser、req、resp);
}そうしないと{
//彼らが成功した場合、それは彼らを歓迎するために返します
//以下のセッション値でNEWUSER=yesの場合、ウェルカムルックアップ
そしてそうならば
//scrにipcity/ country / postcodeがあるので、ファイナライズします
サインアップとしてユーザーipcountry/ city/ipを追加することによるユーザー追加
//新しくない場合は、すでにログインしています
このコードが記録に入れた関連セッション値と
彼らに私のアカウントを返します
//ここでの認証が失敗した場合、または共有を拒否した場合
メール、ログインページが返されます
識別子識別子=this.verifyResponse(req);
if(識別子!= null){
resp.sendRedirect(scr + "?act = welcome");
} そうしないと {
resp.sendRedirect(scr + "?act = login");
}
}
}
//---認証リクエストを送信---
public String authRequest(String userSuppliedString、String Ouser、
HttpServletRequest httpReq、HttpServletResponse httpResp)がスローします
IOException {
試す {
//アプリケーションが配置するreturn_toURLを構成します
受け取る
//OpenIDプロバイダーからの認証応答
文字列returnToUrl=httpReq.getRequestURL()。toString();
// ---プロキシ設定を転送します(必要な場合のみ)---
// ProxyProperties proxyProps = new ProxyProperties();
// proxyProps.setProxyName( "proxy.example.com");
// proxyProps.setProxyPort(8080);
// HttpClientFactory.setProxyProperties(proxyProps);
//ユーザー指定の識別子で検出を実行します
//変更-セッションからマネージャー値を検索
manager =(ConsumerManager)
httpReq.getSession()。getAttribute( "open_id_consumer_manager");
発見のリスト=manager.discover(userSuppliedString);
//OpenIDプロバイダーとの関連付けを試みます
//認証用に1つのサービスエンドポイントを取得します
発見されたDiscoveryInformation=
manager.associate(discoveries);
//ユーザーのセッションに検出情報を保存します
httpReq.getSession()。setAttribute( "openid-disc"、discovered);
//OpenIDに送信されるAuthRequestメッセージを取得します
プロバイダー
AuthRequest authReq = manager.authenticate(discovered、
returnToUrl);
FetchRequest fetch = FetchRequest.createFetchRequest();
if(userSuppliedString.startsWith(GOOGLE_ENDPOINT)){
fetch.addAttribute( "email"、 "http://axschema.org/
連絡先/メール"、true);
fetch.addAttribute( "firstName"、 "http://axschema.org/
namePerson / first "、true);
fetch.addAttribute( "lastName"、 "http://axschema.org/
namePerson / last "、true);
} else if(userSuppliedString.startsWith(YAHOO_ENDPOINT)){
fetch.addAttribute( "email"、 "http://axschema.org/
連絡先/メール"、true);
fetch.addAttribute( "fullname"、 "http://axschema.org/
namePerson "、true);
} そうしないと {
//myOpenIDで機能します
fetch.addAttribute( "fullname"、 "http://
schema.openid.net/namePerson "、true);
fetch.addAttribute( "email"、 "http://schema.openid.net/
連絡先/メール"、true);
}
httpReq.getSession()。setAttribute( "Ouser"、Ouser);
//認証リクエストに拡張機能を添付します
authReq.addExtension(fetch);
httpResp.sendRedirect(authReq.getDestinationUrl(true));
} catch(OpenIDException e){
//ユーザーにエラーを表示します
}
nullを返します。
}
//---認証応答の処理---
パブリック識別子verifyResponse(HttpServletRequest httpReq){
試す {
//認証応答からパラメータを抽出します
//(OpenIDプロバイダーからのHTTPリクエストとして送信されます)
ParameterList response = new
ParameterList(httpReq.getParameterMap());
//以前に保存された検出情報を取得します
発見されたDiscoveryInformation=(DiscoveryInformation)
httpReq.getSession()。getAttribute( "openid-disc");
//HTTPリクエストから受信URLを抽出します
StringBuffer receiveURL = httpReq.getRequestURL();
文字列queryString=httpReq.getQueryString();
if(queryString!= null && queryString.length()> 0)
receiveURL.append( "?")。append(httpReq.getQueryString());
//応答を確認します; ConsumerManagerは同じである必要があります
//(静的)認証リクエストを送信するために使用されるインスタンス
//変更済み-検証を実行する前にセッション値を検索します
結果
manager =(ConsumerManager)
httpReq.getSession()。getAttribute( "open_id_consumer_manager");
VerificationResult検証=
manager.verify(receiveingURL.toString()、response、discovered);
//検証結果を調べ、検証済みを抽出します
//識別子
識別子検証済み=verification.getVerifiedId();
文字列id=verified.getIdentifier();
if(verified!= null){
AuthSuccess authSuccess =(AuthSuccess)
verify.getAuthResponse();
if(authSuccess.hasExtension(AxMessage.OPENID_NS_AX)){
FetchResponse fetchResp =(FetchResponse)
authSuccess.getExtension(AxMessage.OPENID_NS_AX);
メールのリスト=
fetchResp.getAttributeValues( "email");
文字列email=(String)emails.get(0);
////////////////////////////////////////////////// //////////////////////////////
//各人が実装する必要のあるカスタムビット
アプリケーションと対話します。
//ユーザーを認証し、確認メールを送信します
ユーザーはローカルシステムに存在します
//もしそうなら{
//
httpReq.getSession()。setAttribute( "USERNAME"、usern);
httpReq.getSession()。setAttribute( "LOGGEDIN"、 "on");
//}そうしないと{
文字列firstName=
fetchResp.getAttributeValue( "firstName");
文字列lastName=
fetchResp.getAttributeValue( "lastName");
弦
fullname = fetchResp.getAttributeValue( "fullname");
if(fullname == null)
{fullname = "";}
if(firstName == null)
{firstName = "";}
if(lastName == null){lastName = "";}
if(!fullname.equals( "")){
if(fullname.indexOf( "、")>-1)
{{
firstName = fullname.substring(0、fullname.indexOf( "、"));
lastName = fullname.substring(fullname.indexOf( "、")、fullname.length());
} else if(fullname.indexOf( "
")>-1){
firstName = fullname.substring(0、fullname.indexOf( ""));
lastName = fullname.substring(fullname.indexOf( "")、fullname.length());
}
}
//これは返されたユーザー名です
ユーザー名を要求するさまざまなサービスから-次のように返されます
openid_username
// openid-selectorを使用する場合、
openid_identifierとopenid_username-これがこのプログラムの現在の内容です
探す
弦
ouser =(String)httpReq.getSession()。getValue( "Ouser");
if(ouser == null){ouser = "";}
//Adduser-メールアドレスを渡し、
ouser
//Adduserクラス内-ouserが空白の場合
メールを0からsubstring.indexOf( "@")に分割します
//乱数を生成します-ルックアップ
現在のユーザー-存在する場合は、最後に乱数を追加します
//そしてメールと新しいユーザーを追加します
ユーザー名
// bacに新しいユーザーを返し、ログインします
上記のように。
httpReq.getSession()。setAttribute( "NEWUSER"、 "YES");
//
httpReq.getSession()。setAttribute( "USERNAME"、usern);
httpReq.getSession()。setAttribute( "LOGGEDIN"、 "on");
//}
返品確認済み。// 成功
}
}
} catch(OpenIDException e){
//ユーザーにエラーを表示します
}
nullを返します。
}