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