基于表单的身份验证是一种通过登录表单完成用户身份验证的方式。该表单是内置的,由spring security框架提供。
httpsecurity类提供了formlogin()方法,该方法负责呈现登录表单并验证用户凭据。
在本教程中,我们将创建一个实现基于表单的身份验证的示例。让我们开始示例。
首先通过提供项目详细信息来创建maven项目。
该项目最初看起来像这样:
通过使用以下java文件在应用程序中配置spring安全性。创建一个包 com.nhooo 并将所有文件放入其中。
//appconfig.java
package com.nhooo; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import org.springframework.web.servlet.config.annotation.enablewebmvc; import org.springframework.web.servlet.view.internalresourceviewresolver; import org.springframework.web.servlet.view.jstlview; @enablewebmvc @configuration @componentscan({ "com.nhooo.controller.*" }) public class appconfig { @bean public internalresourceviewresolver viewresolver() { internalresourceviewresolver viewresolver = new internalresourceviewresolver(); //viewresolver.setviewclass(jstlview.class); viewresolver.setprefix("/web-inf/views/"); viewresolver.setsuffix(".jsp"); return viewresolver; } }
//mvcwebapplicationinitializer.java
package com.nhooo; import org.springframework.web.servlet.support.abstractannotationconfigdispatcherservletinitializer; public class mvcwebapplicationinitializer extends abstractannotationconfigdispatcherservletinitializer { @override protected class[] getrootconfigclasses() { return new class[] { websecurityconfig.class }; } @override protected class[] getservletconfigclasses() { // todo auto-generated method stub return null; } @override protected string[] getservletmappings() { return new string[] { "/" }; } }
//securitywebapplicationinitializer.java
package com.nhooo; import org.springframework.security.web.context.*; public class securitywebapplicationinitializer extends abstractsecuritywebapplicationinitializer { }
//websecuiryconfig.java
package com.nhooo; import org.springframework.context.annotation.*; import org.springframework.security.config.annotation.web.builders.httpsecurity; import org.springframework.security.config.annotation.web.configuration.*; import org.springframework.security.core.userdetails.*; import org.springframework.security.provisioning.inmemoryuserdetailsmanager; import org.springframework.security.web.util.matcher.antpathrequestmatcher; @enablewebsecurity @componentscan("com.nhooo") public class websecurityconfig extends websecurityconfigureradapter { @bean public userdetailsservice userdetailsservice() { inmemoryuserdetailsmanager manager = new inmemoryuserdetailsmanager(); manager.createuser(user.withdefaultpasswordencoder() .username("admin").password("admin123").roles("admin").build()); return manager; } @override protected void configure(httpsecurity http) throws exception { http.authorizerequests(). antmatchers("/index", "/user","/").permitall() .antmatchers("/admin").authenticated() .and() .formlogin() // it renders a login form .and() .logout() .logoutrequestmatcher(new antpathrequestmatcher("/logout")); } }
创建一个控制器homecontroller并将其放入 com.nhooo.controller 包中。它包含以下代码。
//homecontroller.java
package com.nhooo.controller; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; @controller public class homecontroller { @requestmapping(value="/", method=requestmethod.get) public string index() { return "index"; } @requestmapping(value="/admin", method=requestmethod.get) public string admin() { return "admin"; } }
该项目包含以下两个视图(jsp页面)。将它们放入 web-inf/views 文件夹中。
//index.jsp
index page welcome to nhooo!
admin login
//admin.jsp
home page login successful! logout
welcome admin
//pom.xml
4.0.0 com.nhooo springsecurity 0.0.1-snapshot war 1.8 1.8 org.springframework spring-webmvc 5.0.2.release org.springframework.security spring-security-web 5.0.0.release org.springframework.security spring-security-core 5.0.4.release org.springframework.security spring-security-config 5.0.4.release javax.servlet javax.servlet-api 3.1.0 provided javax.servlet jstl 1.2 org.apache.maven.plugins maven-war-plugin 2.6 false
添加所有这些文件后,项目结构如下所示:
在服务器上运行应用程序,然后看到它会向浏览器产生以下输出。
输出:
单击链接,将呈现一个登录表单,该表单将用于基于表单的身份验证。
之后验证凭据会验证用户身份并呈现到管理页面。
888棋牌游戏的友情链接: