Để truy xuất Chủ đề, chúng tôi có thể sử dụng kết hợp LoginModule và Valve. Thực tế là van được gọi trước khi xác thực đá trong đang giúp chúng tôi ở đây. Khi van được gọi nó đặt phiên trong ThreadLocal (tương tự như cách JBOSS lưu yêu cầu trong ThreadLocal) và sau đó khi LoginModule.commit() được gọi nó sẽ lưu đối tượng vào phiên.
Để cấu hình tiện ích này mã biên dịch cho các lớp học dưới đây để một cái bình và đặt nó dưới $ 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;
}
}
Trong $ CATALINA_BASE/conf/server.xml thêm sau cấu hình Van như đứa trẻ yếu tố của.
<Valve className="my.test.ContainerServices" />
Trong file jaas.config thêm lớp giống như LoginModule.
DummyAppLogin {
my.test.ContainerServices required debug=true;
my.test.DummyAppLoginModule required debug=true;
};
Bây giờ sau khi bạn đăng nhập, chứng thực Chủ đề có thể được lấy ra sử dụng tiếp theo.
session.getAttribute(ContainerServices.SUBJECT_KEY);
Nguồn
2015-09-24 03:51:50
Hãy chắc chắn rằng vương quốc của bạn được sử dụng cho webapp của bạn, sau đó bạn có thể thử Servlets API của 'HttpServletRequest.getUserPrincipal() ' –
tôi biết điều đó và nó hoạt động, nhưng tôi cần phải lấy đối tượng để có được cũng roleprincipal – sasaman85