自定义AccessDeniedHandler
在Spring默认的AccessDeniedHandler中只有对页面请求的处理,而没有对Ajax的处理。而在项目开发是Ajax又是我们要常用的技术,所以我们可以通过自定义AccessDeniedHandler来处理Ajax请求。我们在Spring默认的AccessDeniedHandlerImpl上稍作修改就可以了。
- public class DefaultAccessDeniedHandler implements AccessDeniedHandler {
- /* (non-Javadoc)
- * @see org.springframework.security.web.access.AccessDeniedHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.security.access.AccessDeniedException)
- */
- private String errorPage;
- //~ Methods ========================================================================================================
- public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)
- throws IOException, ServletException {
- boolean isAjax = ControllerTools.isAjaxRequest(request);
- if(isAjax){
- Message msg = MessageManager.exception(accessDeniedException);
- ControllerTools.print(response, msg);
- }else if (!response.isCommitted()) {
- if (errorPage != null) {
- // Put exception into request scope (perhaps of use to a view)
- request.setAttribute(WebAttributes.ACCESS_DENIED_403, accessDeniedException);
- // Set the 403 status code.
- response.setStatus(HttpServletResponse.SC_FORBIDDEN);
- // forward to error page.
- RequestDispatcher dispatcher = request.getRequestDispatcher(errorPage);
- dispatcher.forward(request, response);
- } else {
- response.sendError(HttpServletResponse.SC_FORBIDDEN, accessDeniedException.getMessage());
- }
- }
- }
- /**
- * The error page to use. Must begin with a "/" and is interpreted relative to the current context root.
- *
- * @param errorPage the dispatcher path to display
- *
- * @throws IllegalArgumentException if the argument doesn't comply with the above limitations
- */
- public void setErrorPage(String errorPage) {
- if ((errorPage != null) && !errorPage.startsWith("/")) {
- throw new IllegalArgumentException("errorPage must begin with '/'");
- }
- this.errorPage = errorPage;
- }
- }
这里我们直接将异常信息通过PrintWriter输出到前台,然后在前台做统一的处理就可以了。在前台对后台消息统一处理的方法可以参考我的这篇文章http://blog.csdn.net/jaune161/article/details/18135607
最后在配置文件中配置下
- <sec:http auto-config="true" access-decision-manager-ref="accessDecisionManager">
- <sec:access-denied-handler ref="accessDeniedHandler"/>
- <sec:session-management invalid-session-url="/login.jsp" />
- <sec:intercept-url pattern="/app.jsp" access="AUTH_LOGIN"/>
- <sec:intercept-url pattern="/**" access="AUTH_GG_FBGBGG"/>
- <sec:form-login login-page="/login.jsp" authentication-failure-url="/login.jsp"
- default-target-url="/index.jsp"/>
- </sec:http>
- <!-- 自定义权限不足处理程序 -->
- <bean id="accessDeniedHandler" class="com.zrhis.system.security.RequestAccessDeniedHandler">
- <property name="errorPage" value="/WEB-INF/error/403.jsp"></property>
- </bean>
自定义AccessDeniedHandler的更多相关文章
- OAuth2.0实战:认证、资源服务异常自定义!
大家好,我是不才陈某~ 这是<Spring Security 进阶>的第4篇文章,往期文章如下: 实战!Spring Boot Security+JWT前后端分离架构登录认证! 妹子始终没 ...
- Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】
源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...
- SpringBoot捕获AccessDeniedException
https://www.jianshu.com/p/bb14cca5ab3d 自定义AccessDeniedHandler /** * @Author: jialing xu * @Descripti ...
- 【JavaEE】SSH+Spring Security自定义Security的部分处理策略
本文建立在 SSH与Spring Security整合 一文的基础上,从这篇文章的example上做修改,或者从 配置了AOP 的example上做修改皆可.这里主要补充我在实际使用Spring Se ...
- Spring Security 入门(1-6-2)Spring Security - 内置的filter顺序、自定义filter、http元素和对应的filterChain
Spring Security 的底层是通过一系列的 Filter 来管理的,每个 Filter 都有其自身的功能,而且各个 Filter 在功能上还有关联关系,所以它们的顺序也是非常重要的. 1.S ...
- Spring Security Oauth2 自定义 OAuth2 Exception
付出就要得到回报,这种想法是错的. 前言 在使用Spring Security Oauth2登录和鉴权失败时,默认返回的异常信息如下 { "error": "unauth ...
- springboot+security整合(2)自定义校验
说明 springboot 版本 2.0.3源码地址:点击跳转 系列 springboot+security 整合(1) springboot+security 整合(2) springboot+se ...
- springboot2.0整合springsecurity前后端分离进行自定义权限控制
在阅读本文之前可以先看看springsecurity的基本执行流程,下面我展示一些核心配置文件,后面给出完整的整合代码到git上面,有兴趣的小伙伴可以下载进行研究 使用maven工程构建项目,首先需要 ...
- Spring Security学习笔记-自定义Spring Security过滤链
Spring Security使用一系列过滤器处理用户请求,下面是spring-security.xml配置文件. <?xml version="1.0" encoding= ...
随机推荐
- js-知识点
1.递归,数组选出最大值 var arr = [9,8,55,66,49,68,109,55,33,6,2,1]; var max = arr[0]; function findMax( i ){ i ...
- php 异步
.post(handle,{username: username.val(),content: content.val()},function(data){ },'json');
- win2008服务器,fastCGI完美设置教程
在WIN2008的IIS7上使用FASTCGI调用PHP-CGI.EXE,默认只有4个进程,这样对于大流量的网站为说,进程数不足带来的进程排队现象十分严重,解决方案如下.32位系统 http://ww ...
- c socket(续)
存在两种字节顺序:NBO与HBO 网络字节顺序NBO(Network Byte Order):按从高到低的顺序存储,在网络上使用统一的网络字节顺序,可以避免兼容性问题. 主机字节顺序(HBO,Host ...
- java 读取excel 正常 xls
package com.sun.test; import java.io.BufferedInputStream;import java.io.File;import java.io.FileInpu ...
- javascript 限制字符串字数换行 带BUG
function chang(str ,len) { function lenStat(str) { function isChinese(str) { //判断是不是中文 var reCh = /[ ...
- reshape2 数据操作 数据融合 (melt)
前面一篇讲了cast,想必已经见识到了reshape2的强大,当然在使用cast时配合上melt这种强大的揉数据能力才能表现的淋漓尽致. 下面我们来看下,melt这个函数以及它的特点. melt(da ...
- POJ 1042 Gone Fishing#贪心
(- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<cstring> using namespace std ...
- MongoDB数据模型(二)
原文地址 接上一篇 四.模型树结构 父引用的模型树结构 这个数据模型描述了一个树形结构,在子节点中存储父节点的引用. 模式 父引用模式存储每个树节点到文档中,除了树节点外,文档还存储了父节点的id. ...
- Linux C/C++编译环境搭建
1. 配置GCC,LInux 在安装后已经有GCC了,但可能文件不全,我们可以利用 sudo apt-get install build-essential 安装 2. 安装文本编辑器 sudo ap ...