做项目的一些小总结,rbac模型,aop表达式,拦截器,异常

rbac模型
在这里插入图片描述
在这里插入图片描述

spring aop
(1)、切面表达式:
execution(*[修饰符]? *[返回值] [包路径]?方法名throw *[异常类]?)

//匹配所有public修饰的方法
@Pointcut(“execution(public * **(…))”)
public void exTest1(){}

//com包下的所有类的方法
@Pointcut(“execution(* com..(…))”)
public void exTest3(){}

//com包下的所有包及子包的所有类的方法
@Pointcut(“execution(* com…*(…))”)
public void exTest4(){}

//匹配所有使用了AspectAnnotation注解的类的所有方法
@Pointcut("@target(com.tiglle.manage.AspectAnnotation)")
public void targetMatch(){}

//方法注解匹配,匹配所有带AspectAnnotation注解的方法
@Pointcut("@annotation(com.tiglle.manage.annotation.AspectAnnotation)")
public void test(){}
(2)、自定义注解
@Documented
@Retention(RetentionPolicy.RUNTIME) //运行时生效
@Target(ElementType.METHOD) // 只可以在方法上使用
public @interface ViewRecords {
String id() default “id”;
}
@Target:
作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
1.CONSTRUCTOR:用于描述构造器
    2.FIELD:用于描述域
    3.LOCAL_VARIABLE:用于描述局部变量
    4.METHOD:用于描述方法
    5.PACKAGE:用于描述包
    6.PARAMETER:用于描述参数
    7.TYPE:用于描述类、接口(包括注解类型) 或enum声明
@Retention:
作用:表示需要在什么级别保存该注释信息,用于描述注解的生命周期
1.SOURCE:在源文件中有效(即源文件保留)
    2.CLASS:在class文件中有效(即class保留)
    3.RUNTIME:在运行时有效(即运行时保留)
    
拦截器
(1)认证拦截器,一般实现HandlerInterceptor接口。
public class AuthInterceptor implements HandlerInterceptor(){}
(2)需要在配置文件或配置类中对拦截器进行配置
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//调用registry.addInterceptor() 方法注册该拦截器。
registry.addInterceptor(authenticationInterceptor())
.addPathPatterns("/") //拦截所有请求
.excludePathPatterns("/login/
"); //排除该路径
}
@Bean
public AuthInterceptor authenticationInterceptor() {
return new AuthInterceptor();
}
}
WebMvcConfigurer 接口中的常用方法
/* 拦截器配置 /
void addInterceptors(InterceptorRegistry var1);
/
视图跳转控制器 /
void addViewControllers(ViewControllerRegistry registry);
/
*
*静态资源处理
/
void addResourceHandlers(ResourceHandlerRegistry registry);
/
默认静态资源处理器 /
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
/

* 这里配置视图解析器
**/
void configureViewResolvers(ViewResolverRegistry registry);

全局常量
public class GlobalConstant {
/**
* 请求头中的token字段
*/
public final static String TOKEN = “msgToken”;

}

全局上下文
自定义缓存,可在登录成功后将数据存在Map中,当用到数据时直接从缓存中取出,不需要再查询数据库,提高了查询效率。因为缓存的容量是有限的,因此需要配合相应的淘汰机制(缓存淘汰算法),当容量达到上限时将最近最久未使用的用户信息删除。
@Component
public class GlobalContext {
/**
* 请求头中的token字段
* 用户id => 用户信息
*/
public final Map<Long, SysUserVo> userMap = new ConcurrentHashMap<>(32);
}

异常处理
继承 RuntimeException异常类后,可作为自定义异常类。

public class MsgmanageException extends RuntimeException{
private Integer code;
private String msg;
       public MsgmanageException(){}
public MsgmanageException(Integer code,String msg) {
  		  super(msg);
  		  this.code = code;
   		 this.msg = msg;
}
}

全局异常处理类用来接收异常

@ControllerAdvice          //该注解表示拦截所有带有@Controller注解的类,表示该类为异常处理类
public class GlobalException {
 	@ExceptionHandler(MsgmanageException.class)         //用来接收自定义异常
 	public String abnormalPermissions(Model model, HttpServletRequest request, Exception e ){
 	
     String message = e.getMessage();      //可以接收code和msg
     //接收访问路径
     model.addAttribute("url",request.getRequestURL());
     //接收MsgmanageException传来的异常信息
     model.addAttribute("exception",e);
     return "error/error";	    //返回错误页面
 }


	@ExceptionHandler(Exception.class)			//可以接收所有异常信息
	@ResponseBody
	public Object labRunException(Exception e) {
   		 log.error(e.getMessage(),e);
    	if(!HttpContextUtils.isAjax()){				//如果不是ajax类型的异常,返回页面
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/unknown");
        return mv;
    }else{									//如果为其他异常,返回json对象
        return ResultJson.error(e.getMessage());
    }
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值