2014-03-29 95 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教程

相關問題