博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC+shiro权限拦截(不使用shiro注解方式)
阅读量:7197 次
发布时间:2019-06-29

本文共 1100 字,大约阅读时间需要 3 分钟。

  hot3.png

        一.使用AOP和自定义注解

        自定义注解:

@Retention(RetentionPolicy.RUNTIME)  @Target({ElementType.METHOD})public @interface CustomCheckRole {	String[] roles();  //传入多个角色}

        编写AOP, 拦截在controller包下注解有@CustomCheckRole的方法, 然后在方法中拦截没有注解中标记了的角色的用户, 抛出没有shiro没有权限的异常.

@Aspect  @Component  public class AnnotationRoleInterceptor {	 @Around("execution(* com.fgw.cms.controller..*(..)) && @annotation(customCheckRole)")	 public Object doInterceptor(ProceedingJoinPoint pjp, CustomCheckRole customCheckRole) throws Throwable {		 boolean hasRole = false;		 Subject currentUser = SecurityUtils.getSubject();		 if(customCheckRole != null) {			 String[] roles = customCheckRole.roles();			 for(String role : roles) {				 if(currentUser.hasRole(role)) {					 hasRole = true;					 break;				 }			 }		 }else {			 hasRole = true;		 }		 System.out.println("(AOP)拦截到了:"+pjp.getSignature().getName());  		 if(hasRole) {			 return pjp.proceed();		 }else {			 throw new AuthorizationException();		 }	 }}

        然后根据上一篇配置的全局异常拦截处理器, 会执行对应的方法.

        二.使用springmvc拦截器

        自定义拦截器,在preHandler中定义自己的逻辑, 和上面的实现差不多,拦截器也是一种AOP.

转载于:https://my.oschina.net/kkdo/blog/746122

你可能感兴趣的文章
Educational Codeforces Round 9 C. The Smallest String Concatenation 排序
查看>>
mysql binlog解析概要
查看>>
jquery技巧之让任何组件都支持类似DOM的事件管理
查看>>
windows下配置两个或多个Tomcat启动的方法
查看>>
设计模式(七): 通过转接头来观察"适配器模式"(Adapter Pattern)
查看>>
C#运算符大全_各种运算符号的概述及作用
查看>>
移动web开发之像素和DPR
查看>>
排序算法总结之快速排序
查看>>
JAVA 爬虫Gecco
查看>>
iOS开发之三个Button实现图片无限轮播(参考手机淘宝,Swift版)
查看>>
JAVA-开发IDE版本
查看>>
$.ajax()方法详解
查看>>
【Coursera】Fourth Week(1)
查看>>
自定义控件
查看>>
C10K 问题原文
查看>>
BoneCP 升级遇到的问题
查看>>
常用chrome插件推荐
查看>>
python的最最最最最基本语法(3)
查看>>
随机抽样一致性算法(RANSAC)
查看>>
[LeetCode] Repeated Substring Pattern 重复子字符串模式
查看>>