问题描述:

我在项目中的某个Controller上添加了@RequirePermissions注解,希望在执行该请求前,可以先进行权限验证。但是当我请求该Controller时,返回的确是404错误。

首先我怀疑的是因为权限不足而抛出了404错误。但是我发现我在AController的请求方法1上加了@RequiresPermession注释,但是请求方法2同样也报了404错误。所以应该不是shiro对权限进行了拦截,更像是整个controller的请求映射都没被Spring正常解析。

哪个步骤产生了404错误

我们知道SpringMVC处理请求转发的地方是在DispatchServletdoDispatch方法中。

  1. protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
  2. HttpServletRequest processedRequest = request;
  3. HandlerExecutionChain mappedHandler = null;
  4. boolean multipartRequestParsed = false;
  5. WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
  6. try {
  7. ModelAndView mv = null;
  8. Exception dispatchException = null;
  9. try {
  10. //如果是Multipart请求,则先处理
  11. processedRequest = checkMultipart(request);
  12. multipartRequestParsed = (processedRequest != request);
  13. // Determine handler for the current request.
  14. //根据请求找到对应HandlerMapping,在通过HandlerMapping返回对应的处理器执行链HandlerExecuteChain
  15. mappedHandler = getHandler(processedRequest);
  16. //找不到对应的映射,则抛出404异常
  17. if (mappedHandler == null) {
  18. noHandlerFound(processedRequest, response);
  19. return;
  20. }
  21. // Determine handler adapter for the current request.
  22. HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
  23. // Process last-modified header, if supported by the handler.
  24. String method = request.getMethod();
  25. boolean isGet = "GET".equals(method);
  26. //GET 和 HEAD请求 如果资源没更新,则直接返回
  27. if (isGet || "HEAD".equals(method)) {
  28. long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
  29. if (logger.isDebugEnabled()) {
  30. logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
  31. }
  32. if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
  33. return;
  34. }
  35. }
  36. //请求的预处理,其实就是应用拦截器的preHandle方法
  37. if (!mappedHandler.applyPreHandle(processedRequest, response)) {
  38. return;
  39. }
  40. //正式由Controller处理请求,
  41. // Actually invoke the handler.
  42. mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
  43. if (asyncManager.isConcurrentHandlingStarted()) {
  44. return;
  45. }
  46. //根据Controller返回的视图名,解析视图
  47. applyDefaultViewName(processedRequest, mv);
  48. //后置处理,应用拦截器的后置处理方法
  49. mappedHandler.applyPostHandle(processedRequest, response, mv);
  50. }
  51. catch (Exception ex) {
  52. dispatchException = ex;
  53. }
  54. catch (Throwable err) {
  55. // As of 4.3, we're processing Errors thrown from handler methods as well,
  56. // making them available for @ExceptionHandler methods and other scenarios.
  57. dispatchException = new NestedServletException("Handler dispatch failed", err);
  58. }
  59. //处理异常或是渲染视图
  60. processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
  61. }
  62. catch (Exception ex) {
  63. triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
  64. }
  65. catch (Throwable err) {
  66. triggerAfterCompletion(processedRequest, response, mappedHandler,
  67. new NestedServletException("Handler processing failed", err));
  68. }
  69. finally {
  70. if (asyncManager.isConcurrentHandlingStarted()) {
  71. // Instead of postHandle and afterCompletion
  72. if (mappedHandler != null) {
  73. mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
  74. }
  75. }
  76. else {
  77. // Clean up any resources used by a multipart request.
  78. if (multipartRequestParsed) {
  79. cleanupMultipart(processedRequest);
  80. }
  81. }
  82. }
  83. }

一种怀疑是在getHandler时,找不到对应的executeHandlerChain,所以产生了404错误。但是在断点中我们发现依旧可以获取到相应的executeHandlerChain

貌似没有问题(其实如果够细心且了解MappingHandler的话,此时应该已经能看出问题了)。

继续往下,直到过了前置处理依旧没有问题(说明基本上不是拦截器造成的404错误)。

而再往下发现经过ha.handle()方法后,返回的mv对象为null,而此时看response对象已经出现了404的错误。

因此我们将关注点放在handle的执行顺序上。

我们得到的haHttpRequestHandlerAdapter对象。它的handle方法如下:

  1. @Override
  2. @Nullable
  3. public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
  4. throws Exception {
  5. ((HttpRequestHandler) handler).handleRequest(request, response);
  6. return null;
  7. }

HandlerAdapter是一个处理器适配器。主要是适配不同类型的处理器。而此时的Handler类型是ResourceHttpRequestHandler

其中handleRequest方法如下:

  1. @Override
  2. public void handleRequest(HttpServletRequest request, HttpServletResponse response)
  3. throws ServletException, IOException {
  4. // For very general mappings (e.g. "/") we need to check 404 first
  5. //根据请求路径,解析对应的静态资源
  6. Resource resource = getResource(request);
  7. //如果找不到对应资源,则抛出404错误
  8. if (resource == null) {
  9. logger.trace("No matching resource found - returning 404");
  10. response.sendError(HttpServletResponse.SC_NOT_FOUND);
  11. return;
  12. }
  13. if (HttpMethod.OPTIONS.matches(request.getMethod())) {
  14. response.setHeader("Allow", getAllowHeader());
  15. return;
  16. }
  17. // Supported methods and required session
  18. checkRequest(request);
  19. // Header phase
  20. if (new ServletWebRequest(request, response).checkNotModified(resource.lastModified())) {
  21. logger.trace("Resource not modified - returning 304");
  22. return;
  23. }
  24. // Apply cache settings, if any
  25. prepareResponse(response);
  26. // Check the media type for the resource
  27. MediaType mediaType = getMediaType(request, resource);
  28. if (mediaType != null) {
  29. if (logger.isTraceEnabled()) {
  30. logger.trace("Determined media type '" + mediaType + "' for " + resource);
  31. }
  32. }
  33. else {
  34. if (logger.isTraceEnabled()) {
  35. logger.trace("No media type found for " + resource + " - not sending a content-type header");
  36. }
  37. }
  38. // Content phase
  39. if (METHOD_HEAD.equals(request.getMethod())) {
  40. setHeaders(response, resource, mediaType);
  41. logger.trace("HEAD request - skipping content");
  42. return;
  43. }
  44. ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(response);
  45. if (request.getHeader(HttpHeaders.RANGE) == null) {
  46. Assert.state(this.resourceHttpMessageConverter != null, "Not initialized");
  47. setHeaders(response, resource, mediaType);
  48. this.resourceHttpMessageConverter.write(resource, mediaType, outputMessage);
  49. }
  50. else {
  51. Assert.state(this.resourceRegionHttpMessageConverter != null, "Not initialized");
  52. response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
  53. ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(request);
  54. try {
  55. List<HttpRange> httpRanges = inputMessage.getHeaders().getRange();
  56. response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
  57. this.resourceRegionHttpMessageConverter.write(
  58. HttpRange.toResourceRegions(httpRanges, resource), mediaType, outputMessage);
  59. }
  60. catch (IllegalArgumentException ex) {
  61. response.setHeader("Content-Range", "bytes */" + resource.contentLength());
  62. response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
  63. }
  64. }
  65. }

其中需要关系的部分是getResource方法,因为找不到对应的Resource,而产生了404错误。我们也找到了404错误的原因。

找到404的原因后,继续分析。ResourceHttpRequestHandler是负责处理静态资源的。正常情况下,我们到控制器的请求不应该是由ResourceHttpRequestHandler处理。因此,我们得到的Handler并非是我们期望的。

getHandler解析的Handler为什么不对

首先看DispatchServletgetHandler方法。

  1. @Nullable
  2. protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
  3. if (this.handlerMappings != null) {
  4. //遍历内部的HandlerMapping(内置处理器),返回该请求映射的处理器
  5. for (HandlerMapping hm : this.handlerMappings) {
  6. if (logger.isTraceEnabled()) {
  7. logger.trace(
  8. "Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");
  9. }
  10. //返回处理器,并形成处理器链
  11. HandlerExecutionChain handler = hm.getHandler(request);
  12. if (handler != null) {
  13. return handler;
  14. }
  15. }
  16. }
  17. return null;
  18. }

DispatcherServlet在初始化时会创建内置的一些HandlerMapping。常见的有SimpleUrlHandlerMapping(映射请求和静态资源),RequestMappingHandlerMapping(映射请求和@RequestMapping注解的Controller中的方法),BeanNameUrlHandlerMapping(映射请求和处理器bean,映射关系由bean Name确定)等。

为什么RequestMappingHandlerMapping没能够为我们对应的处理器?了解下RequestMappingHandlerMappinggetHandler方法:

  1. public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
  2. //调用内部获取处理器的方法(模板模式)
  3. Object handler = getHandlerInternal(request);
  4. //如果处理器为空 则使用默认的处理器
  5. if (handler == null) {
  6. handler = getDefaultHandler();
  7. }
  8. if (handler == null) {
  9. return null;
  10. }
  11. //如果返回的处理器是bean Name,则获取bean对象
  12. // Bean name or resolved handler?
  13. if (handler instanceof String) {
  14. String handlerName = (String) handler;
  15. handler = obtainApplicationContext().getBean(handlerName);
  16. }
  17. //形成处理器执行链(主要是添加拦截器)
  18. HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
  19. //如果是跨域请求,则设置跨域的配置
  20. if (CorsUtils.isCorsRequest(request)) {
  21. CorsConfiguration globalConfig = this.globalCorsConfigSource.getCorsConfiguration(request);
  22. CorsConfiguration handlerConfig = getCorsConfiguration(handler, request);
  23. CorsConfiguration config = (globalConfig != null ? globalConfig.combine(handlerConfig) : handlerConfig);
  24. executionChain = getCorsHandlerExecutionChain(request, executionChain, config);
  25. }
  26. return executionChain;
  27. }

查找处理器的逻辑主要是是在getHandlerInternal方法中:

  1. protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
  2. //根据请求解析路径
  3. String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
  4. if (logger.isDebugEnabled()) {
  5. logger.debug("Looking up handler method for path " + lookupPath);
  6. }
  7. this.mappingRegistry.acquireReadLock();
  8. try {
  9. //获取对应的处理器方法
  10. HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);
  11. if (logger.isDebugEnabled()) {
  12. if (handlerMethod != null) {
  13. logger.debug("Returning handler method [" + handlerMethod + "]");
  14. }
  15. else {
  16. logger.debug("Did not find handler method for [" + lookupPath + "]");
  17. }
  18. }
  19. return (handlerMethod != null ? handlerMethod.createWithResolvedBean() : null);
  20. }
  21. finally {
  22. this.mappingRegistry.releaseReadLock();
  23. }
  24. }

lookupHandlerMethod方法则是从MappingRegistry中获取匹配url的方法。在根据URL匹配的精度确认最后的方法。ReqeustMappingHandlerMapping找不到处理器,说明MappingRegistry并没有解析到对应的处理器方法。

RequstMappingHandlerMapping的初始化过程

RequestMappingHandlerMapping实现了InitializingBean接口。在其afterPropertiesSet方法中实现了将

处理器映射方法mappingRegistry的逻辑。具体实现在其父类AbstractHandlerMethodMapping中。

  1. //初始化时检测处理器方法
  2. @Override
  3. public void afterPropertiesSet() {
  4. initHandlerMethods();
  5. }
  6. //扫描上下文中的bean,注册对应的处理器方法
  7. protected void initHandlerMethods() {
  8. if (logger.isDebugEnabled()) {
  9. logger.debug("Looking for request mappings in application context: " + getApplicationContext());
  10. }
  11. //获取上下文中的bean name
  12. String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?
  13. BeanFactoryUtils.beanNamesForTypeIncludingAncestors(obtainApplicationContext(), Object.class) :
  14. obtainApplicationContext().getBeanNamesForType(Object.class));
  15. //遍历bean names
  16. for (String beanName : beanNames) {
  17. if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
  18. Class<?> beanType = null;
  19. try {
  20. beanType = obtainApplicationContext().getType(beanName);
  21. }
  22. catch (Throwable ex) {
  23. // An unresolvable bean type, probably from a lazy bean - let's ignore it.
  24. if (logger.isDebugEnabled()) {
  25. logger.debug("Could not resolve target class for bean with name '" + beanName + "'", ex);
  26. }
  27. }
  28. //是否为标准处理器(RequestMappingHandlerMapping的实现根据类上是否有@Controller或是@RequestMapping注释)
  29. if (beanType != null && isHandler(beanType)) {
  30. //筛选对应的方法并注册
  31. detectHandlerMethods(beanName);
  32. }
  33. }
  34. }
  35. handlerMethodsInitialized(getHandlerMethods());
  36. }

接下来就是在RequestMappingHandlerMapping初始化的过程中断点调试,看看是什么问题:

可以看到相应的控制器被代理过后丢失了注释。而这里的代理并非是AspectJ的创建的,而是com.sun.Proxy对象。

如果在启动时观察对应控制器的bean的创建情况,可以发现这个bean被增强了两次:

第一次增强:

第二次增强:

可以看到第二次增强过后bean丢失了@Controller的注释。

解决方案

我们已经知道造成404的真正原因是Controller初始化时被增强了两次。并在第二次增强时丢掉了注释。导致了该Controller无法被正常映射。因此我们只需要关闭一次增强过程即可。事实上,由于已经存在了ProxyCreator,因此ShiroAnnotationProcessorAutoConfiguration中的DefaultAdvisorAutoProxyCreator就不再需要了。

所以可以通过在配置文件中将shiro.annotations.enabled属性设置为false。或者是直接在项目的配置中exclude掉ShiroAnnotationProcessorAutoConfiguration。然后再声明AuthorizationAttributeSourceAdvisor即可。

Shiro踩坑记(二):使用RequiresXXX的注解后,访问对应请求返回404的更多相关文章

  1. 【React踩坑记二】react项目实现JS路由跳转

    这里使用的是4.31版本的react-router-dom "react-router-dom": "^4.3.1", 直接使用以下代码即可实现路由跳转 thi ...

  2. Shiro踩坑记(一):关于shiro-spring-boot-web-starter自动注解无法注入authorizer的问题

    一)问题描述: 我在一个Spring的项目中使用shiro搭建权限控制框架.主要通过shiro-spring-boot-web-starter包快速集成Shiro.但是项目无法启动,报没有author ...

  3. crm踩坑记(二)

    Linux tmux 如何查看 tmux如何进行滚动呢? prefix + [, prefix为tmux的前置动作,默认是ctrl + b. 使用方向键或者pageUp来进行翻页. q可以退出滚动模式 ...

  4. Visual Studio For MacOS 踩坑记(二)

    Visual Studio For MacOS安装安卓SDK. 系统默认安装了安卓6.0  API23的SDK.  但是我需要安卓7.0的,API24.  遂安装. SDK可以下载成功,但是用Visu ...

  5. iOS自动化打包上传的踩坑记

    http://www.cocoachina.com/ios/20160624/16811.html 很久以前就看了很多关于iOS自动打包ipa的文章, 看着感觉很简单, 但是因为一直没有AppleDe ...

  6. [技术博客] 敏捷软工——JavaScript踩坑记

    [技术博客] 敏捷软工--JavaScript踩坑记 一.一个令人影响深刻的坑 1.脚本语言的面向对象 面向对象特性是现代编程语言的基本特性,JavaScript中当然集成了面向对象特性.但是Java ...

  7. Spark踩坑记——Spark Streaming+Kafka

    [TOC] 前言 在WeTest舆情项目中,需要对每天千万级的游戏评论信息进行词频统计,在生产者一端,我们将数据按照每天的拉取时间存入了Kafka当中,而在消费者一端,我们利用了spark strea ...

  8. Spark踩坑记——数据库(Hbase+Mysql)

    [TOC] 前言 在使用Spark Streaming的过程中对于计算产生结果的进行持久化时,我们往往需要操作数据库,去统计或者改变一些值.最近一个实时消费者处理任务,在使用spark streami ...

  9. 【踩坑记】从HybridApp到ReactNative

    前言 随着移动互联网的兴起,Webapp开始大行其道.大概在15年下半年的时候我接触到了HybridApp.因为当时还没毕业嘛,所以并不清楚自己未来的方向,所以就投入了HybridApp的怀抱. Hy ...

随机推荐

  1. 三通道低功耗ASK低频唤醒接收器PAN3501完全替代AS3933/GC3933

    低频唤醒接收器PAN3501软硬件兼容AS3933/GC3933,且新增了寄存器功能,可直接替换,供应稳定,高性价比. 产品介绍:    PAN3501是一款最多三个通道接收的低功耗ASK接收机,可用 ...

  2. 微信小程序placeholder设置自定义颜色

    原地址链接:https://blog.csdn.net/august_leo/article/details/80877382 这是微信小程序input组件的官方文档描述,下图红框里的placehol ...

  3. 天天写order by,你知道Mysql底层执行原理吗?

    前言 文章首发于微信公众号[码猿技术专栏]. 在实际的开发中一定会碰到根据某个字段进行排序后来显示结果的需求,但是你真的理解order by在 Mysql 底层是如何执行的吗? 假设你要查询城市是苏州 ...

  4. Vue生成分享海报(含二维码)

    本文已同步到专业技术网站 www.sufaith.com, 该网站专注于前后端开发技术与经验分享, 包含Web开发.Nodejs.Python.Linux.IT资讯等板块. 功能需求: 海报有1张背景 ...

  5. 会话技术(session/cookie)

    session可保存int double bool array string object:cookie只能保存stringsession 可通过php.ini文件查看存放的位置:cookie不同浏览 ...

  6. C++语言实现链式栈

    在之前写的C语言实现链式栈篇博文中,我已经给大家大概介绍了关于链式栈的意义以及相关操作,我会在下面给大家分享百度百科对链式栈的定义,以及给大家介绍利用C++实现链式栈的基本操作. 百度百科链式栈 链式 ...

  7. Powershell 输出信息过多,结尾显示省略号

    有时候我们通过powershell指令去查询某些信息时,因为输出结果过多,导致一部分重要信息被省略号代替,如下图 面对这种情况无论是 |fl 还是  out-file 亦或是 export-csv都无 ...

  8. 008-进制-C语言笔记

    008-进制-C语言笔记 学习目标 1.[掌握]include预处理指令 2.[掌握]多文件开发 3.[了解]认识进制 4.[掌握]进制之间的互相转换 5.[掌握]原码,反码,补码 6.[掌握]位运算 ...

  9. 程序员小张的第一篇博文 --记Markdown的使用学习

    1.前言 为了即将到来的面试做准备,以及记录一下平日里自己的学习过程和生活日常,我开始进驻博客园啦!这就是我的第一篇博客(有点小激动)~ 作为一只新手,首先记录一下今晚的编写博文的学习过程吧~ 2.使 ...

  10. sprint3总结 && sprint4计划

    sprint3总结 在一周时间里,逻辑部分顺利的将数据库,查词,UI部分连接到一起.并且各部分也针对新的要求做出了一些修改,目前数据库和查词alpha版已经完成,UI部分还需要一些美化,逻辑部分也还需 ...