mirror of
https://github.com/RemainderTime/spring-boot-base-demo.git
synced 2026-02-06 23:21:11 +08:00
1.实现接口权限控制、sa-tokan拦截器类实现
底层异常捕获类实现
This commit is contained in:
@@ -21,7 +21,7 @@ import java.util.Map;
|
||||
/**
|
||||
* @Author: xiongfeng
|
||||
* @CreateTime: 2023-11-08 11:48
|
||||
* @Description: TODO 全局异常捕获类
|
||||
* @Description: TODO 全局异常捕获类(仅 Controller 层异常)
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@@ -59,7 +59,7 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler{
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(LoginException.class)
|
||||
public GenericResponse tokenExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
|
||||
public GenericResponse tokenExceptionHandler(HttpServletRequest request, final LoginException e, HttpServletResponse response) {
|
||||
log.error("token exception", e);
|
||||
LoginException exception = (LoginException) e;
|
||||
response.setStatus(HttpStatus.FORBIDDEN.value());
|
||||
@@ -95,6 +95,7 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler{
|
||||
public ResponseEntity exceptionHandler(jakarta.servlet.http.HttpServletRequest request, final Exception e, jakarta.servlet.http.HttpServletResponse response) {
|
||||
Map<String, String> errors = new HashMap<>();
|
||||
errors.put("message", e.getMessage());
|
||||
log.error("error------{}", e);
|
||||
return ResponseEntity.status(SystemStatus.ERROR.getCode()).body(errors);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package cn.xf.basedemo.common.exception;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
/**
|
||||
* @Description: 全局异常捕获类(所有异常(包括拦截器、Controller、视图))HandlerExceptionResolver更底层
|
||||
* @ClassName: GlobalExceptionResolver
|
||||
* @Author: xiongfeng
|
||||
* @Date: 2025/8/23 23:30
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class GlobalExceptionResolver implements HandlerExceptionResolver {
|
||||
@Override
|
||||
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
|
||||
try (PrintWriter writer = response.getWriter()) {
|
||||
if (ex instanceof LoginException) {
|
||||
response.setStatus(HttpStatus.FORBIDDEN.value());
|
||||
LoginException le = (LoginException) ex;
|
||||
writer.write(new ObjectMapper().writeValueAsString(
|
||||
new GenericResponse(le.getCode(), null, le.getMessage())
|
||||
));
|
||||
} else if (ex instanceof BusinessException) {
|
||||
BusinessException be = (BusinessException) ex;
|
||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
||||
writer.write(new ObjectMapper().writeValueAsString(
|
||||
new GenericResponse(be.getCode(), null, be.getMessage())
|
||||
));
|
||||
} else {
|
||||
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
||||
writer.write(new ObjectMapper().writeValueAsString(
|
||||
new GenericResponse(500, null, "系统异常")
|
||||
));
|
||||
}
|
||||
} catch (IOException ioEx) {
|
||||
log.error("写响应失败", ioEx);
|
||||
}
|
||||
return new ModelAndView();
|
||||
}
|
||||
}
|
||||
@@ -15,22 +15,18 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public class ApplicationContextUtils implements ApplicationContextAware {
|
||||
//放置在获取bean的时候提示空指针,将其定义为静态变量
|
||||
private static ApplicationContext context;
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
//类初始化完成之后调用setApplicationContext()方法进行操作
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
ApplicationContextUtils.context = applicationContext;
|
||||
ApplicationContextUtils.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public static ApplicationContext getContext(){
|
||||
return context;
|
||||
public static <T> T getBean(Class<T> clazz) {
|
||||
return applicationContext.getBean(clazz);
|
||||
}
|
||||
|
||||
public static Object getBean(String beanName){
|
||||
//在这一步的时候一定要注意,此时可调用这个方法的时候
|
||||
//context可能为空,会提示空指针异常,需要将其定义成静态的,这样类加载的时候
|
||||
//context就已经存在了
|
||||
return context.getBean(beanName);
|
||||
public static Object getBean(String name) {
|
||||
return applicationContext.getBean(name);
|
||||
}
|
||||
}
|
||||
|
||||
23
src/main/java/cn/xf/basedemo/config/SaTokenConfigure.java
Normal file
23
src/main/java/cn/xf/basedemo/config/SaTokenConfigure.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package cn.xf.basedemo.config;
|
||||
|
||||
import cn.dev33.satoken.interceptor.SaInterceptor;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* @Description: sa token拦截器注册类
|
||||
* @ClassName: SaTokenConfigure
|
||||
* @Author: xiongfeng
|
||||
* @Date: 2025/8/24 20:30
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Configuration
|
||||
public class SaTokenConfigure implements WebMvcConfigurer {
|
||||
// 注册 Sa-Token 拦截器,打开注解式鉴权功能
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
// 注册 Sa-Token 拦截器,打开注解式鉴权功能
|
||||
registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**");
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package cn.xf.basedemo.controller.business;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.xf.basedemo.common.model.LoginUser;
|
||||
import cn.xf.basedemo.common.model.RetObj;
|
||||
import cn.xf.basedemo.interceptor.SessionContext;
|
||||
@@ -34,6 +36,7 @@ public class UserController {
|
||||
|
||||
@Operation(summary = "用户信息", description = "用户信息")
|
||||
@PostMapping("/info")
|
||||
@SaCheckPermission("user:info")
|
||||
public RetObj info(){
|
||||
LoginUser loginUser = SessionContext.getInstance().get();
|
||||
return RetObj.success(loginUser);
|
||||
@@ -51,4 +54,10 @@ public class UserController {
|
||||
return userService.getEsId(userId);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取用户权限数据", description = "用户信息")
|
||||
@GetMapping("/getPermission")
|
||||
public RetObj getPermission(){
|
||||
return RetObj.success(StpUtil.getPermissionList());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.xf.basedemo.interceptor;
|
||||
|
||||
import cn.dev33.satoken.interceptor.SaInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
|
||||
@@ -4,8 +4,6 @@ import cn.dev33.satoken.stp.StpInterface;
|
||||
import cn.xf.basedemo.common.utils.ApplicationContextUtils;
|
||||
import cn.xf.basedemo.mappers.SysPermissionMapper;
|
||||
import cn.xf.basedemo.mappers.SysRoleMapper;
|
||||
import cn.xf.basedemo.model.domain.SysRole;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
@@ -20,13 +18,14 @@ import java.util.List;
|
||||
@Component
|
||||
public class StpInterfaceImpl implements StpInterface {
|
||||
|
||||
private SysPermissionMapper sysPermissionMapper= (SysPermissionMapper) ApplicationContextUtils.getBean("SysPermissionMapper");
|
||||
private SysRoleMapper sysRoleMapper= (SysRoleMapper) ApplicationContextUtils.getBean("SysRoleMapper");
|
||||
|
||||
private SysPermissionMapper sysPermissionMapper = ApplicationContextUtils.getBean(SysPermissionMapper.class);
|
||||
private SysRoleMapper sysRoleMapper = ApplicationContextUtils.getBean(SysRoleMapper.class);
|
||||
@Override
|
||||
public List<String> getPermissionList(Object userId, String s) {
|
||||
//获取登录用户权限数据
|
||||
return sysPermissionMapper.getPermissionListByRoleId((Long) userId);
|
||||
Long aLong = Long.valueOf(userId.toString());
|
||||
List<String> permissionList = sysPermissionMapper.getPermissionListByRoleId(aLong);
|
||||
return permissionList;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,6 +2,7 @@ package cn.xf.basedemo.mappers;
|
||||
|
||||
import cn.xf.basedemo.model.domain.SysPermission;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -11,6 +12,7 @@ import java.util.List;
|
||||
* @createDate 2025-08-19 21:22:03
|
||||
* @Entity cn.xf.basedemo.model.domain.SysPermission
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysPermissionMapper extends BaseMapper<SysPermission> {
|
||||
|
||||
List<String> getPermissionListByRoleId(Long useId);
|
||||
|
||||
@@ -2,6 +2,7 @@ package cn.xf.basedemo.mappers;
|
||||
|
||||
import cn.xf.basedemo.model.domain.SysRole;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -11,6 +12,7 @@ import java.util.List;
|
||||
* @createDate 2025-08-19 21:22:03
|
||||
* @Entity cn.xf.basedemo.model.domain.SysRole
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysRoleMapper extends BaseMapper<SysRole> {
|
||||
|
||||
List<String> getRoleListByUserId(Long userId);
|
||||
|
||||
Reference in New Issue
Block a user