Reduce number of logins when using the webconsole
@@ -33,6 +33,7 @@ import javax.security.auth.login.FailedLoginException;
import javax.security.auth.login.LoginContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
import org.apache.felix.webconsole.WebConsoleSecurityProvider2;
import org.osgi.service.cm.ManagedService;
@@ -52,6 +53,7 @@ public class JaasSecurityProvider implements WebConsoleSecurityProvider2, Manage
private String realm;
private String role;
+ private int sessionTimeout;
public JaasSecurityProvider() {
updated(null);
@@ -85,6 +87,7 @@ public class JaasSecurityProvider implements WebConsoleSecurityProvider2, Manage
}
realm = getString(properties, "realm", "karaf");
role = getString(properties, "role", System.getProperty("karaf.admin.role", "admin"));
+ sessionTimeout = Integer.parseInt(getString(properties, "sessionTimeout", "0"));
}
private String getString(Dictionary<String, ?> properties, String key, String def) {
@@ -177,7 +180,23 @@ public class JaasSecurityProvider implements WebConsoleSecurityProvider2, Manage
String password = srcString.substring( i + 1 );
// authenticate
- Subject subject = doAuthenticate( username, password );
+ Subject subject = null;
+ try
+ {
+ HttpSession session = request.getSession(false);
+ if ( session != null )
+ {
+ subject = (Subject) session.getAttribute( KarafOsgiManager.SUBJECT_RUN_AS );
+ }
+ }
+ catch ( Throwable t )
+ {
+ // ignore
+ }
+ if ( subject == null )
+ {
+ subject = doAuthenticate(username, password);
+ }
if ( subject != null )
{
// as per the spec, set attributes
@@ -190,6 +209,21 @@ public class JaasSecurityProvider implements WebConsoleSecurityProvider2, Manage
// set the JAAS subject
request.setAttribute( KarafOsgiManager.SUBJECT_RUN_AS, subject );
+ // create a session and store the information
+ try
+ {
+ HttpSession session = request.getSession(true);
+ if (sessionTimeout != 0)
+ {
+ session.setMaxInactiveInterval(sessionTimeout);
+ }
+ session.setAttribute( KarafOsgiManager.SUBJECT_RUN_AS, subject );
+ }
+ catch ( Throwable t )
+ {
+ // ignore
+ }
+
// succeed
return true;
}
