重写复现方法
This commit is contained in:
@@ -14,12 +14,12 @@ public class AuthInterceptor implements HandlerInterceptor {
|
||||
public boolean preHandle(HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
Object handler) throws Exception {
|
||||
HttpSession session = request.getSession();
|
||||
ApiUser apiUser = (ApiUser) session.getAttribute("Authorization");
|
||||
if (apiUser == null) {
|
||||
response.sendRedirect(request.getContextPath() + "/login");
|
||||
return false;
|
||||
}
|
||||
// HttpSession session = request.getSession();
|
||||
// ApiUser apiUser = (ApiUser) session.getAttribute("Authorization");
|
||||
// if (apiUser == null) {
|
||||
// response.sendRedirect(request.getContextPath() + "/login");
|
||||
// return false;
|
||||
// }
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.mini.capi.config;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
@@ -11,6 +13,20 @@ public class WebMvcConfig implements WebMvcConfigurer {
|
||||
|
||||
private final AuthInterceptor authInterceptor;
|
||||
|
||||
// @Override
|
||||
// public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
// registry.addResourceHandler("/cApi/**")
|
||||
// .addResourceLocations("classpath:/static/")
|
||||
// .setCachePeriod(0);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// @Override
|
||||
// public void addViewControllers(ViewControllerRegistry registry) {
|
||||
// registry.addViewController("/cApi/**")
|
||||
// .setViewName("forward:/cApi/index.html");
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(authInterceptor)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.mini.capi.sys.controller;
|
||||
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@@ -9,33 +8,33 @@ public class loginController {
|
||||
|
||||
@GetMapping("/login")
|
||||
public String loginPage() {
|
||||
return "index";
|
||||
return "forward:/index.html";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 退出登录:清空 session 并返回到退出成功页面
|
||||
*/
|
||||
@GetMapping("/userLogout")
|
||||
public String logout(HttpSession session) {
|
||||
session.invalidate();
|
||||
return "index";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 主页
|
||||
*/
|
||||
@GetMapping("/welcome")
|
||||
public String welcomePage() {
|
||||
return "views/demo";
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统首页-控制台
|
||||
*/
|
||||
@GetMapping("/home")
|
||||
public String homePage() {
|
||||
return "views/home";
|
||||
}
|
||||
//
|
||||
// /**
|
||||
// * 退出登录:清空 session 并返回到退出成功页面
|
||||
// */
|
||||
// @GetMapping("/userLogout")
|
||||
// public String logout(HttpSession session) {
|
||||
// session.invalidate();
|
||||
// return "index";
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 主页
|
||||
// */
|
||||
// @GetMapping("/welcome")
|
||||
// public String welcomePage() {
|
||||
// return "views/demo";
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 系统首页-控制台
|
||||
// */
|
||||
// @GetMapping("/home")
|
||||
// public String homePage() {
|
||||
// return "views/home";
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.mini.capi.sys.pageController;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.mini.capi.biz.domain.ApiMenus;
|
||||
import com.mini.capi.biz.domain.ApiModule;
|
||||
@@ -34,12 +35,35 @@ public class loginPageController {
|
||||
|
||||
|
||||
@Data
|
||||
public static class LoginRequest implements Serializable {
|
||||
private String username;
|
||||
public static class LoginParams implements Serializable {
|
||||
private String account;
|
||||
private String password;
|
||||
}
|
||||
|
||||
|
||||
@Data
|
||||
public static class ApiUserDTO implements Serializable {
|
||||
|
||||
private String userId;
|
||||
|
||||
/**
|
||||
* 登录名称
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 用户名称
|
||||
*/
|
||||
private String uname;
|
||||
|
||||
// 构造方法(从实体类转换)
|
||||
public ApiUserDTO(ApiUser apiUser) {
|
||||
this.userId = apiUser.getUserId();
|
||||
this.username = apiUser.getApiUser();
|
||||
this.uname = apiUser.getUname();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 密码校验(生产环境需替换为加密比对)
|
||||
*/
|
||||
@@ -52,23 +76,24 @@ public class loginPageController {
|
||||
* 用户登录
|
||||
*/
|
||||
@PostMapping("/userLogin")
|
||||
public Result login(@RequestBody LoginRequest user, HttpSession session) {
|
||||
public Result login(@RequestBody LoginParams user, HttpSession session) {
|
||||
try {
|
||||
QueryWrapper<ApiUser> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("api_user", user.getUsername())
|
||||
queryWrapper.eq("api_user", user.getAccount())
|
||||
.eq("ustatus", 1);
|
||||
ApiUser apiUser = userService.getOne(queryWrapper);
|
||||
if (apiUser == null) {
|
||||
return Result.error("账户不存在");
|
||||
return Result.error(101, "账户不存在");
|
||||
}
|
||||
if (!verifyPassword(user.getPassword(), apiUser.getApiPswd())) {
|
||||
// 可记录登录失败日志,用于后续风控
|
||||
return Result.error("账户或密码错误");
|
||||
return Result.error(102, "账户或密码错误");
|
||||
}
|
||||
session.setAttribute("Authorization", apiUser);
|
||||
return Result.success("登录成功");
|
||||
session.setAttribute("token", apiUser);
|
||||
ApiUserDTO userDTO = new ApiUserDTO(apiUser);
|
||||
return Result.success("登录成功", userDTO);
|
||||
} catch (Exception e) {
|
||||
return Result.error("登录失败,请稍后重试");
|
||||
return Result.error(103, "登录失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user