使用Spring MVC登录应用程序无法正常工作

时间:2014-03-29 06:20:16

标签: java xml spring jsp spring-mvc

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

下面是我的控制器:

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;
        }
        }

            }

1 个答案:

答案 0 :(得分:0)

我不知道你到底做错了什么,但似乎你在这里重新发明轮子......你想要做的事完全被Spring Security所覆盖。查看this教程