📅  最后修改于: 2020-12-05 03:10:01             🧑  作者: Mango
基于表单的身份验证是一种通过登录表单完成用户身份验证的方法。该表格是内置的,由Spring安全框架提供。
HttpSecurity类提供了formLogin()方法,该方法负责呈现登录表单并验证用户凭据。
在本教程中,我们将创建一个实现基于表单的身份验证的示例。让我们开始这个例子。
首先通过提供项目详细信息来创建一个Maven项目。
该项目最初看起来像这样:
使用以下Java文件在应用程序中配置spring安全性。创建一个包com.javatpoint并将所有文件放入其中。
// AppConfig.java
package com.javatpoint;
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.javatpoint.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.javatpoint;
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.javatpoint;
import org.springframework.security.web.context.*;
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
}
// WebSecuiryConfig.java
package com.javatpoint;
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.javatpoint")
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.javatpoint.controller包中。它包含以下代码。
// HomeController.java
package com.javatpoint.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 Javatpoint!
Admin login
// admin.jsp
Home Page
login successful!
Logout
Welcome Admin
// pom.xml
4.0.0
com.javatpoint
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
添加所有这些文件后,项目结构如下所示:
通过服务器运行该应用程序,然后看到它向浏览器产生以下输出。
输出:
单击链接,将呈现一个登录表单,该表单将用于基于表单的身份验证。
验证凭据后,它会验证用户身份并呈现到管理页面。