本文介绍了Java三种方式实现接口校验,主要包括AOP, MVC拦截器,分享给大家,具体如下:
代码如下定义一个权限注解
包com.thinkgem.jeesite.common.annotation; 进口java.lang.annotation.ElementType; 进口java.lang.annotation.Retention; 进口java.lang.annotation.RetentionPolicy; 进口java.lang.annotation.Target;/* * *权限注解 *由汉明> @Aspect @ component 公开课AccessTokenAspect { @ autowired 私人ApiService ApiService; @Around (“@annotation (com.thinkgem.jeesite.common.annotation.AccessToken)”) 公共对象doAccessCheck (ProceedingJoinPoint pjp)抛出Throwable { HttpServletRequest请求=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes ()) .getRequest (); 字符串id=request.getParameter (" id "); 字符串标记=request.getParameter(“令牌”); 布尔验证=apiService.verifyToken (id、令牌); 如果(验证){ 对象对象=pjp.proceed ();//执行连接点方法//获取执行方法的参数 返回对象; 其他}{ 返回ResultApp.error(3”牌失效”); } } } >之前牌验证类,存储用到复述,
包com.thinkgem.jeesite.common.service; 进口com.thinkgem.jeesite.common.utils.JedisUtils; 进口io.jsonwebtoken.Jwts; 进口io.jsonwebtoken.SignatureAlgorithm; 进口io.jsonwebtoken.impl.crypto.MacProvider; 进口org.slf4j.Logger; 进口org.slf4j.LoggerFactory; 进口org.springframework.beans.factory.annotation.Autowired; 进口org.springframework.stereotype.Service; 进口org.springframework.transaction.annotation.Transactional; 进口redis.clients.jedis.Jedis; 进口. io . *; 进口java.security.Key; 进口java.util.Date;/* * *牌登陆验证 *由汉明; 公共静态关键键;//私人日志记录器=LoggerFactorygetLogger (getClass ());/* * *生成令牌 *以关键字节流形式存入复述 * * @param日期失效时间 * @param appId appId * @return */公共字符串generateToken(日期,日期字符串appId) { 能能=零; 尝试{ 能=JedisUtils.getResource (); byte [] buf=jedis.get (“api:关键”.getBytes ()); 如果(buf==null){//建新的关键 关键=MacProvider.generateKey (); ByteArrayOutputStream包=new ByteArrayOutputStream (); ObjectOutputStream oos=new ObjectOutputStream(包); oos.writeObject(关键); buf=bao.toByteArray (); jedis.set (“api:关键”.getBytes (), buf); 其他}{//重用老的关键 关键=(关键)新ObjectInputStream(新ByteArrayInputStream (buf)) .readObject (); } }捕捉(IOException io) {//System.out.println (io); }捕捉(ClassNotFoundException c) {//System.out.println (c); }捕捉(异常e) {//日志记录器。错误(“ApiService”、“generateToken”键,e); 最后}{ JedisUtils.returnResource(能); } 字符串标记=Jwts.builder () .setSubject (appId) .signWith (SignatureAlgorithm。HS512,键) .setExpiration(日期) .compact ();//计算失效秒,7889400秒三个月 日期temp=new日期(); 长间隔=(date.getTime ()——temp.getTime ())/1000; JedisUtils。集(+ appId,令牌,(int)间隔); 返回标记; }/* * *验证令牌 * @param appId appId * @param标记牌 * @return */appId公共布尔verifyToken(字符串,字符串标记){ 如果(appId==null | |令牌==null) { 返回错误; } 能能=零; 尝试{ 能=JedisUtils.getResource (); 如果(键==null) { byte [] buf=jedis.get (“api:关键”.getBytes ()); 如果(buf==null) { 返回错误; } 关键=(关键)新ObjectInputStream(新ByteArrayInputStream (buf)) readObject (); } Jwts.parser () .setSigningKey(键).parseClaimsJws(令牌).getBody () .getSubject () .equals (appId); 返回true; }捕捉(异常e) {//日志记录器。错误(“ApiService”、“verifyToken”键,e); 返回错误; 最后}{ JedisUtils.returnResource(能); } }/* * *获取令牌 * @param appId * @return */getToken公共字符串(字符串appId) { 能能=零; 尝试{ 能=JedisUtils.getResource (); 返回jedis.get (+ appId); }捕捉(异常e) {//日志记录器。错误(“ApiService”、“getToken”, e); 返回"; 最后}{ JedisUtils.returnResource(能); } } }浅谈Java三种方式实现接口校验