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 前端请求方法封装