2014-03-29 70 views
-1

嗨我已经创建了一个使用Spring MVC的登录应用程序,但它似乎无法正常工作。问题在于我在Authenticate类中用于从休眠状态返回用户名和密码的逻辑。如果它没有保留任何用户名或密码,并且它没有正确验证登录并且登录正在发生,即使对于错误的用户名和密码,我也会从authenticate类中获取空列表。使用Spring MVC登录应用程序无法正常工作

下面

是我的控制器:

package com.lnt.controller; 

import java.util.ArrayList; 
import java.util.List; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.servlet.ModelAndView; 

import com.lnt.services.AuthenticateServices; 

@Controller 
@RequestMapping("/Login.spring") 
public class LoginController { 

    //@Autowired 
    AuthenticateServices authenticateService = new AuthenticateServices(); 

    @RequestMapping(method=RequestMethod.POST) 
    public ModelAndView processCredentials(HttpServletRequest req, HttpServletResponse res) 

    { 
     String userName =req.getParameter("userName") ; 
     String password = req.getParameter("password"); 
     System.out.println("into login controller"); 
     System.out.println(userName); 
     System.out.println(password); 
     String message = "Invalid credentials"; 
     List<String> userdetails = new ArrayList<String>(); 
     userdetails = authenticateService.verifyUserNameAndPassword(userName, password); 
     //System.out.println(userdetails.get(0)+ "index0"); 
     if((userdetails)!=null) 
      { 
      message = "welcome" + userName ; 

     } 
     return new ModelAndView("results", "message", message) ; 
    } 

这里是我的JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<form action="Login.htm" method = "post"> 
<input type = "text" name = "userName" id = "userName"> 
<input type = "password" name = "password" id = "password"> 
<input type = "submit"> 
</form> 
</body> 
</html> 

这里是我的身份验证服务类:

package com.lnt.services; 

import java.util.List; 

import org.hibernate.SQLQuery; 
import org.hibernate.Session; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 

//import org.apache.catalina.Session; 



public class AuthenticateServices { 

    //private org.springframework.orm.hibernate3.HibernateTemplate HibernateTemplate ; 

    public AuthenticateServices() { 

    } 

    /*public AuthenticateServices(
      org.springframework.orm.hibernate3.HibernateTemplate hibernateTemplate) { 
     super(); 
     HibernateTemplate = hibernateTemplate; 
    }*/ 

    public List<String> verifyUserNameAndPassword(String userName, String Password) 
    { 
     //Session session = new Configuration().configure().buildSessionFactory().openSession() ; 
     //ser = null ; 
     System.out.println("into checking the verifyingusername and status"); 
     List<String> userobjs = null ; 
     //boolean userstatus = false; 
     try 
     { 

     Session session = null ; 
     session = new Configuration().configure().buildSessionFactory().openSession(); 
     Transaction tx = null; 
     tx = session.beginTransaction(); 
     //List<User> userobjs = HibernateTemplate.find("from user where. u.Username=? and u.password=?",userName,Password); 
      StringBuilder searchQuery = new StringBuilder(); 
      searchQuery.append("Select loginid, password from login_info where loginid ='" + userName + "' and password = '" + Password + "'"); 
     //System.out.println(userobjs + "userobjects"); 
      tx.commit(); 
      SQLQuery Sqlquery = session.createSQLQuery(searchQuery.toString()); 
      userobjs = Sqlquery.list(); 
      System.out.println("userlist" + userobjs); 
      System.out.println("query used" + Sqlquery); 
      session.close(); 

     } 
     catch(Exception e) 
     { 
      System.out.println(e); 
     } 

     if (userobjs!=null) 
     { 
     return userobjs ; 
     } 

     else 
     { 
      return null; 
     } 
     } 

      } 

回答

0

我完全不知道你做错了,但你似乎是在这里重新发明轮子......你试图做的是完全覆盖由Spring Security编辑。退房this教程

相关问题