Nutz中过滤特殊字符

##Servlet中有获取Request参数的方法,而Nutz中也有重写类似的方法,我们只要知道它如何得到RequestMap就可以处理请求中的参数,进而对它进行处理。


  • 在Nutz项目中的MainModule中配置你写的类(如AnyMobileActionFilter.class);
  • 把需要过滤的字符写在配置文件中(如:SCFilter.properties).
  • 编写AnyMobileActionFilter 它是记住要实现ActionFilter,这个是Nutz中的对请求统一处理的过滤器;

一、MainModule中配置过滤器:

@Filters({@By(type=AnyMobileActionFilter.class)})
public class MainModule {

}

二、在配置文件中配置需要过滤的字符:

SCFilter.properties
#有多个参数请用"|"号隔开,可以有多个KEY=Value
#Key的名字随意取
NAME=滚犊子
HELLO=go and fuck youself|fuck
SPEAK=不要说脏话

三、编写AnyMobileActionFilter:

package com.carforu.web.filter;

import java.text.Normalizer;
import java.util.List;
import java.util.Map;
import java.util.Set;


import org.nutz.ioc.Ioc;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionFilter;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.View;
import org.nutz.mvc.view.ForwardView;


public class AnyMobileActionFilter implements ActionFilter {

    /**
     * 遍历当前参数
     * 
     * @param Map
     *            <String, String[]> 获取的内容
     * @return bool boolean
     */
    public boolean SCFCheck(Map<String, String[]> originalQueryString) {
        boolean bool = true;
        if (originalQueryString != null) {
            for (String key : (Set<String>) originalQueryString.keySet()) {
                if (bool != false) {
                    String[] rawVals = originalQueryString.get(key);
                    for (int i = 0; i < rawVals.length; i++) {
                        bool = stripXSS(rawVals[i]);
                        if(bool == false) break;
                    }
                    bool = stripXSS(key);
                    if(bool == false) break;
                } else {
                    break;
                }
            }
        }
        return bool;
    }

    /**
     * 判断是否匹配
     * 
     * @param value
     *            当前要匹配内容
     * @return bool boolean
     */
    private boolean stripXSS(String value) {

        String cleanValue = null;
        Boolean bool = true;
        Ioc ioc = Mvcs.getIoc();
        PropertiesProxy scfilter = ioc.get(PropertiesProxy.class, "scfilter");

        if (value != null) {
            cleanValue = Normalizer.normalize(value, Normalizer.Form.NFD);

            List<String> fkeys = scfilter.getKeys();

            for (String fk : fkeys) {
                if (bool != false) {
                    String scfvalue = scfilter.get(fk);
                    String[] propertiesList = scfvalue.split("\\|");
                    for (int i = 0; i < propertiesList.length; i++) {
                        /*Pattern scfpattern = Pattern.compile(propertiesList[i],
                                Pattern.CASE_INSENSITIVE);
                        Matcher m = scfpattern.matcher(cleanValue);*/
                        int index = cleanValue.indexOf(propertiesList[i]);
                        if (index != -1) {
                            bool = false;
                            break;
                        }else{
                            bool = true;
                        }
                    }
                }else{
                    break;
                }

            }
        }
        return bool;
    }

    @Override
    public View match(ActionContext ac) {
        Map<String, String[]> originalQueryString = ac.getRequest()
                .getParameterMap();
        boolean bool = this.SCFCheck(originalQueryString);
        if (bool) {
            return null;
        } else {
            return new ForwardView("/unsafeCode.html");
        }
    }
}

这样你就可以随意配置要过滤的字符啦。

转载于:https://www.cnblogs.com/ChickenTang/p/5655404.html

### 如何在 Nutz 框架中利用 Guava 实现重发机制 为了实现在 Nutz 框架中的消息或请求重发机制,可以借助 Google 的 Guava 库来简化这一过程。Guava 提供了 `Retryer` 类用于处理重试逻辑。 #### 使用 Retrying 来构建重发机制 首先,在项目中引入必要的依赖项: ```xml <dependency> <groupId>com.github.rholder</groupId> <artifactId>guava-retrying</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.nutz</groupId> <artifactId>nutz</artifactId> <version>1.r.68</version> </dependency> ``` 接着创建一个简单的服务接口及其实现类,并在此基础上加入基于 Guava 的重试功能[^1]。 对于具体的业务方法而言,可以通过装饰器模式将其包裹在一个支持自动重试的方法调用里。下面是一个完整的例子说明如何做到这一点: ```java import com.google.common.base.Predicates; import com.github.rholder.retry.*; import org.nutz.dao.impl.NutDao; public class RetryService { private final NutDao dao; // 假设这是你要操作的数据访问对象 public RetryService(NutDao dao){ this.dao = dao; } /** * 尝试执行数据库查询并允许失败后重新尝试最多三次. */ public void queryWithRetries() throws Exception { // 定义当异常发生时应该采取的动作以及等待多久再做下一次尝试 WaitStrategy waitStrategy = new ExponentialWait StrategyBuilder() .withInitialWaitDuration(Duration.ONE_HUNDRED_MILLISECONDS) .withMaxWaitDuration(Duration.FIVE_SECONDS).build(); StopStrategy stopStrategy = StopStrategies.stopAfterAttempt(3); AttemptTimeLimiter<Runnable> timeLimiter = AttemptTimeLimiters.noTimeLimit(); Retryer<Void> retryer = RetryerBuilder.<Void>newBuilder() .retryIfException(Predicates.alwaysTrue()) .withWaitStrategy(waitStrategy) .withStopStrategy(stopStrategy) .withAttemptTimeLimiter(timeLimiter) .build(); try{ retryer.call(() -> { System.out.println("Executing database operation..."); // 执行实际的操作, 如果抛出了未捕获的运行期异常则会触发重试 dao.fetch(User.class, 1); return null; }); } catch (RetryGiveUpException e) { throw new RuntimeException("Failed after multiple attempts",e.getCause()); } } } ``` 上述代码展示了怎样通过配置不同的策略来自定义重试行为,比如设置最大重试次数、每次重试之间的间隔时间等参数。此外还指定了哪些类型的错误应当引起重试——这里选择了总是重试任何发生的异常情况。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值