在Spring默认的AccessDeniedHandler中只有对页面请求的处理,而没有对Ajax的处理。而在项目开发是Ajax又是我们要常用的技术,所以我们可以通过自定义AccessDeniedHandler来处理Ajax请求。我们在Spring默认的AccessDeniedHandlerImpl上稍作修改就可以了。

  1. public class DefaultAccessDeniedHandler implements AccessDeniedHandler {
  2. /* (non-Javadoc)
  3. * @see org.springframework.security.web.access.AccessDeniedHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.security.access.AccessDeniedException)
  4. */
  5. private String errorPage;
  6. //~ Methods ========================================================================================================
  7. public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)
  8. throws IOException, ServletException {
  9. boolean isAjax = ControllerTools.isAjaxRequest(request);
  10. if(isAjax){
  11. Message msg = MessageManager.exception(accessDeniedException);
  12. ControllerTools.print(response, msg);
  13. }else if (!response.isCommitted()) {
  14. if (errorPage != null) {
  15. // Put exception into request scope (perhaps of use to a view)
  16. request.setAttribute(WebAttributes.ACCESS_DENIED_403, accessDeniedException);
  17. // Set the 403 status code.
  18. response.setStatus(HttpServletResponse.SC_FORBIDDEN);
  19. // forward to error page.
  20. RequestDispatcher dispatcher = request.getRequestDispatcher(errorPage);
  21. dispatcher.forward(request, response);
  22. } else {
  23. response.sendError(HttpServletResponse.SC_FORBIDDEN, accessDeniedException.getMessage());
  24. }
  25. }
  26. }
  27. /**
  28. * The error page to use. Must begin with a "/" and is interpreted relative to the current context root.
  29. *
  30. * @param errorPage the dispatcher path to display
  31. *
  32. * @throws IllegalArgumentException if the argument doesn't comply with the above limitations
  33. */
  34. public void setErrorPage(String errorPage) {
  35. if ((errorPage != null) && !errorPage.startsWith("/")) {
  36. throw new IllegalArgumentException("errorPage must begin with '/'");
  37. }
  38. this.errorPage = errorPage;
  39. }
  40. }

这里我们直接将异常信息通过PrintWriter输出到前台,然后在前台做统一的处理就可以了。在前台对后台消息统一处理的方法可以参考我的这篇文章http://blog.csdn.net/jaune161/article/details/18135607

最后在配置文件中配置下

  1. <sec:http auto-config="true" access-decision-manager-ref="accessDecisionManager">
  2. <sec:access-denied-handler ref="accessDeniedHandler"/>
  3. <sec:session-management invalid-session-url="/login.jsp" />
  4. <sec:intercept-url pattern="/app.jsp" access="AUTH_LOGIN"/>
  5. <sec:intercept-url pattern="/**" access="AUTH_GG_FBGBGG"/>
  6. <sec:form-login login-page="/login.jsp" authentication-failure-url="/login.jsp"
  7. default-target-url="/index.jsp"/>
  8. </sec:http>
  9. <!-- 自定义权限不足处理程序 -->
  10. <bean id="accessDeniedHandler" class="com.zrhis.system.security.RequestAccessDeniedHandler">
  11. <property name="errorPage" value="/WEB-INF/error/403.jsp"></property>
  12. </bean>

自定义AccessDeniedHandler的更多相关文章

  1. OAuth2.0实战:认证、资源服务异常自定义!

    大家好,我是不才陈某~ 这是<Spring Security 进阶>的第4篇文章,往期文章如下: 实战!Spring Boot Security+JWT前后端分离架构登录认证! 妹子始终没 ...

  2. Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】

    源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...

  3. SpringBoot捕获AccessDeniedException

    https://www.jianshu.com/p/bb14cca5ab3d 自定义AccessDeniedHandler /** * @Author: jialing xu * @Descripti ...

  4. 【JavaEE】SSH+Spring Security自定义Security的部分处理策略

    本文建立在 SSH与Spring Security整合 一文的基础上,从这篇文章的example上做修改,或者从 配置了AOP 的example上做修改皆可.这里主要补充我在实际使用Spring Se ...

  5. Spring Security 入门(1-6-2)Spring Security - 内置的filter顺序、自定义filter、http元素和对应的filterChain

    Spring Security 的底层是通过一系列的 Filter 来管理的,每个 Filter 都有其自身的功能,而且各个 Filter 在功能上还有关联关系,所以它们的顺序也是非常重要的. 1.S ...

  6. Spring Security Oauth2 自定义 OAuth2 Exception

    付出就要得到回报,这种想法是错的. 前言 在使用Spring Security Oauth2登录和鉴权失败时,默认返回的异常信息如下 { "error": "unauth ...

  7. springboot+security整合(2)自定义校验

    说明 springboot 版本 2.0.3源码地址:点击跳转 系列 springboot+security 整合(1) springboot+security 整合(2) springboot+se ...

  8. springboot2.0整合springsecurity前后端分离进行自定义权限控制

    在阅读本文之前可以先看看springsecurity的基本执行流程,下面我展示一些核心配置文件,后面给出完整的整合代码到git上面,有兴趣的小伙伴可以下载进行研究 使用maven工程构建项目,首先需要 ...

  9. Spring Security学习笔记-自定义Spring Security过滤链

    Spring Security使用一系列过滤器处理用户请求,下面是spring-security.xml配置文件. <?xml version="1.0" encoding= ...

随机推荐

  1. notepad++正则表达式替换字符串详解

    正则表达式是一个查询的字符串,它包含一般的字符和一些特殊的字符,特殊字符可以扩展查找字符串的能力,正则表达式在查找和替换字符串的作用不可忽视,它 能很好提高工作效率. EditPlus的查找,替换,文 ...

  2. C1FlexGrid小结(转自http://www.cnblogs.com/C1SupportTeam/archive/2012/12/11/2812316.html)

    C1FlexGrid控件来对一个表格格式中的数据进行显示,编辑,组和总结.该表格可以绑定到一个数据源,它可以对自己的数据进行管理. C1FlexGrid控件有一个包含以下元素的丰富的对象模型: 以下的 ...

  3. Linux服务器导入导出SVN项目

    导出项目: # svnadmin dump /var/svn/pro1 > /mydata/pro1.backup 导入项目: 新建项目仓库: # svnadmin create /var/sv ...

  4. UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 26269: illegal multibyte sequence

    解决方法参见下面的链接: http://blog.csdn.net/jim7424994/article/details/22675759

  5. wps使用积累

    1.word加批注: 选中文字--插入--批注

  6. linux搭建phantomjs+webdriver+testng+ant自动化工程

    因为项目的原因,需要将脚本在linux环境无浏览器化去跑,那么原有的在windows系统下有浏览器化的自动化脚本场景就不适用了,这里给出linux系统下搭建phantomjs+webdriver+te ...

  7. 大数据时代之hadoop(三):hadoop数据流(生命周期)

    了解hadoop,首先就需要先了解hadoop的数据流,就像了解servlet的生命周期似的.hadoop是一个分布式存储(hdfs)和分布式计算框架(mapreduce),但是hadoop也有一个很 ...

  8. PAT 团体程序设计天梯赛-练习集 L1-017. 到底有多二

    一个整数“犯二的程度”定义为该数字中包含2的个数与其位数的比值.如果这个数是负数,则程度增加0.5倍:如果还是个偶数,则再增加1倍.例如数字“-13142223336”是个11位数,其中有3个2,并且 ...

  9. nginx读取图片没有权限

    场景是这样的,我们项目中上传图片到linux服务器后,保存图片存储路径和网络访问路径.app中用数据库返回的 图片访问路径来访问图片(nginx通过nfs方式读取图片).但是访问不到.要手动 chmo ...

  10. MVC5笔记【一】

    一.global.asax文件的作用:全局性配置文件 理解什么是路由? 有什么作用: 路由主要提供一个路由表 请求的时候被加载,请求url要去路由表当中去对照 规则 解析规则 控制器/动作放方法,转移 ...