利用Spring IOC技術(shù)實現(xiàn)用戶登錄驗證機(jī)制
利用 Spring IOC 技術(shù)實現(xiàn)用戶登錄的驗證機(jī)制,對用戶進(jìn)行登錄驗證。
首先利用 Spring 的自動裝配模式將 User 對象注入到控制器中,然后將用戶輸入的用戶名和密碼與系統(tǒng)中限定的合法用戶的用戶名和密碼進(jìn)行匹配。
當(dāng)用戶名與密碼匹配成功時,跳轉(zhuǎn)到登錄成功頁面;當(dāng)用戶名與密碼不匹配時,跳轉(zhuǎn)到登錄失敗的頁面。
1.創(chuàng)建 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.創(chuàng)建控制器 TestUtil ,注入 User 對象并進(jìn)行登錄驗證。代碼如下:
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.xml 中為 User 對象的屬性賦值,并使用自動裝配的方式在控制器 TestUtil 中注入 User 對象。代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="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.xml 文件中配置 applicationContext.xml 的自動加載,當(dāng)項目啟動后程序?qū)⒆詣蛹虞d配置文件中的信息。代碼如下:
<!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.xml</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 需要引關(guān)于 spring-web-mvc 的 JAR 包支持。
////end
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Springboot+SpringSecurity+JWT實現(xiàn)用戶登錄和權(quán)限認(rèn)證示例
- Springboot+Shiro記錄用戶登錄信息并獲取當(dāng)前登錄用戶信息的實現(xiàn)代碼
- SpringMVC 實現(xiàn)用戶登錄實例代碼
- springmvc+spring+mybatis實現(xiàn)用戶登錄功能(上)
- springmvc+spring+mybatis實現(xiàn)用戶登錄功能(下)
- Spring mvc 實現(xiàn)用戶登錄的方法(攔截器)
- spring aop action中驗證用戶登錄狀態(tài)的實例代碼
- 詳解使用Spring3 實現(xiàn)用戶登錄以及權(quán)限認(rèn)證
- Spring Cloud Gateway 使用JWT工具類做用戶登錄校驗功能
- 使用Spring框架實現(xiàn)用戶登錄
相關(guān)文章
使用Log4j2代碼方式配置實現(xiàn)線程級動態(tài)控制
這篇文章主要介紹了使用Log4j2代碼方式配置實現(xiàn)線程級動態(tài)控制,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
解決HttpServletResponse和HttpServletRequest取值的2個坑
這篇文章主要介紹了解決HttpServletResponse和HttpServletRequest取值的2個坑問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
一文講解如何解決Java中的IllegalArgumentException異常
這篇文章主要給大家介紹了關(guān)于如何解決Java中IllegalArgumentException異常的相關(guān)資料,IllegalArgumentException是Java中的一個標(biāo)準(zhǔn)異常類,通常在方法接收到一個不合法的參數(shù)時拋出,需要的朋友可以參考下2024-03-03
springcloud?eureka切換nacos的配置方法
這篇文章主要介紹了springcloud?eureka切換nacos,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01
ocp開閉原則_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了ocp開閉原則的相關(guān)資料,ocp開閉原則指導(dǎo)我們?nèi)绾谓⒁粋€穩(wěn)定的、靈活的系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
詳解在SpringBoot中使用MongoDb做單元測試的代碼
這篇文章主要介紹了詳解在SpringBoot中使用MongoDb做單元測試的代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

