2012-06-01 54 views
0

我需要在login.jsp调用的登录servlet之前和之后实现一些逻辑。GateIn:用于登录servlet的过滤器

所以我写了一个URL /登录过滤器来做到这一点。我需要对某些操作的用户配置文件,所以我创造了这个LoginFilter类:

public class LoginFilter implements Filter { 
    private static Logger logger = Logger.getLogger(LoginFilter.class); 

    @Override 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
     HttpServletRequest httpRequest = (HttpServletRequest) request; 
     String username = httpRequest.getParameter("username"); 
     String password = httpRequest.getParameter("password"); 

     chain.doFilter(request, response); 

     PortalRequestContext context = PortalRequestContext.getCurrentInstance(); 

     if (context == null) 
      logger.info("PortalRequestContext is NULL"); 
     else { 
      String userId = context.getRemoteUser(); 

      if (userId == null || userId.equals("")) 
       logger.info("Login failed, IP:" + httpRequest.getRemoteAddr()); 
      else 
       logger.info("Login executed, username:" + userId); 
     } 
    } 

的问题是,“上下文”(PortalRequestContext)总是空。我做错了什么?这是正确的方法吗?

回答

1

如果您正在使用GateIn,你可以尝试使用

org.exoplatform.portal.webui.util.Util.getPortalRequestContext().getRequest() 

CE

0

在登录时,尚未创建PortalRequestContext的,但你可以通过调用HttpServletRequest#getRemoteUser()

0
让远程用户

您可以添加一个GateIN过滤器,如详细的here

而且你可以在此过滤器静态地使用ConversationState来获取当前用户名:

ConversationState.getCurrent().getIdentity().getUserId();

1

你可以开发一个阀门,并把它添加到的“门户” Web应用程序上下文文件(Tomcat的/ conf目录/卡塔利娜/本地主机/ portal.xml)。 这就是在GateIN做了SSO扩展,例如: 见ServletAccessValve

ServletAccess.setRequestAndResponse(request, response); 

然后,请求在SSOLoginModule访问使用此:

// Tomcat way (Assumed that ServletAccessValve has been configured in context.xml) 
    else 
    { 
    request = ServletAccess.getRequest(); 
    } 

对于JBoss,它更简单,你有只是使用

javax.security.jacc.PolicyContext.getContext(HttpServletRequest.class.getName()) 
0

只需使用通话状态对象:

// Gets the current user id 
ConversationState conversationState = ConversationState.getCurrent(); 

org.exoplatform.services.security.Identity identity = conversationState.getIdentity(); 
String userId = identity.getUserId();