wSam wrote:
The ThreadLocal holder sounds interesstig. I would be thankful when you could post some code yes :-).
Holder (static class, singleton):
Code:
public class EncryptionPasswordHolder {
private static ThreadLocal<String> passwordHolder = new ThreadLocal<String>();
public static void clearPassword() {
passwordHolder.set(null);
}
public static String getPassword() {
return passwordHolder.get();
}
public static void setPassword(String password) {
if (password != null) {
passwordHolder.set(password);
}
}
}
Usage in an servlet filter (note: the code is vastly simplified, I leave it to you to null-safe, type check and all the other ancilliary aspects):
Code:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpSession httpSession = ((HttpServletRequest) request).getSession();
if (httpSession != null) {
// Obtain your password here, this example retrieves from a session attribute
String password = (String) httpSession.getAttribute(sessionAttribute);
if (password != null) {
EncryptionPasswordHolder.setPassword(password);
}
}
try {
// now the password is accessible to classes running inside of this filter via
// EncryptionPasswordHolder.getPassword()
chain.doFilter(request, response);
} finally {
// Clean up
EncryptionPasswordHolder.clearPassword();
}
}