首页 > 其他分享 >拦截器实现拦截到人员的id

拦截器实现拦截到人员的id

时间:2024-10-08 14:19:13浏览次数:11  
标签:拦截器 issueId public taskDto release 拦截 id

WebConfig.WebConfig(配置拦截器)

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public CrewInterceptor crewInterceptor() {
        return new CrewInterceptor();
    }

    /*@Autowired
    private CrewInterceptor crewInterceptor;*/

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(crewInterceptor())
                .addPathPatterns("/**"); // 拦截所有路径
    }
}

Interceptor.CrewInterceptor(创建拦截器)

public class CrewInterceptor implements HandlerInterceptor {

    @Autowired
    private CrewMapper crewMapper;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(!(handler instanceof HandlerMethod)){
            return true;
        }

        //
        int id=request.getIntHeader("X-Crew-ID");
        try {
            if(crewMapper.selectById(id).getType() >= 4 && crewMapper.selectById(id).getType() <= 0){
                throw new Exception("权限位置,人员信息错误");
            }
        }
        catch (Exception e){
            response.setStatus(401);
            return false;
        }

        LocalStrorageUtil.set(id);
        return true;
    }
}

util.LocalStorageUtil(实用工具)

public class LocalStrorageUtil {

    //提供ThreadLocal对象
    private static ThreadLocal threadLocal = new ThreadLocal();

    public static Object get(){
        return threadLocal.get();
    }

    public static void set(Object o){
        threadLocal.set(o);
    }

    public static void remove(){
        threadLocal.remove();
    }
}

具体例子实现

@Autowired
private TaskService taskService;
 //*发布任务*//

直接使用请求Param Inter issueId
/*@PostMapping("/release")
public Result release(@RequestParam Integer issueId, @RequestBody TaskDto taskDto){
    log.info("id为:{}的人员发布了id为{}的任务",issueId,taskDto.getId());
    taskService.release(issueId,taskDto);
    return Result.success();
}*/

    
拦截器拦截请求体X-Crew-ID,使用工具类LocalStrorageUtil获取到issueId
@PostMapping("/release")
public Result release(@RequestBody TaskDto taskDto){
    log.info("id为:{}的人员发布了id为{}的任务", LocalStrorageUtil.get(),taskDto.getId());
    System.out.println(LocalStrorageUtil.get());
    int issueId= (int) LocalStrorageUtil.get();
    System.out.println(issueId);
    taskService.release(issueId,taskDto);
    return Result.success();
}

具体拦截器和Jwt令牌代码:

https://blog.csdn.net/2301_79890818/article/details/142701328

标签:拦截器,issueId,public,taskDto,release,拦截,id
From: https://blog.csdn.net/2301_79890818/article/details/142758765

相关文章

  • 【源码+文档+调试讲解】基于Android的固定资产借用管理平台
    摘 要固定资产借用管理平台设计的目的是为用户提供使用申请、故障报修、设备归还、意见反馈等管理方面的平台。与PC端应用程序相比,固定资产借用管理平台的设计主要为管理员和用户提供便利。用户可以通过Android及时查看设备信息等。固定资产借用管理平台是在Android操作......
  • 【JS】requestIdleCallback实现分块执行
    点击按钮后,执行一个耗时较长的dom操作,页面很长时间没有响应,给用户一种卡死的现象<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0">&......