{"id":391,"date":"2016-06-30T06:55:07","date_gmt":"2016-06-30T04:55:07","guid":{"rendered":"http:\/\/h2916922.stratoserver.net:8084\/?p=391"},"modified":"2016-07-04T11:13:27","modified_gmt":"2016-07-04T09:13:27","slug":"lifting-on-icn-authentication-in-custom-applications","status":"publish","type":"post","link":"https:\/\/www.ivojonker.nl\/?p=391","title":{"rendered":"Container managed EE app sharing BPF\/ICN\/ICM authentication"},"content":{"rendered":"<p>From my experience as a P8 engineer i know many are actually struggling with this. So in this post i&#8217;ll share a textbook example on how to \u00a0re-use your BPF\/ICN\/ICM authentication in your own custom Java EE application.<\/p>\n<p>Download the complete sample project <a href=\"\/ivo\/wp-attachments\/EE_Auth_ICN_src.zip\">here<\/a><\/p>\n<p>The following project assumes the use of IBM Websphere with support for EE6 (personally using\u00a08.5.5.).<\/p>\n<p>The sample project contains a few essential parts:<\/p>\n<p>1. A Simple rest-service that exposes two methods;<\/p>\n<ol>\n<li>a \/ping api that will be accessible to all;<\/li>\n<li>and a \/getObjetStoreID api that is only available to users currently logged on.<\/li>\n<\/ol>\n<pre class=\"lang:java decode:true \" title=\"A simple service\">\/**\r\n * Sample rest service for which access is managed by the container.  \r\n * @author nl.ivojonker\r\n *\/\r\n@Stateless\r\npublic class SampleService {\r\n\r\n\r\n\tprivate static final String CPEIIOPURL = \"iiop:\/\/\";\r\n\tprivate static final String OBJECTSTORENAME = \"OBJECTSTORENAME\";\r\n\r\n\t\/**\r\n\t * Accesible to everyone (see web.xml)\r\n\t * @return\r\n\t *\/\r\n\t@Path(\"\/ping\")\r\n\tpublic String ping(){\r\n\t\treturn \"pong!\";\r\n\t}\r\n\r\n\t\/**\r\n\t * A simple api call that requires the user to be authenticated (see web.xml) \r\n\t * @return\r\n\t *\/\r\n\t@Path(\"\/getObjectStoreID\")\r\n\t@Produces(MediaType.TEXT_PLAIN)\r\n\tpublic Response someApi(){\r\n\t\tConnection connection = Factory.Connection.getConnection(CPEIIOPURL);\r\n\r\n\t\tSubject subject = UserContext.getAmbientSubject();\r\n\t\tif (subject == null) {\r\n\t\t\treturn Response.status(Status.FORBIDDEN).entity(\"No SSO or existing session\").build();\r\n\t\t}\r\n\r\n\t\tUserContext.get().pushSubject(subject);\r\n\r\n\t\tDomain domain = Factory.EntireNetwork.fetchInstance(connection, null).get_LocalDomain();\r\n\t\tObjectStore store = Factory.ObjectStore.fetchInstance(domain, OBJECTSTORENAME, null);\r\n\r\n\t\treturn Response.ok(store.get_Id().toString()).build();\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>2. A web.xml in which is specified what resources (urls) are behind authentication, and which urls are not.<\/p>\n<pre class=\"lang:xhtml decode:true\" title=\"web.xml providing security constraints\">\t&lt;security-constraint&gt;\r\n\t\t&lt;web-resource-collection&gt;\r\n\t\t\t&lt;web-resource-name&gt;SampleService&lt;\/web-resource-name&gt;\r\n\t\t\t&lt;description&gt;While the complete application is secured....&lt;\/description&gt;\r\n\t\t\t&lt;url-pattern&gt;\/*&lt;\/url-pattern&gt;\r\n\t\t&lt;\/web-resource-collection&gt;\r\n\t\t&lt;auth-constraint&gt;\r\n\t\t\t&lt;role-name&gt;All Authenticated&lt;\/role-name&gt;\r\n\t\t&lt;\/auth-constraint&gt;\r\n\t\t&lt;user-data-constraint&gt;\r\n\t\t\t&lt;description&gt;User data constraints&lt;\/description&gt;\r\n\t\t\t&lt;transport-guarantee&gt;NONE&lt;\/transport-guarantee&gt;\r\n\t\t&lt;\/user-data-constraint&gt;\r\n\t&lt;\/security-constraint&gt;\r\n\r\n\t&lt;security-constraint&gt;\r\n\t\t&lt;web-resource-collection&gt;\r\n\t\t\t&lt;web-resource-name&gt;SampleService&lt;\/web-resource-name&gt;\r\n\t\t\t&lt;description&gt;Everyone is allowed to access the ping page&lt;\/description&gt;\r\n\t\t\t&lt;url-pattern&gt;\/rest\/ping&lt;\/url-pattern&gt;\r\n\t\t&lt;\/web-resource-collection&gt;\r\n\t\t&lt;user-data-constraint&gt;\r\n\t\t\t&lt;description&gt;User data constraints&lt;\/description&gt;\r\n\t\t\t&lt;transport-guarantee&gt;NONE&lt;\/transport-guarantee&gt;\r\n\t\t&lt;\/user-data-constraint&gt;\r\n\t&lt;\/security-constraint&gt;\r\n\r\n\t&lt;security-role&gt;\r\n\t\t&lt;description&gt;All Authenticated&lt;\/description&gt;\r\n\t\t&lt;role-name&gt;All Authenticated&lt;\/role-name&gt;\r\n\t&lt;\/security-role&gt;\r\n\t&lt;security-role&gt;\r\n\t\t&lt;description&gt;Everyone&lt;\/description&gt;\r\n\t\t&lt;role-name&gt;Everyone&lt;\/role-name&gt;\r\n\t&lt;\/security-role&gt;<\/pre>\n<p>3. An (websphere specific) ibm-application-bnd.xml file in which we map security roles to subjects.<\/p>\n<p>Note that this can be managed from within the wasadmin as well.<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;application-bnd xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xmlns=\"http:\/\/websphere.ibm.com\/xml\/ns\/javaee\"\r\n    xsi:schemaLocation=\"http:\/\/websphere.ibm.com\/xml\/ns\/javaee http:\/\/websphere.ibm.com\/xml\/ns\/javaee\/ibm-application-bnd_1_0.xsd\" version=\"1.0\"&gt;\r\n  &lt;security-role name=\"All Authenticated\"&gt;\r\n    &lt;special-subject type=\"ALL_AUTHENTICATED_USERS\"\/&gt;\r\n  &lt;\/security-role&gt;\r\n  &lt;security-role name=\"Everyone\"&gt;\r\n    &lt;special-subject type=\"EVERYONE\"\/&gt;\r\n  &lt;\/security-role&gt;\r\n&lt;\/application-bnd&gt;\r\n\r\n<\/pre>\n<p>Wrapping it all up; there&#8217;s just no need for complex mechanisms, &#8211; or worse, storing and sharing passwords between services \ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>From my experience as a P8 engineer i know many are actually struggling with this. So in this post i&#8217;ll share a textbook example on how to \u00a0re-use your BPF\/ICN\/ICM [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-391","post","type-post","status-publish","format-standard","hentry","category-geen-categorie"],"_links":{"self":[{"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/posts\/391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=391"}],"version-history":[{"count":11,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/posts\/391\/revisions"}],"predecessor-version":[{"id":403,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/posts\/391\/revisions\/403"}],"wp:attachment":[{"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}