springboot+html登录验证

本文介绍了如何使用Spring Boot开发一个简单的HTML登录页面,表单数据通过GET请求发送到后端,后端利用JDBC查询数据库并进行密码匹配。展示了@Autowired注解注入JdbcTemplate及Controller中处理GET请求的方法。
login.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/jdbc1" method="get">
    <input type="text" name="username">
    <input type="password" name="userpassword">
    <input type="submit" value="提交">
</form>
</body>
</html>
lianjie.contrller

lianjie.controller
由于这种方法返回的是List,所以用了一个比较无厘头的比较密码方法
将前台传来的密码也加上{},再和数据库取出的list比较

package aa.mysql428.controller;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
@Controller

public class lianjie {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @ResponseBody
    @GetMapping("/jdbc1")
    //public Map<String, Object> map(@RequestParam("username") String name , @RequestParam("userpassword") String password) {
    public String map(@RequestParam("username") String name , @RequestParam("userpassword") String password) {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select password1 from user where username1="+name);
      String a= list.get(0).toString();
      String b="{password1="+password+"}";
      if(b.equals(a)){
          return "successful";
      }
      else{
          return "fail";
      }



    }
}

aplication.yml
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/customers?serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver

sprintboot如何接收html表单传参
 public String map(@RequestParam("username") String name , @RequestParam("userpassword") String password)
将取出的List转换为字符串
 String a= list.get(0).toString();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值