わかりましたので、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 クラス (このクラスの 1 つの属性に正確に) にアクセスしたいので、LdapConnect クラスを HttpServlet クラスから拡張し、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 オブジェクトを取得できません。