菜单控制:
可以用来判断这个用户是不是有这些角色,没有的话就不展示
数据控制:
由于数据都是从后端查的,在后端控制权限就可以了
<!--
开启权限控制注解支持
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
*/
@ControllerAdvice
public class ControllerExceptionAdvice {
//只有出现AccessDeniedException异常才调转403.jsp页面
@ExceptionHandler(AccessDeniedException.class)
public String exceptionAdvice(){
System.out.println("1234");
return "forward:/403.jsp";
}
}
注:如果是spring项目,也要把这个扫描进去。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。