我有一个使用custom login module JBoss的应用程序。身份验证可能由于各种原因而失败,我必须向用户显示这些,而不是通常的Inavlid username / password
错误。
有没有办法从登录消息中获取错误消息?我认为最好的方法是通过异常,因为authenticate返回一个布尔值,但我无法确定如何在身份验证后捕获它。任何指针都欢迎。
答案 0 :(得分:3)
使用了阀org.jboss.web.tomcat.security.ExtendedFormAuthenticator
并从会话中抓取j_exception
。
参考:
答案 1 :(得分:2)
您可以使用数据库登录模块,然后使用
获取异常异常e =(例外)SecurityContextAssociation.getContextInfo(“org.jboss.security.exception”);
您可以在托管bean函数中使用此代码来获取错误消息ex。
public String getLoginFailureMsg(){
Exception e = (Exception) SecurityContextAssociation.
getContextInfo("org.jboss.security.exception");
if(e != null){
if(e.getMessage().contains("PB00019"))
return "invalid username";
else
return "invalid password";
}
return null;
}
使用Jboss 7设置JAAS,请看这个打击:
http://amatya.net/blog/implementing-security-with-jaas-in-jboss-as-7/
答案 2 :(得分:1)
我遇到了同样的问题...但是我不喜欢将代码写入容器中,原因很明显。
所以我所做的就是自己在会话中添加例外。
首先,构建一个ThreadLocal异常持有者,在LoginContext和ServletContext之间发送异常:
public final class SecurityThreadLocal {
private static final ThreadLocal<Exception> j_exception = new ThreadLocal<Exception>();
public static void setException(Exception e) {
j_exception.set(e);
}
public static Exception getException() {
return (Exception)j_exception.get();
}
public static void clear() {
j_exception.remove();
}
}
将LoginException添加到SecurityThreadLocal:
catch (Exception e) { // or just catch LoginException
log.log(Level.SEVERE, e.getMessage(), e);
SecurityThreadLocal.setException(e);
}
使用过滤器向HttpSession添加异常:
<强>的web.xml 强>
<filter-mapping>
<filter-name>SecurityFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<强> SecurityFilter.java 强>
if (uri.endsWith("<form-error-page>") && session != null){
Exception j_exception = SecurityThreadLocal.getException();
if( j_exception != null)
session.setAttribute("j_exception", j_exception);
}
但你应该知道,因为我知道这是一种不好的做法和安全漏洞。
嗯......就我而言,客户赢了......