aspose file tools*
The moose likes Struts and the fly likes Using Filters with Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Using Filters with Struts" Watch "Using Filters with Struts" New topic
Author

Using Filters with Struts

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi there !
I�m having problems on using a simple Filter to check authentication . As I redirect every request to this filter ,apparently the container loses the request content and doesn�t populate the corresponding FormBean after passing through the filter, before going to the Action...
Do I have to configure some special feature ?
Thanx
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
I've never had this problem. Maybe if you post your code, something may stand out.
Chad
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Ok Chad !
Follows the code :
WEB.XML (Filter declaration)

<filter>
<filter-name>FiltroLogin</filter-name>
<display-name>FiltroLogin</display-name>
<description></description>
<filter-class>debut.FiltroLogin</filter-class>
</filter>



<filter-mapping>
<filter-name>FiltroLogin</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>

STRUTS-CONFIG.XML
<form-bean name="loginForm" type="debut.LoginForm"/>

<action
path="/Login"
type="debut.LoginAction"
name="loginForm"
scope="session"
validate="false"
input="/login.jsp">

<forward name="sucesso" path="/success.jsp"/>
<forward name="falha" path="/error.jsp"/>

</action>
FILTER code :

package debut;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class FiltroLogin implements Filter {

private FilterConfig config = null;

public void init (FilterConfig config) throws ServletException{

this.config = config;

}

public void destroy(){

config = null;

}

public void doFilter(ServletRequest request,ServletResponse response,
FilterChain chain) throws
IOException, ServletException {

HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession sessao = req.getSession();

String statusLogin = (sessao.getAttribute("statusLogin") == null) ? "" : (String)sessao.getAttribute("statusLogin");

if(statusLogin.equals("LOGGED")){

chain.doFilter(req, response);
}else{
sessao.setAttribute("URL",req.getRequestURL().toString());
res.sendRedirect("login.jsp");// CALLS THE LOGIN if it�s not already done...
}
}

}

LOGIN.JSP:
Simply posts username and password to Login.do

FORMBEAN
public class LoginForm extends ActionForm {
private String user = null;
private String password = null;


/* Set/Get user*/
public void setUser(String user){
this.user = ( user == null ) ? "" : user;
System.out.println("user = "+user);
}
public String getUser (){
System.out.println("user = "+user);
return user ;
}

/* Set/Get password*/
public void setPassword(String password){
this.password = ( password == null ) ? "" : password;
System.out.println("password = "+password);
}
public String getPassword (){
System.out.println("password = "+password);
return password ;
}
/* Metodo necess�rio para o Struts */
public void reset(ActionMapping mapping,HttpServletRequest request) {


this.user = null;
this.password = null;

}
}
LOGIN ACTION :
public class LoginAction extends Action {

public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

String target = new String("falha");
String user = "";
String password = "";

if ( form != null ) {
// Usa o FormBean para recuperar os parametros do request
LoginForm loginForm = (LoginForm)form;
user = loginForm.getUser();
password = loginForm.getPassword();
System.out.println("user = "+user+" e password = "+password);
}


if(user.equals("user") && password.equals("password")){

HttpSession sessao = request.getSession();
sessao.setAttribute("statusLogin","LOGGED");
target = new String("sucesso");
}

// Forward to the appropriate View
return (mapping.findForward(target));
}
}

Thanks
F�bio
[ June 23, 2003: Message edited by: fabio gama ]
[ June 23, 2003: Message edited by: fabio gama ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I�ve found it myself...
I hadn�t noticed that after some changes, I had left an IF dispatching always to the login.jsp... when not logged... so... always...
Tks anyway !
Best regards,
F�bio
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
In fact, when it�s already logged on , the request is not lost when passing through the filter, but, if it�s not , when redirected to the login page, the FormBean (all the request object) is lost, once it�s scope has been declared �request� in struts-config.xml... If I declare them always as �session� to avoid loosing the posted data , I�ll have a tremendous memory overhead , so... I�m a little confused on which way to take...
Does anyone have a good way for mixing Filters and Struts for authentication and not having this kind of problem ?

Thanx
F�bio
 
jQuery in Action, 2nd edition
 
subject: Using Filters with Struts