没有废话,直接上代码
@Configuration
@ConditionalOnProperty(name="baidu.enabled", havingValue="true")
@RefreshScope
public class EnableBaiduConfig {
@Value("${baidu.app_id}")
private String appId;
@Value("${baidu.api_key}")
private String apiKey;
@Value("${baidu.secret_key}")
private String secretKey;
@Bean
public BaiduService baiduIdent () {
return new BaiduServiceImpl(appId, apiKey, secretKey);
}
}
import com.baidu.aip.contentcensor.AipContentCensor;
@Service
public class BaiduServiceImpl implements BaiduService {
private static Logger log = LoggerFactory.getLogger(BaiduServiceImpl.class);
private String appId;
private String apiKey;
private String secretKey;
public BaiduServiceImpl(String appId, String apiKey, String secretKey) {
super();
this.appId = appId;
this.apiKey = apiKey;
this.secretKey = secretKey;
}
private AipContentCensor getClient() {
AipContentCensor contentCensorClient = new AipContentCensor(appId, apiKey, secretKey);
contentCensorClient.setConnectionTimeoutInMillis(2000);
contentCensorClient.setSocketTimeoutInMillis(60000);
return contentCensorClient;
}
@Override
public Boolean discernSensitiveWords(String text) {
AipContentCensor client = getClient();
String res;
try {
res = client.textCensorUserDefined(URLDecoder.decode(text, "UTF-8")).toString();
log.info("{百度鉴文}----------->" + res);
} catch (UnsupportedEncodingException e) {
return false;
}
// 标识审核是否通过的结果所在未知
int len = res.lastIndexOf("m\":") + 3;
String spam = res.substring(len, len + 1);
if (spam.equals("0")) {
return true;
} else {
return false;
}
}
}