生成一个springboot登录代码 要求实现用户名和密码校验,并且用拦截器拦截不正确的请求,

时间: 2023-05-30 18:08:04 浏览: 101
以下是一个示例的Spring Boot登录代码,其中实现了用户名和密码的校验,并使用拦截器拦截不正确的请求: 1. 首先,在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 创建一个名为`UserController`的控制器,用于处理登录请求和返回登录页面: ```java @RestController public class UserController { @GetMapping("/") public String index() { return "Welcome to the login page!"; } @GetMapping("/login") public String login() { return "Please enter your username and password."; } @PostMapping("/login") public String doLogin(HttpServletRequest request, String username, String password) { // 校验用户名和密码是否正确 if ("admin".equals(username) && "123456".equals(password)) { // 登录成功,将用户信息保存到Session中 request.getSession().setAttribute("user", username); return "Login successful!"; } else { // 登录失败,返回错误信息 return "Username or password is incorrect!"; } } } ``` 3. 创建一个名为`LoginInterceptor`的拦截器,用于拦截不正确的请求: ```java public class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 检查用户是否已经登录,如果未登录则跳转到登录页面 if (request.getSession().getAttribute("user") == null) { response.sendRedirect("/login"); return false; } return true; } } ``` 4. 在`Application`类中添加以下配置,以启用Spring Security和拦截器: ```java @SpringBootApplication public class Application extends WebSecurityConfigurerAdapter { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override protected void configure(HttpSecurity http) throws Exception { // 禁用CSRF防护和HTTP Basic认证 http.csrf().disable().httpBasic().disable() // 配置登录页面和登录请求的URL .formLogin().loginPage("/login").usernameParameter("username").passwordParameter("password").loginProcessingUrl("/login") // 配置拦截器 .and().addFilterBefore(new LoginInterceptor(), BasicAuthenticationFilter.class) // 配置允许访问的页面 .authorizeRequests().antMatchers("/login", "/").permitAll().anyRequest().authenticated(); } } ``` 5. 运行应用程序,并访问http://localhost:8080/,应该会看到“Welcome to the login page!”的消息。点击“Please enter your username and password.”链接进入登录页面,输入正确的用户名和密码,应该会看到“Login successful!”的消息。如果输入错误的用户名或密码,则会看到“Username or password is incorrect!”的消息。如果访问受保护的页面而未登录,则应该会被重定向到登录页面。

相关推荐

最新推荐

recommend-type

python实现用户名密码校验

主要为大家详细介绍了python实现用户名密码校验,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

SpringBoot框架集成token实现登录校验功能

主要为大家详细介绍了SpringBoot框架集成token实现登录校验功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

SpringBoot实现前端验证码图片生成和校验

主要为大家详细介绍了SpringBoot实现前端验证码图片生成和校验,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

springboot validator枚举值校验功能实现

主要介绍了springboot validator枚举值校验功能实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

JAVA实现社会统一信用代码校验的方法

主要介绍了JAVA实现社会统一信用代码校验的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。