Servlet过滤器无法正常工作

时间:2014-10-10 13:23:17

标签: spring-mvc authentication servlet-filters

我正在使用spring mvc。拦截网址我在其中使用servlet过滤器。我试图通过" login.jsp"来验证用户。但是当我输入用户名和密码并提交它时。它又一次 引导我去" login.jsp"

我的Servlet过滤器:

@WebFilter(urlPatterns={"/*"})
public class MyFilter implements Filter {


private ServletContext context;


public MyFilter() {
    // TODO Auto-generated constructor stub
}


public void destroy() {
    // TODO Auto-generated method stub
}


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

    response.setContentType("text/html");
    PrintWriter out  = response.getWriter();

    HttpServletRequest request1 = (HttpServletRequest)request;
    HttpServletResponse response1 = (HttpServletResponse)response;
    HttpSession session = request1.getSession(false);

    String uri = request1.getRequestURI();

    if( (session==null || session.getAttribute("users")==null)&& (!uri.endsWith("login.jsp") && !uri.endsWith("record/authenticate")) ){
            request1.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request1, response1);
    }
    else{
        chain.doFilter(request,response);
    }
}


public void init(FilterConfig fConfig) throws ServletException {
    this.context = fConfig.getServletContext();
}

}

我的控制器:

@Controller

@RequestMapping("/record")
public class MainController {

@Resource(name="userService")
private UserService userService; 

@Resource(name="roleService")
private RoleService roleService;



@RequestMapping(value="/login")
public String GetFront(Model model){
    return "login";
}

@RequestMapping(value="/authenticate",method = RequestMethod.POST)
public String authenticate(@RequestParam("uname")String userName,@RequestParam("pass")String password,
        HttpSession session,HttpServletRequest request,Model model){

    boolean success = userService.validate(userName,password);
    User user = userService.getuserByName(userName);
    Set<Role> roles = user.getRole();
    //String role = null;
    /*
    for (Role role1: roles) {           
        if(role1.getRoleName().equalsIgnoreCase("admin")){
            role = "admin";
        }           
    }
    */
    if(success){
        session = request.getSession();
        session.setAttribute("users",userName);
        session.setAttribute("role", roles);
        return "Hello";
    }
    else{
        return "error";
    }

}

}

我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Project1</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
<filter-name>MyFilter</filter-name>
<filter-class>Filter.MyFilter</filter-class>
</filter>


<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

1 个答案:

答案 0 :(得分:0)

您必须在过滤器本身中设置会话属性,因为每次以登录形式输入一些输入并将其提交到下一页或servlet时,用户的会话属性为null。这就是为什么它在您的登录页面上循环。