java.lang.NullPointerException: null

springboot 中一个空指针的错误提示:

2019-03-25 11:26:25.769 ERROR 8896 --- [nio-8680-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]  :Servlet.service() for servlet [dispatcherServlet] in context with path /ding threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null

at jlsky.auth.AuthInterceptor.preHandle(AuthInterceptor.java:55) ~[classes/:na]

(省略余下的 tomcat的各种提示信息。)

事情是这样的,参照 https://www.jianshu.com/p/e88d3f8151db 做了个自定义注解和拦截器的权限模块。但是一运行到 @Autowired 注入的方法就报错。

拦截器的部分代码如下:

public class AuthInterceptor implements HandlerInterceptor {
@Autowired RoleConfirmImpl roleConfirm;

@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp,Object obj) throws ResultException {
String token = req.getHeader("token"); // 从请求中获取 token
if(!(obj instanceof HandlerMethod)){ //如果不是映射到方法,直接通过。
return true;
}
HandlerMethod handlerMethod = (HandlerMethod) obj;
Method method = handlerMethod.getMethod();

//检查有没有需要验证用户授权的注解
if(method.isAnnotationPresent(AuthToken.class)){
AuthToken authToken = method.getAnnotation(AuthToken.class);
if(authToken.required()){
//执行认证
if(token == null){
throw new ResultException(ResultCode.LOGIN_NOTOKEN);
}
//verify token,获取 token中的 userid
int userid = JwtUtil.verifyUserId(token);
log.info("userid: "+ userid);
roleConfirm.belong(userid,authToken.role());
return true;
}else{
return false;
}
}
return true; //没有应用注解的访问,许可通过。
}
起初,是通过接口间接访问的方法。去掉接口这个环节,问题依旧。
再后来在方法上添加了 static 解决问题。
(如果在接口的方法上加 static,那么就需要直接写方法体,也就失去了定义接口意义。)

虽然问题解决了,但是总觉得这个方法有点LOW。其实我也尝试了加注解或者放到main方法定义的扫描路径下,都不行。

转载于:https://www.cnblogs.com/htsky/p/10592893.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值