Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

指定一个词不是敏感词,但是WordDenys.system()包含该敏感词,导致我这边敏感词校验还是认为这是敏感词 #22

Open
AlanFin opened this issue Jul 26, 2022 · 11 comments

Comments

@AlanFin
Copy link

AlanFin commented Jul 26, 2022

/**
 * 现在的情况是我想使用WordDenys.system()里面的敏感词汇,但是里面的部分我想去掉,比如WordDenys.system()里面包含“五星红旗”敏感词,我想
 * 将“五星红旗”设置为非敏感词,我就自己实现了IWordAllow类allow()中的方法,我将‘’五星红旗‘’写在这个方法里面初始化,可是初始化后我去校验“五
 * 星红旗”仍然为敏感词
 */
@Bean
public SensitiveWordBs sensitiveWordBs() {
    return SensitiveWordBs.newInstance()
        .wordDeny(WordDenys.chains(WordDenys.system(), myDdWordDeny))
        .wordAllow(WordAllows.chains(WordAllows.system(), myDdWordAllow))
        // 各种其他配置
        .init();
}
@ft0907
Copy link

ft0907 commented Jul 27, 2022

看下这个回复吧,这也太坑了!#20

@AlanFin
Copy link
Author

AlanFin commented Jul 27, 2022

看下这个回复吧,这也太坑了!#20
1、他这个不一样吧,他的的初始化后用wordBs 校验是否包含敏感词,校验存在后又使用SensitiveWordHelper去查询所有敏感词,他都不是使用同一个去校验的;
2、我的是初始化后其他地方直接注入 @Autowired private SensitiveWordBs sensitiveWordBs;然后统一使用sensitiveWordBs校验的还是会失败;

@ft0907
Copy link

ft0907 commented Jul 27, 2022

我和一样,通过spring 注入的就是不行,按正常来说应该是属于一个bean,但是它死活就是不行
尝试用一个静态变量去接收,然后init、使用都用这个静态变量去操作。估计可以

@AlanFin
Copy link
Author

AlanFin commented Jul 27, 2022

你去看下我刚刚上传的sensitive-word-admin这个demo吧,好像是可以,估计是我前面敏感词设置的不对,比如我设置的"安门"未非敏感词,但是打"天安门"这三个字会匹配未敏感词

@houbb
Copy link
Owner

houbb commented Jul 27, 2022

spring 整合可以阅读下:

https://mp.weixin.qq.com/s/rQo75cfMU_OEbTJa0JGMGg

@AlanFin
Copy link
Author

AlanFin commented Jul 27, 2022

spring 整合可以阅读下:

https://mp.weixin.qq.com/s/rQo75cfMU_OEbTJa0JGMGg

已经ok了,thank you

@ft0907
Copy link

ft0907 commented Jul 27, 2022

你确定你的代码可以,没有问题?下面是我的代码,但是一样的还是不行,永远返回false!

@RequiredArgsConstructor
public class SensitiveWordConfig {

    @Nonnull
    private final WordAllowImpl wordAllowImpl;
    @Nonnull
    private final WordDenyImpl wordDenyImpl;

    /**
     * 初始化
     *
     * @return 初始化
     */
    @Bean
    public SensitiveWordBs sensitiveWordBs() {
        return SensitiveWordBs.newInstance()
            .wordAllow(WordAllows.chains(WordAllows.system(), wordAllowImpl))
            .wordDeny(WordDenys.chains(WordDenys.system(), wordDenyImpl))
            .init();
    }

}

@Service
@RequiredArgsConstructor
public class WordDenyImpl implements IWordDeny {

    @Override
    public List<String> deny() {
        return Collections.singletonList("你好");
    }

}

// 这里是一个接口类
@Override
    public void createForWordDeny() {
        wordDenyImpl.deny();
        sensitiveWordBs.init();
        System.out.println(sensitiveWordBs.contains("你好"));//永远是false

    }

@AlanFin
Copy link
Author

AlanFin commented Jul 27, 2022

你确定你的代码可以,没有问题?下面是我的代码,但是一样的还是不行,永远返回false! `@Configuration @requiredargsconstructor public class SensitiveWordConfig {

@Nonnull
private final WordAllowImpl wordAllowImpl;
@Nonnull
private final WordDenyImpl wordDenyImpl;

/**
 * 初始化
 *
 * @return 初始化
 */
@Bean
public SensitiveWordBs sensitiveWordBs() {
    return SensitiveWordBs.newInstance()
        .wordAllow(WordAllows.chains(WordAllows.system(), wordAllowImpl))
        .wordDeny(WordDenys.chains(WordDenys.system(), wordDenyImpl))
        .init();
}

}`

`@Service @requiredargsconstructor public class WordDenyImpl implements IWordDeny {

@Override
public List<String> deny() {
    return Collections.singletonList("你好");
}

}`

    public void createForWordDeny() {
        wordDenyImpl.deny();
        sensitiveWordBs.init();
        System.out.println(sensitiveWordBs.contains("你好"));

    }```

你拉下我的demo来跑下,数据库连接改下,脚本也在里面 https://github.com/AlanFin/sensitive-word-admin.git

@houbb
Copy link
Owner

houbb commented Jul 27, 2022 via email

@ft0907
Copy link

ft0907 commented Jul 27, 2022

代码本身只是最基本的对象创建,和 spring 关系不大。 看一下是不是使用问题 发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: join001 @.> 发送时间: 2022年7月27日 15:02 收件人: houbb/sensitive-word @.> 抄送: 老马啸西风 @.>, Comment @.> 主题: Re: [houbb/sensitive-word] 指定一个词不是敏感词,但是WordDenys.system()包含该敏感词,导致我这边敏感词校验还是认为这是敏感词 (Issue #22) @.注入才行,构造函数注入就不行!这作者要改进下,应该是上下文获取方式不太对! — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.>

找到问题,是因为没有引入数据库实现!具体修改的代码如下

    @Nonnull
    private final SensitiveWordRepository sensitiveWordRepository; 

@Override
public List<String> deny() {
    return sensitiveWordRepository.findAll(); // 这里配置数据库读取值
}

public void createForWordDeny() {
       // wordDenyImpl.deny(); //这行代码也不需要,就可以了
        sensitiveWordBs.init();
        System.out.println(sensitiveWordBs.contains("你好"));
    }

@ft0907
Copy link

ft0907 commented Jul 28, 2022

@houbb 源代码文件SensitiveWordMap.java文件中的System.out.println能否去掉啊?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants