tag:blogger.com,1999:blog-34354292801230925882024-03-13T23:53:37.026-07:00DNCodeFragsThis blog is created with an intention of sharing my knowledge as a Java enterprise web application developer. I only have few years of experience as a developer and in my short career,I'm grateful to those people who were kind enough to share their knowledge. So I thought to share some of my code fragments, which I found bit difficult to find in web. Hope these code fragments will help someone :)Anonymoushttp://www.blogger.com/profile/16789505062816668313noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-3435429280123092588.post-11192478694354813662012-06-17T06:13:00.002-07:002012-06-17T06:13:45.648-07:00How to create a simple Maven 2 web project with Eclipse<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirbS1xFDq5FzRhu7hYKTt32urItC8tmJ_5PdaN7pCVGpTUAIAdHgz-FIIgzUwb6TZQtZraXlpItQzD9eZYvRWco_4HTGwxNJI88xexPT9-iCFvZQzfHjuXiJiuC1ABuaxUMZifC9saCMap/s1600/eclipse_IDE.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="130" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirbS1xFDq5FzRhu7hYKTt32urItC8tmJ_5PdaN7pCVGpTUAIAdHgz-FIIgzUwb6TZQtZraXlpItQzD9eZYvRWco_4HTGwxNJI88xexPT9-iCFvZQzfHjuXiJiuC1ABuaxUMZifC9saCMap/s200/eclipse_IDE.jpg" width="200" /></a></div>
<br />
Here we will have look at how to create a Maven 2 Web application using "Eclipse IDE for Java EE Developers"<br />
<br />
If you don't have the IDE, it can be downloaded from <a href="http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/heliosr">here </a>for free.<br />
<br />
<a href="http://maven.apache.org/what-is-maven.html">Maven</a> is a tool that can be used for building and managing any Java-based project. Here we will consider only about how to create a Web Application. <br />
<br />
Below are the steps to create the web application,<br />
<br />
Step 1: Right click on project explorer and select New-->Other<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsDgOGwqvQeg66YuODUqAmafBEqzbVSKTKMVSBCBKxwrchzjiTzTalFiQYvaNk6DWrAc1-0ZZcntPQEUG6SLkHz-or5aeaM5mEGW-Rg1x_As6KIoeZKl-PbjEPRtr0XFnBNjbUqZBsqIhS/s1600/new_select.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="226" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsDgOGwqvQeg66YuODUqAmafBEqzbVSKTKMVSBCBKxwrchzjiTzTalFiQYvaNk6DWrAc1-0ZZcntPQEUG6SLkHz-or5aeaM5mEGW-Rg1x_As6KIoeZKl-PbjEPRtr0XFnBNjbUqZBsqIhS/s320/new_select.jpg" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Step 2: Type "maven" in the text box available, select "Maven 2 Project Creation Wizard", and click "Next"</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGIU-vbuoU5Rr-orFlIrGnY_YgkFKnFSwLibgCBDR_RwNBjZNncX_mumzlpR_Kmof_kxy8PpFXWL8IXuaCzcebaFAW_AOGvi1nFLfvOD6rmNgncxpg_9RPxxgvSztls7K36mVwKDa4mWs8/s1600/new_select_wizard.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="304" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGIU-vbuoU5Rr-orFlIrGnY_YgkFKnFSwLibgCBDR_RwNBjZNncX_mumzlpR_Kmof_kxy8PpFXWL8IXuaCzcebaFAW_AOGvi1nFLfvOD6rmNgncxpg_9RPxxgvSztls7K36mVwKDa4mWs8/s320/new_select_wizard.jpg" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
If you don't have "Maven 2 Project" here, it means you will need to add Maven plugin to eclipse. Find here how to do it:-<a href="http://www.dotkam.com/2008/02/24/install-maven-plugin-for-eclipse/">http://www.dotkam.com/2008/02/24/install-maven-plugin-for-eclipse/</a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Step 3: Then, in the new window, type an appropriate name for the project and click "Next"</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKFLZV4wf-EkJvWKloRbtZo49EmwdZbLAzaFMKrMkbJju36YBqnxggLNEil2HNXPN21dUYYMUS1eJP-BznMdXzo2-w-VNwLqzYkYd0E74w9BVy_jNPkY9B62IQs8zTUhdbI_XA4mLknrjl/s1600/wizard_step_2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKFLZV4wf-EkJvWKloRbtZo49EmwdZbLAzaFMKrMkbJju36YBqnxggLNEil2HNXPN21dUYYMUS1eJP-BznMdXzo2-w-VNwLqzYkYd0E74w9BVy_jNPkY9B62IQs8zTUhdbI_XA4mLknrjl/s320/wizard_step_2.jpg" width="276" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Step 4: In the "Maven Archetypes" type "web" and select "maven-archetype-webapp-RELEASE" from the list, and click next</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOUrDb02qrsA6DxB_PtbBDxx31USeMAqZcUJNzgOpaeh_TtYvhduNPTbMMtcwGGCpdObWMyjTvJ-B1ocVeTmTJyzbYvHkXOPL2AZ6sp3mIwpJ3Kjm8qWVfIyyoA2nX9bMFVhakD1aj-wxq/s1600/wizard_step_3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOUrDb02qrsA6DxB_PtbBDxx31USeMAqZcUJNzgOpaeh_TtYvhduNPTbMMtcwGGCpdObWMyjTvJ-B1ocVeTmTJyzbYvHkXOPL2AZ6sp3mIwpJ3Kjm8qWVfIyyoA2nX9bMFVhakD1aj-wxq/s320/wizard_step_3.jpg" width="276" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Step 5: In the "Maven Project Information" window, change the names if you want, else leave it as it is, and then click finish</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicqVcGgesXV0lhTie1NQCSnF1Zm-rcqgyh4cPF06-al_rSYHQNwtwE2SnRrUxHFLhc_9lS1__Jf-LK7XCgkghIHrnNLqk0sv33L0CNvmcHlr0BPqJN38FAyerg0-DJqsjaLH5iouzUQgSl/s1600/wizard_step_4-final.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicqVcGgesXV0lhTie1NQCSnF1Zm-rcqgyh4cPF06-al_rSYHQNwtwE2SnRrUxHFLhc_9lS1__Jf-LK7XCgkghIHrnNLqk0sv33L0CNvmcHlr0BPqJN38FAyerg0-DJqsjaLH5iouzUQgSl/s320/wizard_step_4-final.jpg" width="276" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
If you followed the steps correctly, a project will be created in your workspace with the following structure</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIYXvsOqAEB4Ys0E_6jH260iN43eWURYt7Mkabs1PsdAAXlHGIsDTqQPPvxqhPj-wMItM-v-xQSPTRnT9PEWh6mpzqvKeyynUgc8xPdpjsMMF-dedvuyPnkkqUVQDod9WRcxDgmsP6kiGx/s1600/project_structure.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIYXvsOqAEB4Ys0E_6jH260iN43eWURYt7Mkabs1PsdAAXlHGIsDTqQPPvxqhPj-wMItM-v-xQSPTRnT9PEWh6mpzqvKeyynUgc8xPdpjsMMF-dedvuyPnkkqUVQDod9WRcxDgmsP6kiGx/s320/project_structure.jpg" width="313" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
The pom file will be like below,</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsuA6wXnYJH9OUsmfu2MDP3lAAdO6HqSwea1vcsv4DUpQAf4ED_dZd147olZ8JJM_S2gCnXMoOvWjJYFXc0YzdoltGh7UWwP0u-x5vG4HPs-dDdE2xKYsjOuSLxT825-0KujDZS2LvmlG6/s1600/pom_file.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="152" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsuA6wXnYJH9OUsmfu2MDP3lAAdO6HqSwea1vcsv4DUpQAf4ED_dZd147olZ8JJM_S2gCnXMoOvWjJYFXc0YzdoltGh7UWwP0u-x5vG4HPs-dDdE2xKYsjOuSLxT825-0KujDZS2LvmlG6/s320/pom_file.jpg" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Now you can add any dependencies to the POM file and continue with your project. Happy Coding!</div>
<br /></div>Anonymoushttp://www.blogger.com/profile/16789505062816668313noreply@blogger.com0tag:blogger.com,1999:blog-3435429280123092588.post-33520168299973599512012-06-16T22:47:00.001-07:002012-06-23T23:25:43.715-07:00Java-Spring Security Authenticate through Facebook- OAuth 2<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUwQARVI-a3WMwdMdEFcXeEbvQLW4wBaErPcb3ziQ-E-3bKAQHRmi7DY1V_MnLjn6jvqEkU1xGySAZlWTtp-4b2HVxNdVVY1yEyESI7gV6ktwXqsjThGujix9aATSbi5HQ91JCk6pjWQVe/s1600/login_via_facebook.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="79" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUwQARVI-a3WMwdMdEFcXeEbvQLW4wBaErPcb3ziQ-E-3bKAQHRmi7DY1V_MnLjn6jvqEkU1xGySAZlWTtp-4b2HVxNdVVY1yEyESI7gV6ktwXqsjThGujix9aATSbi5HQ91JCk6pjWQVe/s320/login_via_facebook.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: left;">
Recently I
had a requirement of adding "Login with facebook" functionality to my
existing spring-java web application. As I was not familiar with this
before, tried to find some online examples or tutorials to get some
help. But I couldn't find any good tutorial on this and finally had to
go through the <a href="http://developers.facebook.com/docs/guides/web/" target="_blank">facebook api</a> and <a href="http://restfb.com/" target="_blank">restfb </a> by
myself to find a solution. Since the solution I found is working fine
so far, thought to share it hoping it might be useful to someone else
too.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
My
web application was a spring mvc (spring 3) configured and also spring
security-3. I had a normal login form to the web app through spring
security (/j_spring_security_check). All I wanted was to have a
"Login/Connect with Facebook button". My requirement was to authenticate
the user in my spring security context after he has been authenticated
through Facebook. For this purpose I used a workaround since I couldn't
find any other proper solution-even I tried various methods, even <a href="http://static.springsource.org/spring-social-facebook/docs/1.0.x/reference/html/" target="_blank">Spring social. </a> In spring social they have specified a very convenient way to interact with Facebook's <a href="http://developers.facebook.com/docs/reference/api/">Graph API</a> once
we get the access token from facebook and the access token can be
received from Facebook after OAuth authorization. This is exactly where I
got stuck. So below is the way I got through it.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
I'm going to use <a href="https://developers.facebook.com/docs/authentication/server-side/">server-side authentication</a> with Facebook in this example. </div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
As the first step, we will have to create an application with Facebook. This application will represent our real application in Facebook, and will give the "<b><i>App ID/API Key</i></b>" and "<b><i>App Secret</i></b>" which will be required to connect our application with Facebook.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
For this, go to "<a href="https://developers.facebook.com/apps">https://developers.facebook.com/apps</a>" and create a new application by clicking on "Create New App". After entering the required details, you will be given the App Id and App Secret.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Next, according to the Facebook API we have to redirect user to the URL-</div>
<div class="separator" style="clear: both; text-align: left;">
<span style="background-color: white; color: blue;">"https://www.facebook.com/dialog/oauth?</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="background-color: white; color: blue;"> client_id=YOUR_APP_ID</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="background-color: white; color: blue;"> &redirect_uri=YOUR_REDIRECT_URI</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="background-color: white; color: blue;"> &scope=COMMA_SEPARATED_LIST_OF_PERMISSION_NAMES</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="background-color: white; color: blue;"> &state=SOME_ARBITRARY_BUT_UNIQUE_STRING"</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="background-color: white; color: blue;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="background-color: white;">in order to get the </span><a href="https://developers.facebook.com/docs/reference/dialogs/oauth/">OAuth Dialog</a> from facebook-(the user will enter the Facebook user name and the password here)</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
What I did was, first redirecting the user to my own controller and redirecting to the above url from my controller-In this way I thought we can get more control over constructing the above url.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
So, as the first step, add the something like below in your JSP where the login with Facebook button is required,</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div style="background-color: #8ccce6; color: #051e4f; font-size: 16px; padding: 5px; width: 100%;">
<div class="separator" style="clear: both; text-align: left;">
<a href="facebookLogin.htm"></div>
<div class="separator" style="clear: both; text-align: left;">
<img src="images/fbconnect.png" alt="Connect with Facebook" /></div>
<div class="" style="clear: both; text-align: left;">
</a>
</div>
</div>
<br />
<div class="" style="clear: both; text-align: left;">
will appear like :-
</div>
<div class="separator" style="clear: both; text-align: left;">
</div>
<div>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1s8WuwKnePQurvH2dquVqLQvrovBPDx9UjBnY4C7a2x-AYJq9xNr2s96jiQWygnOcj1ZmdNWIS5pVb2qc30-yjNsWkOfkOTe8JJhg1_BtQ73JbTzNlx2Q5HajKdb4bIkQ2VAyEM5AZMD1/s1600/login_with_facebook.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1s8WuwKnePQurvH2dquVqLQvrovBPDx9UjBnY4C7a2x-AYJq9xNr2s96jiQWygnOcj1ZmdNWIS5pVb2qc30-yjNsWkOfkOTe8JJhg1_BtQ73JbTzNlx2Q5HajKdb4bIkQ2VAyEM5AZMD1/s1600/login_with_facebook.png" /></a>
</div>
<div class="separator" style="clear: both; text-align: left;">
Now the code inside the controller will be like below;</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
</div>
<div style="background-color: #8ccce6; color: #051e4f; font-size: 14px; padding: 5px; width: 100%;">
<div class="separator" style="clear: both;">
@RequestMapping(value = "/facebookLogin")</div>
<div class="separator" style="clear: both;">
public void getFacebookLogin(HttpServletRequest request,HttpServletResponse response) {<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div class="separator" style="clear: both;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>String url="https://www.facebook.com/dialog/oauth/?"</div>
<div class="separator" style="clear: both;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> + "client_id=" + ApplicationConstants.FACEBOOK_APP_ID</div>
<div class="separator" style="clear: both;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> + "&redirect_uri=" + ApplicationConstants.FACEBOOK_REDIRECT_URL</div>
<div class="separator" style="clear: both;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> + "&scope=email,publish_stream,user_about_me,friends_about_me"</div>
<div class="separator" style="clear: both;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> + "&state=" + ApplicationConstants.FACEBOOK_EXCHANGE_KEY</div>
<div class="separator" style="clear: both;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> + "&display=page"</div>
<div class="separator" style="clear: both;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> + "&response_type=code";</div>
<div class="separator" style="clear: both;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>try {</div>
<div class="separator" style="clear: both;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>response.sendRedirect(url);</div>
<div class="separator" style="clear: both;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>} catch (IOException e) {</div>
<div class="separator" style="clear: both;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>// TODO Auto-generated catch block</div>
<div class="separator" style="clear: both;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>e.printStackTrace();</div>
<div class="separator" style="clear: both;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div class="separator" style="clear: both;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
</div>
</div>
</div>
<br />
Lets have a look at the variables appended to the url,<br />
<br />
<b>client_id-</b>this is the application id ("<b><i>App ID/API Key</i></b>") mentioned above, since this will not change throughout the application, i have put it as a constant in my ApplicationConstants.java interface.<br />
<b>redirect_uri- </b>this will be the url, to which the user will be redirected by Facebook after login/cancellation from the <a href="https://developers.facebook.com/docs/reference/dialogs/oauth/">OAuth Dialog</a>. Our next set of code needs to be put here, how to handle the post authentication process. I used like "http://localhost:8080/your_app_name/facebookAuthentication.htm"<br />
<b>scope-</b>The scope parameter allows us to specify a comma separated list of additional permissions which we need the user to grant our application, the complete list can be found <a href="https://developers.facebook.com/docs/authentication/permissions/">here</a>. Add these according to your requirements.<br />
<b>state-</b>this is like a key we used to keep the conversation between our application and Facebook safe, "to ensure the security of the response when the user returns to your app after the authentication step". This can be any string eg:- "justtotestfbresponseafterlogin"-this too is a constant<br />
<b>display-</b> The display mode with which to render the Dialog<br />
<b>response_type-</b>The requested response type, one of code or token, I tried to get the token staright, but I failed, so I'm getting the "code" and using the code we receive, we can get the access token.<br />
<br />
<a href="https://developers.facebook.com/docs/reference/dialogs/oauth/">Here</a> can be found a complete list of parameters with details to the above URL.<br />
<br />
Now its time to see howto handle the response we receive from Facebook when the user redirect to our application from the above URL. this is my code in the controller,<br />
<br />
<div style="background-color: #8ccce6; padding: 5px; width: 100%;">
<div style="color: #051e4f; font-size: 16px;">
@RequestMapping(value = "/facebookAuthentication", method=RequestMethod.GET)</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>public String facebookAuthentication(HttpServletRequest request,HttpServletResponse response) {<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="font-size: 16px;">
<span style="white-space: pre;"><span style="color: #38761d;"><b> //Get the parameter "code" from the request </b></span></span></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>String code=request.getParameter("code");</div>
<div style="color: #051e4f; font-size: 16px;">
<b style="color: #38761d; white-space: pre;">//Check if its null or blank or empty</b></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>if(StringUtils.isNotEmpty(code)){</div>
<span class="Apple-tab-span" style="color: #051e4f; font-size: 16px; white-space: pre;"> </span><span style="color: #051e4f;"> </span><b><span style="color: #38761d;"><span style="white-space: pre;">//If we received a valid code, we can continue to the next step</span></span></b><br />
<span style="color: #051e4f;"> </span><b><span style="color: #38761d;"><span style="white-space: pre;">//Next we want to get the access_token from Facebook using the code we got, </span></span></b><br />
<b><span style="color: #38761d;"><span style="white-space: pre;"> //use the following url for that, in this url,</span></span></b><br />
<b><span style="color: #38761d;"><span style="white-space: pre;"> //</span></span></b><span style="color: #38761d;"><span style="white-space: pre;"><b>client_id-our app id(same as above), </b></span></span><b><span style="color: #38761d;"><span style="white-space: pre;"> </span></span></b><span style="color: #38761d;"><span style="white-space: pre;"><b>redirect_uri-same as above, client_secret-same as //above, code-the code we just got</b></span></span><br />
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>String url="https://graph.facebook.com/oauth/access_token?"</div>
<div style="color: #051e4f; font-size: 16px;">
+ "client_id=" + ApplicationConstants.FACEBOOK_APP_ID</div>
<div style="color: #051e4f; font-size: 16px;">
+ "&redirect_uri=" + ApplicationConstants.FACEBOOK_REDIRECT_URL</div>
<div style="color: #051e4f; font-size: 16px;">
+ "&client_secret=" + ApplicationConstants.FACEBOOK_SECRET_KEY</div>
<div style="color: #051e4f; font-size: 16px;">
+ "&code=" + code;<span style="white-space: pre;"> </span></div>
<div style="font-size: 16px;">
<span class="Apple-tab-span" style="color: #051e4f; white-space: pre;"> </span><b><span style="color: #38761d;">// Create an instance of HttpClient.</span></b></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> HttpClient client = new HttpClient();</div>
<div style="font-size: 16px;">
<span style="color: #051e4f;"><span class="Apple-tab-span" style="white-space: pre;"> </span> </span><span style="color: #38761d;"><b>// Create a method instance.</b></span></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> GetMethod method = new GetMethod(url);<span class="Apple-tab-span" style="white-space: pre;"> </span> </div>
<div style="font-size: 16px;">
<span style="color: #051e4f;"><span class="Apple-tab-span" style="white-space: pre;"> </span> </span><span style="color: #38761d;"><b>// Provide custom retry handler is necessary</b></span></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span>new DefaultHttpMethodRetryHandler(3, false));<span class="Apple-tab-span" style="white-space: pre;"> </span> </div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> try {</div>
<div style="font-size: 16px;">
<span style="color: #051e4f;"><span class="Apple-tab-span" style="white-space: pre;"> </span> </span><span style="color: #38761d;"><b> // Execute the method.</b></span></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> int statusCode = client.executeMethod(method);</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> if (statusCode != HttpStatus.SC_OK) {</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> System.err.println("Method failed: " + method.getStatusLine());</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> }</div>
<div style="font-size: 16px;">
<span style="color: #051e4f;"><span class="Apple-tab-span" style="white-space: pre;"> </span> </span><span style="color: #38761d;"><b> // Read the response body.</b></span></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> byte[] responseBody = method.getResponseBody();</div>
<div style="font-size: 16px;">
<span style="color: #051e4f;"><span class="Apple-tab-span" style="white-space: pre;"> </span> </span><span style="color: #38761d;">// <b>Deal with the response</b>.<b>Use caution: ensure correct character encoding and is</b></span></div>
<div style="font-size: 16px;">
<span style="color: #38761d;"><b> // not binary data</b></span></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> String responseBodyString=new String(responseBody);</div>
<div style="font-size: 16px;">
<span style="color: #051e4f;"><span class="Apple-tab-span" style="white-space: pre;"> </span> </span><span style="color: #38761d;"><b> //will be like below,<span class="Apple-tab-span" style="white-space: pre;"> </span> //access_token=AAADD1QFhDlwBADrKkn87ZABAz6ZCBQZ//DZD&expires=5178320</b></span></div>
<div style="font-size: 16px;">
<span style="color: #051e4f;"><span class="Apple-tab-span" style="white-space: pre;"> </span> </span><span style="color: #38761d;"><b> //now get the access_token from the response</b></span></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> if(responseBodyString.contains("access_token")){</div>
<div style="font-size: 16px;">
<span style="color: #051e4f;"><span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span> </span><span style="color: #38761d;"><b> //success</b></span></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span> String[] mainResponseArray=responseBodyString.split("&");</div>
<div style="font-size: 16px;">
<span style="color: #051e4f;"><span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span> </span><span style="color: #38761d;"><b>//like //{"access_token=</b></span>
<b style="color: #38761d;">AAADD1QFhDlwBADrKkn87ZABAz6ZCBQZ//DZD</b> <span style="color: #38761d;"><b>","expires=5178320"}</b></span></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span> String accesstoken="";</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span> for (String string : mainResponseArray) {</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>if(string.contains("access_token")){</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>accesstoken=string.replace("access_token=", "").trim();</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> }</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span> </div>
<div style="font-size: 16px;">
<span style="color: #051e4f;"><span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span> </span><span style="color: #38761d;"><b> //now we have the access token :)</b></span></div>
<div style="font-size: 16px;">
<span style="color: #38761d;"><b> //Great. Now we have the access token, I have used restfb to get the user details here</b></span></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span> FacebookClient facebookClient = new DefaultFacebookClient(accesstoken);</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span> User user = facebookClient.fetchObject("me", User.class);</div>
<div style="font-size: 16px;">
<span style="color: #051e4f;"> <b> </b></span><span style="color: #38761d;"><b> //In this user object, you will have the details you want from Facebook, Since we have the access token with us, can play around and see what more can be done</b></span></div>
<div style="font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"><span style="color: #051e4f;"> </span><b><span style="color: #38761d;"> //CAME UP TO HERE AND WE KNOW THE USER HAS BEEN AUTHENTICATED BY FACEBOOK, LETS AUTHENTICATE HIM IN OUR APPLICATION</span></b></span></div>
<div style="font-size: 16px;">
<span style="color: #051e4f;"> </span><span class="Apple-tab-span" style="white-space: pre;"><span style="color: #051e4f;"> </span><b><span style="color: #38761d;"> //NOW I WILL CALL MY doAutoLogin METHOD TO AUTHENTICATE THE USER IN MY SPRING SECURITY CONTEXT</span></b></span></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> }else{</div>
<div style="font-size: 16px;">
<span style="color: #051e4f;"><span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span> </span><span style="color: #38761d;"><b> //failed</b></span></div>
<div style="font-size: 16px;">
<span style="color: #051e4f;"> return "redirect:loginPage.htm";</span><span style="color: #051e4f; white-space: pre;"> </span>
</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> }<span class="Apple-tab-span" style="white-space: pre;"> </span> </div>
<div style="color: #051e4f; font-size: 16px;">
<br /></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> } catch (HttpException e) {</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> System.err.println("Fatal protocol violation: " + e.getMessage());</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> e.printStackTrace();</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> } catch (IOException e) {</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> System.err.println("Fatal transport error: " + e.getMessage());</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> e.printStackTrace();</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> } finally {</div>
<div style="font-size: 16px;">
<span style="color: #051e4f;"><span class="Apple-tab-span" style="white-space: pre;"> </span> </span><span style="color: #38761d;">// Release the connection.</span></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> method.releaseConnection();</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> }<span style="white-space: pre;"> </span></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>}else{</div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span><b style="color: #38761d;"> //failed</b></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>return "redirect:loginPage.htm";<span style="white-space: pre;"> </span></div>
<div style="color: #051e4f; font-size: 16px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<span style="white-space: pre;"> </span></div>
<div style="color: #051e4f; font-size: 16px;">
}</div>
</div>
</div>
<br />
Now I will show the code i used to authenticate the user in my spring security context,<br />
<br />
<div style="background-color: #8ccce6; font-size: 16px; padding: 5px; width: 100%;">
<div style="color: #051e4f;">
public String doAutoLogin(String username, String password, HttpServletRequest request)throws Exception {</div>
<div style="color: #051e4f;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>try {</div>
<span class="Apple-tab-span" style="white-space: pre;"><span style="color: #051e4f;"> </span><span style="color: #38761d;"><b> </b></span></span><span style="color: #38761d;"><b>// Must be called from request filtered by Spring Security,</b></span><br />
<span style="color: #38761d;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>// otherwise SecurityContextHolder is not updated</b></span><br />
<div style="color: #051e4f;">
<span style="white-space: pre;"> </span>UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);</div>
<div style="color: #051e4f;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>token.setDetails(new WebAuthenticationDetails(request));</div>
<div style="color: #051e4f;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Authentication authentication = this.authenticationProvider.authenticate(token);</div>
<div style="color: #051e4f;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>SecurityContextHolder.getContext().setAuthentication(authentication);</div>
<div style="color: #051e4f;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>return ApplicationConstants.SUCCESS;</div>
<div style="color: #051e4f;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>} catch (Exception e) {</div>
<div style="color: #051e4f;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>SecurityContextHolder.getContext().setAuthentication(null);</div>
<div style="color: #051e4f;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Failure in autoLogin" + e);<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="color: #051e4f;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div style="color: #051e4f;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>return null;</div>
<div style="color: #051e4f;">
}</div>
<div style="color: #051e4f;">
<br /></div>
</div>
</div>
<br />
Hope this tutorial helps, any feedback is highly appriciated.<br />
<br />
In the next tutorial, lets see how to access various information from Facebook and how to publish to Facebook using restfb api and hopefully Spring social too. </div>Anonymoushttp://www.blogger.com/profile/16789505062816668313noreply@blogger.com6