微人事项目学习笔记(2)- 后端环境搭建

1 后端环境搭建

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2 后端登录

  • 首先根据表生成model和mapper

在这里插入图片描述

  • SecurityConfig
package com.tzb.vhr.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tzb.vhr.model.Hr;
import com.tzb.vhr.model.RespBean;
import com.tzb.vhr.service.HrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.*;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Description TODO
 * @Author tzb
 * @Date 2021/9/25 22:16
 * @Version 1.0
 **/
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    HrService hrService;

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(hrService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated().and().formLogin()
                .usernameParameter("username")
                .passwordParameter("password")
                .loginProcessingUrl("/doLogin")
                .loginPage("/login")
                .successHandler(new AuthenticationSuccessHandler() {
                    @Override
                    public void onAuthenticationSuccess(HttpServletRequest req, HttpServletResponse resp, Authentication authentication) throws IOException, ServletException {
                        resp.setContentType("application/json;charset=utf-8");
                        PrintWriter out = resp.getWriter();
                        Hr hr = (Hr) authentication.getPrincipal();
                        RespBean ok = RespBean.ok("登录成功", hr);
                        String s = new ObjectMapper().writeValueAsString(ok);
                        out.write(s);
                        out.flush();
                        out.close();
                    }
                })
                .failureHandler(new AuthenticationFailureHandler() {
                    @Override
                    public void onAuthenticationFailure(HttpServletRequest req, HttpServletResponse resp, AuthenticationException e) throws IOException, ServletException {
                        resp.setContentType("application/json;charset=utf-8");
                        PrintWriter out = resp.getWriter();
                        RespBean error = RespBean.error("登录失败");
                        if (e instanceof LockedException) {
                            error.setMsg("账户被锁定,请联系管理员");
                        } else if (e instanceof CredentialsExpiredException) {
                            error.setMsg("密码过期,请联系管理员");
                        } else if (e instanceof AccountExpiredException) {
                            error.setMsg("账户过期,请联系管理员");
                        }else if (e instanceof DisabledException) {
                            error.setMsg("账户被禁用,请联系管理员");
                        }else if(e instanceof BadCredentialsException){
                            error.setMsg("用户名或者密码输入错误,请联系管理员");
                        }
                        String s = new ObjectMapper().writeValueAsString(error);
                        out.write(s);
                        out.flush();
                        out.close();
                    }
                })
                .permitAll()
                .and()
                .logout()
                .logoutSuccessHandler(new LogoutSuccessHandler() {
                    @Override
                    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {

                    }
                })
                .permitAll()
                .and()
                .csrf().disable();

    }
}

在这里插入图片描述


2.1 默认登录接口

  • 返回 json 字符串
    在这里插入图片描述

2.2 注销接口

在这里插入图片描述

  • 测试
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 注销
    在这里插入图片描述

  • 再次访问测试接口
    在这里插入图片描述

3 前后端对接

3.1 安装网络请求工具 axios

在这里插入图片描述

  • 封装请求,统一处理异常
  • https://element.eleme.io/#/zh-CN/component/message
    响应拦截器

在这里插入图片描述

3.2 跨域处理

  • 在 node js配置请求转发
  • 新建 vue.config.js
    在这里插入图片描述

3.3 登录

在这里插入图片描述
在这里插入图片描述

4 登录跳转

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 登录成功后
    在这里插入图片描述

5 前端请求方法封装

在这里插入图片描述
vue插件

在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值