Best Practices from Oracle Development's A‑Team

Unsolicited login with OAM 11g

In a previous post I talked a little about protecting only a part of an application with OAM. I included this bit of text describing the use case:

But what if you want to let users access part of the app anonymously, but require them to log in to access some of the apps features? I don't know what anyone else calls this sort of flow, but I call it the shopping cart model (browse around tossing stuff in your card, then sign in to check out).


That post talked about how to support the "shopping cart" login model with OAM if you're using ADF, but what if you're trying to accomplish that with plain old HTML or something else?


First start with a simple page - for example your company's home page. I stuck mine in a virtual directory named UnsolicitedLoginDemonstration. Then create an OAM policy that makes that page Unprotected:

Screen Shot 2012-04-02 at 9.32.37 PM

Notice that I set the Protection Level to "Unprotected" and put it in the Public Resource Authentication and Authorization Policies. I specifically want this to be Unprotected, not Excluded. I'll explain why later.


Then create another new resource - this one called something like "loggedin.jsp":

Screen Shot 2012-04-02 at 9.37.12 PM

Notice that this resource is Protected by OAM.


That's all you need to do in OAM, the rest is inside your web app.


In the HTML of your web app (or your company's home page) add an iframe with the src set to loggedin.jsp:

For example here's how you would do it with an iframe directly: <iframe src="loggedin.jsp"> Sorry, your browser doesn't support iframes (which means it's old and busted). </iframe>

Note: it's really important that you close the iframe with </iframe>. You can't just close the iframe tag directly like <iframe src="something"/> because browsers stop parsing the page at that point!


I'm going to skip the rest of the HTML lesson here, but if you want to support browsers that don't support iframes you can just put HTML inside iframe where I put the "Sorry" text.


If you load up that page you'll see something like this:

Screen Shot 2012-04-03 at 11.35.00 AM

When a user enters their username and password and hits Login they'll load the protected URL - in this case "loggedin.jsp". So all that remains is to have that page reload the parent page. I did that with a little bit of JavaScript. Here's my loggedin.jsp:

&lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/&gt; &lt;script language="JavaScript"&gt; function blastFrame() {     var parentloc = parent.location;     //alert( "Parent location is " + parentloc );     parent.window.location = parentloc; } &lt;/script&gt;     &lt;/head&gt;     &lt;body onload="blastFrame();"&gt; Your browser doesn't support onload in an iframe. &lt;input type="button" value="click me" onclick="blastFrame();"&gt;     &lt;/body&gt; &lt;/html&gt;


If you want to get clever you can go a couple of steps forward and only make the login box appear when the user actually clicks a button or hovers over a chunk of text, and disappears when the user mouses out of it.


To do that we do a little bit of CSS and JavaScript magic. Here's what I have in the HEAD:

&lt;style type="text/css"&gt; #LoginLayer { background-color: yellow; height:200px; width:400px; left:100px; top:100px; visibility: hidden; } &lt;/style&gt; &lt;script language="JavaScript"&gt;   function setVisible( setting )  {     var myElement = document.getElementById("LoginLayer");     if(setting)             myElement.style.visibility="visible";     else             myElement.style.visibility="hidden";     } &lt;/script&gt;     &lt;title&gt;Demonstration of unsolicited login with OAM 11g&lt;/title&gt;     &lt;/head&gt;

The CSS there makes the LoginLayer (which we'll see in a minute) hidden. The JavaScript either makes the LoginLayer visible or hidden depending on the boolean passed in. The only remaining fun is the actual HTML for the login form:


    Or you could do the same thing with a layer so it appears when you click a button:     &lt;P/&gt;       &lt;form&gt;         &lt;input type="BUTTON" value="Click to hide" onClick="setVisible(false)" /&gt;         &lt;input type="BUTTON" value="Click to show" onClick="setVisible(true)" /&gt;       &lt;/form&gt;     or mouseover an image &lt;img src="loginbutton.gif" onmouseover="setVisible(true)"/&gt;     or &lt;div onmouseover="setVisible(true)"&gt;&lt;B&gt;this text&lt;/B&gt;&lt;/div&gt; to show the login iframe below     &lt;BR/&gt;     &lt;p id="LoginLayer" onmouseout="setVisible(false)" &gt;     &lt;iframe src="loggedin.jsp" height="200px" width="400px"&gt;         Sorry, your browser doesn't support iframes (which means it's old and busted).     &lt;/iframe&gt;     &lt;/p&gt;

The result is something like this:

Screen Shot 2012-04-03 at 12.31.34 PM

When you click or mouseover the login box appears like this:

Screen Shot 2012-04-03 at 12.32.31 PM


I did this all with a <P>, but you could do it with an actual layer if you wanted. Or you could use JQuery to make it even cooler. Whatever floats your boat.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha