当前位置: 首页 > wzjs >正文

孝感网站建设软件网站建设服务协议 百度

孝感网站建设软件,网站建设服务协议 百度,用什么做网站最好,平面设计兼职接单群LJF-Framework 第12章 LjfFilter拦截器设计 一、不成熟的想一下 当请求服务时,得进行拦截筛选、认证一下请求的合法性或者其他处理,我们自定义一个LjfFilter接口,然后实现该接口,当请求时,通过配置拦截顺序&#xff…

LJF-Framework 第12章 LjfFilter拦截器设计

一、不成熟的想一下

当请求服务时,得进行拦截筛选、认证一下请求的合法性或者其他处理,我们自定义一个LjfFilter接口,然后实现该接口,当请求时,通过配置拦截顺序,依次执行我们的自定义实现。

首先我们需要一个路由管理器,来管理请求路径

二、路由管理

1、LjfRouter

package com.ljf.framework.router;import com.ljf.framework.LjfManager;
import com.ljf.framework.fun.LjfParamFunction;import java.util.List;/*** 说明:路由匹配器** @Auther: lijinfeng* @Date: 2024/4/22*/
public class LjfRouter {private boolean isMatch;public boolean isContinueMatch() {return ! isMatch;}public LjfRouter(){this.isMatch = true;}/*** 路由匹配* @param pattern 路由匹配符* @param path 被匹配的路由* @return 是否匹配成功*/public boolean isMatch(String pattern, String path) {return LjfManager.getLjfContext().matchPath(pattern, path);}/*** 路由匹配* @param patterns 路由匹配符集合* @param path 被匹配的路由* @return 是否匹配成功*/public boolean isMatch(List<String> patterns, String path) {if(patterns == null) {return false;}for (String pattern : patterns) {if(isMatch(pattern, path)) {return true;}}return false;}/*** 路由匹配* @param patterns 路由匹配符数组* @param path 被匹配的路由* @return 是否匹配成功*/public boolean isMatch(String[] patterns, String path) {if(patterns == null) {return false;}for (String pattern : patterns) {if(isMatch(pattern, path)) {return true;}}return false;}// ------ 使用当前URI匹配/*** 路由匹配 (使用当前URI)* @param pattern 路由匹配符* @return 是否匹配成功*/public boolean isMatchCurrURI(String pattern) {return isMatch(pattern, LjfManager.getLjfContext().getRequestPath());}/*** 路由匹配 (使用当前URI)* @param patterns 路由匹配符集合* @return 是否匹配成功*/public boolean isMatchCurrURI(List<String> patterns) {return isMatch(patterns, LjfManager.getLjfContext().getRequestPath());}/*** 路由匹配 (使用当前URI)* @param patterns 路由匹配符数组* @return 是否匹配成功*/public boolean isMatchCurrURI(String[] patterns) {return isMatch(patterns, LjfManager.getLjfContext().getRequestPath());}public LjfRouter match(List<String> includeList) {if (isMatch){if (isMatchCurrURI(includeList)) {return this;}isMatch = false;}return this;}public LjfRouter notMatch(List<String> excludeList) {if (isMatch){if (!isMatchCurrURI(excludeList)) {return this;}isMatch = false;}return this;}public LjfRouter check(LjfParamFunction<LjfRouter> fun){if(isMatch)  {fun.run(this);}return this;}
}

像这样就可以对访问路径进行一些匹配判断

2、LjfRouterManager

package com.ljf.framework.router;import java.util.Arrays;
import java.util.List;/*** 说明:路由管理器** @Auther: lijinfeng* @Date: 2024/4/23*/
public class LjfRouterManager {/*** 创建路由* @return*/public static LjfRouter createRouter(){return new LjfRouter();}}

三、LjfContext更新

	/*** 校验指定路由匹配符是否可以匹配成功指定路径** @param pattern 路由匹配符* @param path 需要匹配的路径*/public boolean matchPath(String pattern, String path);

添加个路径匹配接口,方便进行请求的路径判断

四、LjfFilter

1、LjfFilter

package com.ljf.framework.filter;import java.util.List;/*** 说明:ljf 拦截器接口**  功能:指定拦截路劲、放行路劲,设置对拦截路劲的处理策略和异常策略** @Auther: lijinfeng* @Date: 2024/4/26*/
public interface LjfFilter {public Integer getOrder();/*** 获取 [拦截路由]**/public List getIncludeList();/*** 获取 [放行路由]**/public List getExcludeList();public LjfFilterAuthStrategy getBeforeAuthStrategy();public LjfFilterAuthStrategy getAuthStrategy();public LjfFilterErrorStrategy getErrorStrategy();
}

2、LjfAbstractFilter

package com.ljf.framework.filter;import com.ljf.framework.exception.LjfException;
import com.ljf.framework.exception.LjfExceptionEnum;
import com.ljf.framework.exception.LjfFilterException;import java.util.ArrayList;
import java.util.List;/*** 说明:客制化应用的filter可以实现改类*** @Auther: lijinfeng* @Date: 2024/4/23*/
public abstract class LjfAbstractFilter implements LjfFilter {// ------------------------ 设置此过滤器 拦截 & 放行 的路由protected abstract List<String> setIncludeList();protected abstract List<String> setExcludeList();/*** 获取 [拦截路由] 集合*/@Overridepublic List<String> getIncludeList() {return this.setIncludeList();}/*** 获取 [放行路由] 集合*/@Overridepublic List<String> getExcludeList() {return this.setExcludeList();}// ------------------------ 钩子函数protected abstract LjfFilterAuthStrategy setBeforeAuth();protected abstract LjfFilterAuthStrategy setAuthStrategy();/*** 异常处理函数:每次[认证函数]发生异常时执行此函数*/public LjfFilterErrorStrategy errorStrategy = e -> {throw new LjfFilterException(LjfExceptionEnum.FILTER_ERROR,e.getMessage());};@Overridepublic LjfFilterAuthStrategy getBeforeAuthStrategy() {return this.setBeforeAuth();}@Overridepublic LjfFilterAuthStrategy getAuthStrategy() {return this.setAuthStrategy();}@Overridepublic LjfFilterErrorStrategy getErrorStrategy() {return errorStrategy;}}

3、LjfFilterAuthStrategy

package com.ljf.framework.filter;/*** 全局过滤器-认证策略*/
public interface LjfFilterAuthStrategy {/*** 执行方法* @param r 无含义参数,留作扩展*/public boolean run(Object r);}

4、LjfFilterErrorStrategy

package com.ljf.framework.filter;/*** 全局过滤器-异常处理策略*/
public interface LjfFilterErrorStrategy {/*** 执行方法* @param e 异常对象* @return 输出对象(请提前序列化)*/public Object run(Throwable e);}

5、LjfFilterManager

package com.ljf.framework.filter;import com.ljf.framework.router.LjfRouterManager;import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.ServiceLoader;
import java.util.concurrent.atomic.AtomicBoolean;/*** 说明:拦截器管理器** @Auther: lijinfeng* @Date: 2024/4/23*/
public class LjfFilterManager {private static boolean isInit = false;private static List<LjfFilter> ljfFilterList = new ArrayList<>();public static List<LjfFilter> getLjfFilterList() {return ljfFilterList;}public static void addLjfFilter(LjfFilter ljfFilter) {LjfFilterManager.ljfFilterList.add(ljfFilter);}public static boolean doLjfFilter() {if(!isInit){loadFilter();isInit = true;}// 标记是否终止AtomicBoolean suc = new AtomicBoolean(true);for (LjfFilter filter : ljfFilterList) {// 执行全局过滤器LjfRouterManager.createRouter().match(filter.getIncludeList()).notMatch(filter.getExcludeList()).check(r -> {try {if (filter.getBeforeAuthStrategy().run(null)) {boolean toRun = filter.getAuthStrategy().run(null);if (!toRun){suc.set(false);}}} catch (Exception e) {filter.getErrorStrategy().run(e);suc.set(false);}});if (!suc.get())break;}return suc.get();}public static void loadFilter() {ServiceLoader<LjfFilter> serviceLoader = ServiceLoader.load(LjfFilter.class);//加载的是多个服务,遍历每个服务for (LjfFilter service : serviceLoader) {//取出一个服务,启动服务LjfFilterManager.addLjfFilter(service);}// 拦截器排序ljfFilterList.sort(Comparator.comparingInt(LjfFilter::getOrder));}public static void clearFilter() {ljfFilterList.clear();}}

五、测试一下

1、启动类

package com.ljf.test;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** 描述 :* <p>* 版本     作者     时间      内容* 1.0      lijinfeng       2025-03-24 09:47     create*/
@SpringBootApplication
public class LjfFilterTest {public static void main(String[] args) {SpringApplication.run(LjfFilterTest.class, args);}
}

2、配置拦截器

package com.ljf.test;import com.ljf.framework.filter.LjfFilterManager;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;import javax.servlet.*;
import java.io.IOException;/*** 描述 :* <p>* 版本     作者     时间      内容* 1.0      lijinfeng       2025-03-27 15:40     create*/
@Component
@Order(-100)
public class MyServletFilter implements Filter {@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {// 执行拦截器boolean continueRun = LjfFilterManager.doLjfFilter();if (!continueRun) return;// 继续执行filterChain.doFilter(servletRequest, servletResponse);}
}

3、写个测试实现类

package com.ljf.test;import com.ljf.framework.filter.LjfAbstractFilter;
import com.ljf.framework.filter.LjfFilterAuthStrategy;import java.util.Arrays;
import java.util.List;/*** 描述 :* <p>* 版本     作者     时间      内容* 1.0      lijinfeng       2025-03-27 15:44     create*/
public class LjfFilterDemo extends LjfAbstractFilter{@Overrideprotected List<String> setIncludeList() {return Arrays.asList("/**");}@Overrideprotected List<String> setExcludeList() {return Arrays.asList("/test/security/login");}@Overrideprotected LjfFilterAuthStrategy setBeforeAuth() {return r -> {System.out.println("是否需要执行拦截验证");return true;};}@Overrideprotected LjfFilterAuthStrategy setAuthStrategy() {return r -> {System.out.println("验证通过");return true;};}@Overridepublic Integer getOrder() {return 0;}
}

4、spi

在这里插入图片描述

com.ljf.test.LjfFilterDemo

5、查看效果

访问我们之前的登录接口,发现就没有输出我们设置的拦截日志。
在这里插入图片描述
访问退出接口,则显示了我们的拦截日志
在这里插入图片描述
到此我们想要的基本功能就有了,后面就需要继续完善更新了。


文章转载自:

http://lN9utYtH.wpcfh.cn
http://9ouCS2OJ.wpcfh.cn
http://xPnVRyqt.wpcfh.cn
http://15Ui8AGt.wpcfh.cn
http://7euDEkAK.wpcfh.cn
http://tn8b1AYw.wpcfh.cn
http://MgCNkIaQ.wpcfh.cn
http://YeDn5TO7.wpcfh.cn
http://iMIsMOYl.wpcfh.cn
http://anB10zC2.wpcfh.cn
http://UhYomugK.wpcfh.cn
http://Agtji6Ns.wpcfh.cn
http://oNX2DSvp.wpcfh.cn
http://gemsgevY.wpcfh.cn
http://GfijJqjs.wpcfh.cn
http://KolmJnYK.wpcfh.cn
http://xaS3Qgm0.wpcfh.cn
http://VKMpxbMc.wpcfh.cn
http://rt3vgaDn.wpcfh.cn
http://wCCmLq5W.wpcfh.cn
http://8kvTKb85.wpcfh.cn
http://RtXFJSLB.wpcfh.cn
http://C7REJyOU.wpcfh.cn
http://CnkpEnkp.wpcfh.cn
http://TD9xotAc.wpcfh.cn
http://5Po9ljfZ.wpcfh.cn
http://ykT93ymT.wpcfh.cn
http://wlqpnTTt.wpcfh.cn
http://nPNMP8AH.wpcfh.cn
http://txns5uHf.wpcfh.cn
http://www.dtcms.com/wzjs/739493.html

相关文章:

  • vue响应式网站开发有哪些官方网站做的比较好
  • 怎么免费注册自己的网站wordpress短信登陆
  • 男女做的那些事情的网站vue开发视频网站
  • 外贸建站 厦门沈阳网页模板建站
  • 卓伊科技网站建设flash网站免费源码带后台
  • 做电商网站价格湿地公园网站建设
  • 电子商务网站建设方案推荐做最好的网站新新
  • 网站的建设方面爱站权重查询
  • 柳州网站建设国际贸易
  • 优秀网站网页设计分析摄影做网站
  • 西平县住房城乡建设局网站网站运营需要 做哪些工作
  • 外贸网站开发建设一个网站的具体流程
  • 北仑网站建设培训学校雷山网站建设
  • 鲁山网站建设兼职简述网站的建设方案
  • 重庆模板建站哪家好安徽网站设计平台
  • 网站仿制公司烟台高新区网站
  • 宿州公司做网站域名代备案
  • 南宁学做网站做网站的时候卖过假货而出过事
  • 北京网站设计 培训学校软件开发的基本
  • 软件开发涵盖网站开发吗网站语言版本
  • 开发app定制网站内容优化技巧
  • 那个网站做图片好看的多少个网站
  • 网站在线解压wordpress模板dux主题
  • 一共有多少网站做网站挣钱打擦边球
  • 青岛做网站公司电话WordPress的电影播放器代码
  • 手机版网站怎么做的网站建设误区图
  • 来个网站好人有好报单一产品销售网站建设模板
  • 中山古镇做网站苏州公众号开发公司
  • 网页制作与网站建设实战大全 豆瓣网站开发汇报ppt模板
  • 做网站不推广平台设计图片