【问题标题】:How to redirect to same page from spring security AccessDeniedHandler如何从spring security AccessDeniedHandler重定向到同一页面
【发布时间】:2018-08-31 03:55:21
【问题描述】:

我在 Spring MVC 应用程序中创建了 CustomAccessDeneiedHandler 类,该应用程序实现了 AccessDeniedHandler 接口,如下所示。

    public class CustomAccessDeniedHandler implements AccessDeniedHandler {
     
        @Override
        public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException exc) throws IOException, ServletException {
            response.sendRedirect(request.getContextPath() + "/accessDenied");
        }
   }

这将使用"/accessDenied" URI 调用控制器GET 方法,该方法在内部返回特定于访问被拒绝错误的JSP 视图。

但是,我不想重定向到单独的视图,而是想在同一页面上显示一条警告消息,上面写着“您无权执行此任务”。

是否有任何解决方法可以让我们将响应发送回请求页面的来源并在该页面上显示错误消息? 我尝试了许多不同的方法,但似乎没有什么对我有用,因为我尝试的每个解决方案都在调用控制器方法并且正在重定向/重新呈现相同或不同的视图。

所以我还没有找到任何标准的解决方案来解决这个问题。任何帮助表示赞赏。

【问题讨论】:

    标签: spring jsp spring-mvc spring-security


    【解决方案1】:

    你可以在javascript的帮助下做到这一点,

    @RequestMapping( value ="/accessDeniedHandler")
    public String accessDeniedModal()
    {
    //Do something here and return modal
    
    return "accessDeniedModal";
    }
    

    和jsp这样,

    <html>
      <head>
        <script>
          function loadModalWindow() {
             // open your window here
             $('<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
       <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h3 id="myModalLabel">Access Denied</h3>
      </div>
        <div class="modal-body">
          <p> <Your message> <p>
       </div>
      <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    </div>
     </div>').modal('show'); 
          }
       </script>
       </head>
    
        <body onload="loadModalWindow()">
    
        </body>
     </html>
    

    瞧,你的模态将在 html 之上打开。

    【讨论】:

      猜你喜欢
      • 2017-05-27
      • 1970-01-01
      • 2012-09-12
      • 2014-12-01
      • 2014-07-22
      • 1970-01-01
      • 2019-03-24
      • 2014-07-11
      • 1970-01-01
      相关资源
      最近更新 更多