Wednesday, January 18, 2023

Form Login and Trapping Failed logins

I'm looking for a way to trap the results for a form based login from j_security_check so I can tell the users why they failed to login.
public class LastAuthenticationErrorHelper { Throwable lastFailure; public LastAuthenticationErrorHelper() { final String func = "LastAuthenticationErrorHelper"; //did login fail? lastFailure = com.ibm.websphere.security.auth.WSSubject.getRootLoginException(); } public boolean wasLoginFailure() { return (lastFailure != null); } public Throwable getRootCause() { return determineCause(lastFailure); } private Throwable determineCause(Throwable e) { Throwable t = null; boolean isWASException = false; if (e instanceof com.ibm.websphere.security.auth.WSLoginFailedException) { isWASException = true; t =((com.ibm.websphere.security.auth.WSLoginFailedException) e).getCause(); } if (e instanceof com.ibm.websphere.security.WSSecurityException) { isWASException = true; t = ((com.ibm.websphere.security.WSSecurityException) e).getCause(); } //is the input a WAS exception? - if so, need to look at t if (isWASException) { //I hope we found a cause for the WAS exception if (t != null) { //good. search deeper return determineCause(t); } else { //this is bad. There should be a cause. return null; } } else { //this input must have been a "final" exception return e; } public class LoginFilter implements Filter { public void doFilter(ServletRequest req,ServletResponse resp,FilterChain chain) throws ServletException, IOException { //ensure that HTTP Session is created. This is important. If I wait until after the filter //is called, the response may already be committed. HttpSession tsession = ((HttpServletRequest) req).getSession(); chain.doFilter(req, resp); //did login fail? LastAuthenticationErrorHelper f = new LastAuthenticationErrorHelper(); if (f.wasLoginFailure()) { HttpSession s = ((HttpServletRequest) req).getSession(); s.setAttribute("rootexc", f.getRootCause()); }

No comments: