用户登录后端springboot流程

一、跨域校验

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @author Mr.Feng
 * @description:解决跨域问题
 * @create 2022-02-22-15:30
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowCredentials(true);
    }

}

二、Domain实体类层

import java.io.Serializable;

/**
 * @author Mr.Feng
 * @description:
 * @create 2022-02-22-17:29
 * 为了前后台的传输还需要实现序列化
 */
public class Admin implements Serializable {
    private Integer id;
    private String name;
    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Admin() {
    }

    public Admin(Integer id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }

    @Override
    public String toString() {
        return "Admin{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

}

三、Dao层

import org.springframework.stereotype.Repository;

/**
 * @author Mr.Feng
 * @description:@Repository这样service层就很方便的调用dao
 * 管理员Dao
 * @create 2022-02-22-17:33
 */
@Repository
public interface AdminMapper {
    /*
    * 验证密码是否正确
    * */
    public int verifyPassword(String name,String password);

}

四、Mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC
        "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.javaclimb.music.dao.AdminMapper">
    <resultMap id="BaseResultMap" type="com.javaclimb.music.domain.Admin" >
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="password" jdbcType="VARCHAR" property="password"/>
    </resultMap>

    <select id="verifyPassword" resultType="java.lang.Integer">
        select count(*) from admin where name=#{name} and password=#{password}
    </select>

</mapper>

五、Service层下Service和实现类Impl


/**
 * @author Mr.Feng
 * @description:service层肯定调用的是dao层
 * 管理员service接口
 * @create 2022-02-22-17:51
 */
public interface AdminService {
    /*
    * 验证密码是否正确
    * */
    public boolean verifyPassword(String name,String password);
}



package com.javaclimb.music.service.impl;

import com.javaclimb.music.dao.AdminMapper;
import com.javaclimb.music.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Mr.Feng
 * @description:管理员service实现类
 * @service 注解可以供controller调用
 * @create 2022-02-22-17:53
 */
@Service
public class AdminServiceImpl implements AdminService {
    /*调用Mapper里面的东西*/
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public boolean verifyPassword(String name, String password) {
        return adminMapper.verifyPassword(name,password)>0;
    }
}

六、Controller层

import com.alibaba.fastjson.JSONObject;
import com.javaclimb.music.service.AdminService;
import com.javaclimb.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author Mr.Feng
 * @description:
 * @create 2022-02-22-18:59
 */
@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;

    /*判断是否登录成功*/
    @RequestMapping(value = "/admin/login/status",method = RequestMethod.POST)
    public Object loginStatus(HttpServletRequest request, HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        boolean flag = adminService.verifyPassword(name,password);
        if (flag) {
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"登录成功");
            session.setAttribute(Consts.NAME,name);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.MSG,"用户名或密码错误");
        return jsonObject;
    }
}

七、其他一些配置

1、启动类 添加@MapperScanner注解

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*热更新 热加载
* 1、ctrl+shift+A -->搜索registry...,找到compiler.automake.allow.when.app.running
* 2、ctrl + F9 才进行热加载*/
@SpringBootApplication
@MapperScan("com.javaclimb.music.dao")
public class MusicApplication {

    public static void main(String[] args) {
        SpringApplication.run(MusicApplication.class, args);
    }

}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值