时间:2021-05-20
菜单控制:
可以用来判断这个用户是不是有这些角色,没有的话就不展示
数据控制:
由于数据都是从后端查的,在后端控制权限就可以了
<!-- 开启权限控制注解支持 jsr250-annotations="enabled"表示支持jsr250-api的注解,需要jsr250-api的jar包 pre-post-annotations="enabled"表示支持spring表达式注解 secured-annotations="enabled"这才是SpringSecurity提供的注解 --> <security:global-method-security jsr250-annotations="enabled" pre-post-annotations="enabled" secured-annotations="enabled"/>注:这个要放在mvc的容器中,因为子容器可以访问到主容器,主容器访问不到子容器
/表示当前类中所有方法都需要ROLE_ADMIN或者ROLE_PRODUCT才能访问@Controller@RequestMapping("/product")@RolesAllowed({"ROLE_ADMIN","ROLE_PRODUCT"})//JSR-250注解public class ProductController {@RequestMapping("/findAll")public String findAll(){return "product-list";}}//表示当前类中findAll方法需要ROLE_ADMIN或者ROLE_PRODUCT才能访问@Controller@RequestMapping("/product")public class ProductController {@RequestMapping("/findAll")@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_PRODUCT')")//spring表达式注解public String findAll(){return "product-list";}}//表示当前类中所有方法都需要ROLE_ADMIN或者ROLE_PRODUCT才能访问@Controller@RequestMapping("/product")@Secured({"ROLE_ADMIN","ROLE_PRODUCT"})//SpringSecurity注解public class ProductController {@RequestMapping("/findAll")public String findAll(){return "product-list";}}但是会报403无法访问
方式一:在 spring-security.xml配置文件中处理
<!--设置可以用spring的el表达式配置Spring Security并自动生成对应配置组件(过滤器)--><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>方式三:编写异常处理器
/** * @author WGR * @create 2020/3/2 -- 17:33 */@ControllerAdvicepublic class ControllerExceptionAdvice { //只有出现AccessDeniedException异常才调转403.jsp页面 @ExceptionHandler(AccessDeniedException.class) public String exceptionAdvice(){ System.out.println("1234"); return "forward:/403.jsp"; }}注:如果是spring项目,也要把这个扫描进去。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文讲述springboot整合springsecurity在方法上使用注解实现权限控制,使用自定义userdetailservice,从mysql中加载用户信
最近项目需要用到SpringSecurity的权限控制,故花了点时间简单的去看了一下其权限控制相关的源码(版本为4.2)。AccessDecisionManag
Springsecurity实现权限管理示例,具体如下:1、配置文件1、POM.xml
1.前言我在SpringSecurity实战干货:内置Filter全解析对SpringSecurity的内置过滤器进行了罗列,但是SpringSecurity真
一、SPEL表达式权限控制从springsecurity3.0开始已经可以使用springExpression表达式来控制授权,允许在表达式中使用复杂的布尔逻辑