विषय पुनर्प्राप्त करने के लिए हम LoginModule और वाल्व के संयोजन का उपयोग कर सकते हैं। तथ्य यह है कि प्रमाणन से पहले वाल्वों को बुलाया जाता है, यहां हमें मदद मिल रही है। जब वाल्व का आह्वान किया जाता है तो यह सत्र थ्रेडलोकल में रखता है (इसी प्रकार जेबीओएसएस थ्रेडलोकल में अनुरोध कैसे सहेजता है) और बाद में जब LoginModule.commit() का आह्वान किया जाता है तो यह सत्र को सत्र में सहेजता है।
एक जार करने के लिए नीचे दिए गए इस ऐड वर्ग के लिए संकलित कोड कॉन्फ़िगर और $ CATALINA_BASE/lib/
package my.test;
import java.io.IOException;
import java.util.Map;
import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;
import javax.servlet.ServletException;
import org.apache.catalina.Session;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ValveBase;
/**
* Use following class to retrieve subject in your HTTPServlet when using Tomcat.
*/
public class ContainerServices extends ValveBase implements LoginModule {
// Key to revtieve subject from session.
public static final String SUBJECT_KEY =
"javax.security.auth.Subject.container";
/**
* Session for current thread.
*/
static InheritableThreadLocal<Session> sessionHolder =
new InheritableThreadLocal<Session>();
// JAAS Subject being authenticated.
private Subject subject;
// Invoke the value.
public void invoke(Request request, Response response) throws IOException,
ServletException {
sessionHolder.set(request.getSessionInternal(true));
try {
// Next in the invocation chain
getNext().invoke(request, response);
} finally {
sessionHolder.remove();
}
}
// Initialize the login module
public void initialize(Subject subject, CallbackHandler callbackHandler,
Map<String, ?> sharedState, Map<String, ?> options) {
this.subject = subject;
}
// Store subject to session.
public boolean commit() throws LoginException {
Session session = sessionHolder.get();
if (session != null) {
session.getSession().setAttribute(ContainerServices.SUBJECT_KEY, subject);
}
return true;
}
// not used
public boolean abort() throws LoginException {
return false;
}
// not used
public boolean login() throws LoginException {
return true;
}
// not used
public boolean logout() throws LoginException {
return true;
}
}
के तहत यह जगह $ CATALINA_BASE में/conf/server.xml तत्व के बच्चे के रूप में वाल्व विन्यास निम्नलिखित को जोड़ने के लिए।
<Valve className="my.test.ContainerServices" />
jaas.config फ़ाइल में LoginModule के रूप में एक ही कक्षा में जोड़ें।
DummyAppLogin {
my.test.ContainerServices required debug=true;
my.test.DummyAppLoginModule required debug=true;
};
अब आप लॉग इन के बाद, प्रमाणीकृत विषय निम्नलिखित का उपयोग कर प्राप्त किया जा सकता है।
session.getAttribute(ContainerServices.SUBJECT_KEY);
स्रोत
2015-09-24 03:51:50
सुनिश्चित करें कि आपके दायरे अपने webapp के लिए प्रयोग किया जाता है हो सकता है, तो आप कोशिश कर सकते हैं सर्वलेट्स एपीआई 'HttpServletRequest.getUserPrincipal()' –
मैं जानता था और यह काम करता है, लेकिन मैं भी roleprincipal पाने के लिए – sasaman85