{"id":7157,"date":"2016-06-22T19:02:41","date_gmt":"2016-06-22T17:02:41","guid":{"rendered":"https:\/\/blog.redbaronofazure.com\/?p=7157"},"modified":"2016-08-18T09:21:07","modified_gmt":"2016-08-18T07:21:07","slug":"azure-ad-for-an-open-source-based-website-part-3","status":"publish","type":"post","link":"https:\/\/blog.redbaronofazure.com\/?p=7157","title":{"rendered":"Azure AD for an Open Source based website \u2013 part 3 &#8211; Java"},"content":{"rendered":"<p>This is the third consecutive post about using Azure AD as the authentication source for Open Source based web applications. The previous two delt with Python and Node.js and this post will cover Java and Tomcat.<\/p>\n<p><strong>Azure AD Application Library &#8211; ADAL<\/strong><\/p>\n<p>There is a lengthy article in the Azure documentation series (see refs) that goes through in\u00a0great detail in showing how you can use ADAL for Java (Azure AD Application Library) to get OAuth authentication working for a Java-based Tomcat webapp. If you just want to see it in action, clone the github repo and make your changes to the web.xml file and deploy it. It is really that simple and you don&#8217;t need to do all 8 steps in the article.<\/p>\n<p>I did just that on a Ubuntu Linux server in Azure, where I cloned the git repo, installed Maven as the build engine and installed the Java JDK<\/p>\n<pre class=\"lang:sh decode:true\">$ git clone --branch complete https:\/\/github.com\/Azure-Samples\/active-directory-java-webapp-openidconnect.git\r\n$ sudo apt-get install maven \r\n$ sudo apt-get install default-jdk\r\n\r\n$ ...edit web.xml ...\r\n\r\n$ mvn package<\/pre>\n<p>Before editing the web.xml file you have to first register yet another Azure AD application. Since I planned to deploy this solution as a webapp in Azure AppServices and give it&#8217;s java\/tomcat support a spin, I registered my app with the following details.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-config-1.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-7159\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-config-1.png\" alt=\"aadjava-config-1\" width=\"707\" height=\"362\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-config-1.png 707w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-config-1-300x154.png 300w\" sizes=\"(max-width: 707px) 100vw, 707px\" \/><\/a><\/p>\n<p>Name is just internal in the Azure portal. Sign-on url must match the url you are at in the application when you request authentication so that Azure AD knows that it is a legitimate request. You also need to create a key that should be inserted in the web.xml config file. The App ID uri is just a name the app will be know to inside the AAD and is nothing that will break this sample code if you change it. I set the Reply Url to go back to the index page of the webapp instead of the page the github sample\u00a0redirects to.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-config-2.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-7160\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-config-2.png\" alt=\"aadjava-config-2\" width=\"696\" height=\"408\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-config-2.png 696w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-config-2-300x176.png 300w\" sizes=\"(max-width: 696px) 100vw, 696px\" \/><\/a><\/p>\n<p>With this you can update the web.xml config file in the solution.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-config-3.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-7165\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-config-3.png\" alt=\"aadjava-config-3\" width=\"828\" height=\"596\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-config-3.png 828w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-config-3-300x216.png 300w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-config-3-768x553.png 768w\" sizes=\"(max-width: 828px) 100vw, 828px\" \/><\/a><\/p>\n<p><strong>Enable Java and Tomcat in Azure AppServices<\/strong><\/p>\n<p>Azure AppServices is has Java runtime and Tomcat preinstalled and all you need to do is to enable them in the Application settings. Once enabled they kick in as soon as you save the settings.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-deploy-3.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-7167\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-deploy-3.png\" alt=\"aadjava-deploy-3\" width=\"578\" height=\"409\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-deploy-3.png 578w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-deploy-3-300x212.png 300w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/><\/a><\/p>\n<p><strong>Build and Deploy<\/strong><\/p>\n<p>After the Maven build process is completed you have a Tomcat WAR-file ready to be deployed. It should be upload to the D:\\home\\site\\wwwroot\\webapps. Since Azure AppServices comes with a ftp server, I choose to deploy the WAR-file using ftp. You&#8217;ll find the hostame of the ftp server in the Settings page in the Azure portal. You need to create a userid\/password under Deployment Credentials to login and the userid is prepended with the app name of the Azure webapp. That is how it knows what webapp you are targeting.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-deploy-1.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-7161\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-deploy-1.png\" alt=\"aadjava-deploy-1\" width=\"756\" height=\"595\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-deploy-1.png 756w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-deploy-1-300x236.png 300w\" sizes=\"(max-width: 756px) 100vw, 756px\" \/><\/a><\/p>\n<p>Once the WAR-file is uploaded, the Tomcat server will pick it up in a few seconds and unzip it.\u00a0How many seconds vary, but once it is done you can see the directory in the Azure portal console.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-deploy-2.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-7166\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-deploy-2.png\" alt=\"aadjava-deploy-2\" width=\"836\" height=\"604\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-deploy-2.png 836w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-deploy-2-300x217.png 300w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-deploy-2-768x555.png 768w\" sizes=\"(max-width: 836px) 100vw, 836px\" \/><\/a><\/p>\n<p><strong>The webapp in action<\/strong><\/p>\n<p>If you have read the previous posts on Azure AD authentication, you will be familiar with these green browser screens. I have modified the github sample to include the bootstrap CSS\/javascript to get a nicer look. I also changed the index.jsp page to output details from Azure AD when the user is authentication.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-1.png\"><img loading=\"lazy\" class=\"alignnone size-large wp-image-7162\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-1-1024x737.png\" alt=\"aadjava-website-1\" width=\"736\" height=\"530\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-1-1024x737.png 1024w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-1-300x216.png 300w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-1-768x552.png 768w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-1.png 1033w\" sizes=\"(max-width: 736px) 100vw, 736px\" \/><\/a><\/p>\n<p>Once you press the &#8220;Login with Azure AD&#8221; you will be redirected to Azure ADs login page.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-2-.png\"><img loading=\"lazy\" class=\"alignnone size-large wp-image-7163\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-2--1024x682.png\" alt=\"aadjava-website-2\" width=\"736\" height=\"490\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-2--1024x682.png 1024w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-2--300x200.png 300w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-2--768x512.png 768w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-2-.png 1157w\" sizes=\"(max-width: 736px) 100vw, 736px\" \/><\/a><\/p>\n<p>When returning to the index.jsp page, the code behind outputs\u00a0some details from the claims Azure AD emits.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-3.png\"><img loading=\"lazy\" class=\"alignnone size-large wp-image-7164\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-3-1024x781.png\" alt=\"aadjava-website-3\" width=\"736\" height=\"561\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-3-1024x781.png 1024w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-3-300x229.png 300w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-3-768x586.png 768w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/06\/aadjava-website-3.png 1036w\" sizes=\"(max-width: 736px) 100vw, 736px\" \/><\/a><\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>With these three posts my ambition was to show you that Azure AD is really platform and language agnostic. I&#8217;ve used webapps hosted on Ubuntu Linux and Azure AppServices using Python, Node.js and Java, each using Azure AD as its authentication source. If you have Active Directory today and expand into the public or Cloud, Azure AD will be your identity companion and will support the applications you have regardless of platform or language. Azure AD is really a key differentiator in Microsoft&#8217;s Cloud offering.<\/p>\n<p><strong>References<\/strong><\/p>\n<p>Azure documentation &#8211; Java Web App Sign-in and Sign-Out using Azure AD<br \/>\n<a href=\"https:\/\/azure.microsoft.com\/en-us\/documentation\/articles\/active-directory-devquickstarts-webapp-java\">https:\/\/azure.microsoft.com\/en-us\/documentation\/articles\/active-directory-devquickstarts-webapp-java<\/a><\/p>\n<p>Github repo &#8211; sample java code for the above documentation<br \/>\n<a href=\"https:\/\/github.com\/Azure-Samples\/active-directory-java-webapp-openidconnect\">https:\/\/github.com\/Azure-Samples\/active-directory-java-webapp-openidconnect<\/a><\/p>\n<p>Github repo &#8211; ADAL for Java (Azure AD Application Library)<br \/>\n<a href=\"https:\/\/github.com\/AzureAD\/azure-activedirectory-library-for-java\">https:\/\/github.com\/AzureAD\/azure-activedirectory-library-for-java<\/a><\/p>\n<p>Azure documentation &#8211; Add a Java Application to Azure AppServices<br \/>\n<a href=\"https:\/\/azure.microsoft.com\/sv-se\/documentation\/articles\/web-sites-java-add-app\/\">https:\/\/azure.microsoft.com\/sv-se\/documentation\/articles\/web-sites-java-add-app\/<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the third consecutive post about using Azure AD as the authentication source for Open Source based web applications. The previous two delt with Python and Node.js and this post will cover Java and Tomcat. Azure AD Application Library &#8211; ADAL There is a lengthy article in the Azure documentation series (see refs) that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[392,151,131,121,101],"tags":[21,51],"_links":{"self":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts\/7157"}],"collection":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7157"}],"version-history":[{"count":7,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts\/7157\/revisions"}],"predecessor-version":[{"id":7196,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts\/7157\/revisions\/7196"}],"wp:attachment":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}