你的位置:首页 > Java教程

[Java教程]Spring:验证用户登录


利用 Spring IOC 技术实现用户登录的验证机制,对用户进行登录验证。

首先利用 Spring 的自动装配模式将 User 对象注入到控制器中,然后将用户输入的用户名和密码与系统中限定的合法用户的用户名和密码进行匹配。

当用户名与密码匹配成功时,跳转到登录成功页面;当用户名与密码不匹配时,跳转到登录失败的页面。

1.创建 User 对象,定义用户名和密码属性,代码如下:

package com.importnew;public class User {  private String username;  private String password;    public String getUsername() {    return username;  }  public void setUsername(String username) {    this.username = username;  }  public String getPassword() {    return password;  }  public void setPassword(String password) {    this.password = password;  }}

 

2.创建控制器 TestUtil ,注入 User 对象并进行登录验证。代码如下:

package com.importnew;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;public class TestUtil extends AbstractController{    private User user;  public User getUser() {    return user;  }  public void setUser(User user) {    this.user = user;  }  @Override   protected ModelAndView handleRequestInternal(HttpServletRequest arg0,      HttpServletResponse arg1) throws Exception {    String username = arg0.getParameter("username");    String password = arg0.getParameter("password");    if(username.equals(user.getUsername()) && password.equals(user.getPassword())){      return new ModelAndView("yes");    }else{            return new ModelAndView("Error");    }  }}

 

3.在 Spring 的配置文件 applicationContext.

<??><beans ="http://www.springframework.org/schema/beans"  ="http://www.w3.org/2001/   ="http://www.springframework.org/schema/context"  ="http://www.springframework.org/schema/tx"  ="http://www.springframework.org/schema/aop"  xsi:schemaLocation="    http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context.xsd    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx.xsd    http://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop.xsd">       <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">     <property name="prefix">       <value>/</value>     </property>     <property name="suffix">       <value>.jsp</value>     </property>   </bean>     <bean id="user" class="com.importnew.User" >     <property name="username">       <value>admin</value>     </property>     <property name="password">       <value>123</value>     </property>   </bean>     <bean autowire="byName" id="testUtil" class="com.importnew.TestUtil" >     <property name="user">       <ref bean="user"/>     </property>   </bean></beans>

 

4.在 web.

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app> <display-name>Archetype Created Web Application</display-name> <servlet>     <servlet-name>dispatcherServlet</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     <init-param>       <param-name>contextConfigLocation</param-name>       <param-value>/applicationContext.</param-value>     </init-param>     <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping>     <servlet-name>dispatcherServlet</servlet-name>     <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>

 

———————————————————————————————————————————————————

备注:

TestUtil 中继承的类 AbstractController 需要引关于 spring-web-mvc 的 JAR 包支持。

////end