好的,所以我在连接到 ldap 服务器(ldapConnect 类)的自定义身份验证提供程序中有一个 retrieveUser 方法:
@Override
protected UserDetails retrieveUser(String username,
UsernamePasswordAuthenticationToken authentication)
throws AuthenticationException {
log.entry("retrieveUser", authentication.getPrincipal());
LdapUser userDetail = null;
UsernamePasswordAuthenticationToken userToken = authentication;
String userName = userToken.getName();
userName = userName != null ? userName.toLowerCase() : userName;
String password = userToken.getCredentials().toString();
if (password == null || "".equals(password)) {
log.debug("retrieveUser", "no password provided");
throw new AuthenticationCredentialsNotFoundException(
"Invalid login or password");
}
// try to connect with ldap and check retrieved username and
// password
LdapConnect ldapConnect = new LdapConnect();
connect = ldapConnect.connection(userName, password);
if (connect) {
log.debug("retrieve user", "correct connection with ldap");
userDetail = new LdapUser();
setUserDetails(userDetail, ldapConnect.getCtx(), username);
userDetail.setLdapConnect(ldapConnect);
}
if (userDetail == null) {
throw new UsernameNotFoundException("User not found");
}
return userDetail;
}
之后,我想访问现有的 LdapConnect 类(恰好访问此类的一个属性),并从 HttpServlet 类扩展我的 LdapConnect 类,并实现了 doGet 方法:
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("ctx", ctx);
getServletContext().getRequestDispatcher("/ldap_saved.jsp").forward(
request, response);
}
最后,我尝试在我的 jsp 文件中从 LdapClass 获取 ctx 属性:
<%DirContext ctx = (DirContext) request.getAttribute("ctx");%>
<%=ctx.toString();%>
但通过这种方式,我没有得到现有的 ctx 对象。