• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Using Filters with Struts

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never had this problem. Maybe if you post your code, something may stand out.
Chad
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic