顶部引入标签库:
<%@taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
在需要面对不同权限的用户展示不同数据处添加:
<security:authorize access="hasAnyRole('ROLE_PRODUCT', 'ROLE_ADMIN')"> <li id="system-setting"><a href="${pageContext.request.contextPath}/product/findAll"> <i class="fa fa-circle-o"></i> 产品管理 </a></li> </security:authorize> <security:authorize access="hasAnyRole('ROLE_ORDER', 'ROLE_ADMIN')"> <li id="system-setting"><a href="${pageContext.request.contextPath}/order/findAll"> <i class="fa fa-circle-o"></i> 订单管理 </a></li> </security:authorize>
经过上面的操作,仅仅是对不同权限的用户显示不同的按钮,实际上权限并没有控制住,用户可以通过地址栏手动输入需要访问的地址,从而访问不在他权限范围内的数据
总结:页面动态菜单的展示只是为了用户体验,并未真正控制权限!
授权操作:
SpringSecurity可以通过注解的方式来控制类或者方法的访问权限。需要开启对应的注解支持,若注解放在controller类中,对应注解支持应该放在mvc配置文件中,因为controller类是由mvc配置文件扫描并创建的(子容器),同理,若注解放在service类中,对应注解支持应该放在spring(applicationContext或spring-security[父容器])配置文件中,与此同理的有spring声明式事务控制。建议放在service类中,因为父容器不会被http请求访问到,相对更安全一些,http请求只会访问子容器,再由子容器访问父容器,注意:父容器不能访问子容器
开启授权的注解支持:这里演示三类注解,但实际开发中,用一类即可!
<!-- 开启权限控制的注解支持 secured-annotations:SpringSecurity内部的权限控制注解开关 pre-post-annotations:spring指定的权限控制注解开关 jsr250-annotations:开启jsr250-api的注解,需要jsr250-api的jar包--> <security:global-method-security secured-annotations="enabled" pre-post-annotations="enabled" jsr250-annotations="enabled"/>
jsr250-api的支持
<dependency> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> <version>1.0</version> </dependency>
在注解支持对应类或者方法上添加注解:
@Controller // @Secured({"ROLE_PRODUCT", "ROLE_ADMIN"}) // SpringSecurity内部制定的注解 // @RolesAllowed({"ROLE_PRODUCT", "ROLE_ADMIN"}) // jsr250注解 @PreAuthorize("hasAnyRole('ROLE_PRODUCT', 'ROLE_ADMIN')") // spring的el表达式注解 @RequestMapping("/product") public class ProductController { @RequestMapping("/findAll") public String findAll(){ return "product-list"; } }
@Controller @RequestMapping("/order") public class OrderController { @Secured({"ROLE_ORDER", "ROLE_ADMIN"}) @RequestMapping("/findAll") public String findAll(){ return "order-list"; } }
权限不足异常处理:每次权限不足都出现403页面,着实难堪!
方式一:在 spring-security.xml配置文件中处理(只能处理403异常,其他异常无法处理)
<security:http auto-config="true" use-expressions="true"> <!--省略其它配置--> <!-- 处理403异常 --> <security:access-denied-handler error-page="/403.jsp"/> </security:http>
方式二:在 web.xml中处理
<error-page> <error-code>403</error-code> <location>/403.jsp</location> </error-page>
<error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page>
方式三:编写异常处理器(建议使用)
springmvc传统方式:
@Component public class HandlerControllerException implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { ModelAndView mv = new ModelAndView(); if (e instanceof AccessDeniedException) { mv.setViewName("redirect:/403.jsp"); } else { mv.setViewName("redirect:/500.jsp"); } return mv; } }
更简便方式:
@ControllerAdvice public class HandlerControllerAdvice { // 只有出现AccessDeniedException异常才跳转403.jsp页面 @ExceptionHandler(AccessDeniedException.class) public String handlerException(){ return "redirect:/403.jsp"; } @ExceptionHandler(RuntimeException.class) public String runtimeHandlerException(){ return "redirect:/500.jsp"; } @ExceptionHandler(Exception.class) public ModelAndView customException(Exception e) { ModelAndView mv = new ModelAndView(); mv.addObject("message", e.getMessage()); mv.setViewName("error"); return mv; } }
在该类中,可以定义多个方法,不同的方法处理不同的异常,例如专门处理空指针的方法、专门处理数组越界的方法...,也可以直接像上面最后的代码一样,在一个方法中处理所有的异常信息。
@ExceptionHandler 注解用来指明异常的处理类型,即如果这里指定为 NullpointerException,则数组越界异常就不会进到这个方法中来。