下面列出了org.springframework.beans.ConfigurablePropertyAccessor#setPropertyValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (StringUtils.isBlank(value)) {
return true;
}
List<String> sentitiveWords = getContentChecker().check(value);
if (LangUtils.isEmpty(sentitiveWords)) {
return true;
}
ConstraintValidatorContextImpl ctx = (ConstraintValidatorContextImpl)context;
// 这种方法虽然可以动态显示错误信息,但是无法把错误信息放到i18n国际化资源文件里
// ctx.addExpressionVariable("invalidWords", StringUtils.join(sentitiveWords, ", "));
// ctx.buildConstraintViolationWithTemplate("${invalidWords}").addConstraintViolation();
Map<String, Object> newAttributes = Maps.newHashMap(ctx.getConstraintDescriptor().getAttributes());
newAttributes.put("invalidWords", StringUtils.join(sentitiveWords, ", "));
ConfigurablePropertyAccessor constraintDescriptor = SpringUtils.newPropertyAccessor(ctx.getConstraintDescriptor(), true);
constraintDescriptor.setPropertyValue("attributes", newAttributes);
return false;
}
protected void setPropertyValue(Object obj, ConfigurablePropertyAccessor bw, String propertyName, Object value){
if(!bw.isWritableProperty(propertyName)){
if(!ignoreNotFoundProperty){
throw new NoSuchElementException("no setter found for property: " + propertyName+", target: " + obj);
}
logger.debug("ignore property: {}={} ", propertyName, value);
return ;
}
bw.setPropertyValue(propertyName, value);
if(logger.isDebugEnabled()){
logger.debug("set property: {}={} ", propertyName, value);
}
}
protected void setPropertyValue(Object obj, ConfigurablePropertyAccessor bw, String propertyName, Object value){
if(!bw.isWritableProperty(propertyName)){
if(!ignoreNotFoundProperty){
throw new NoSuchElementException("no setter found for property: " + propertyName+", target: " + obj);
}
logger.debug("ignore property: {}={} ", propertyName, value);
return ;
}
bw.setPropertyValue(propertyName, value);
if(logger.isDebugEnabled()){
logger.debug("set property: {}={} ", propertyName, value);
}
}
@Override
public <O extends ClientCredentialsTokenEndpointFilter> O postProcess(O filter) {
ConfigurablePropertyAccessor filterAccessor = SpringUtils.newPropertyAccessor(filter, true);
if(oauth2ExceptionRenderer!=null){
filterAccessor.setPropertyValue("authenticationEntryPoint.exceptionRenderer", oauth2ExceptionRenderer);
}
/*AuthenticationManager origin = (AuthenticationManager)filterAccessor.getPropertyValue("authenticationManager");
DelegateAuthenticationManager delegate = new DelegateAuthenticationManager(origin);
filter.setAuthenticationManager(delegate);*/
if(tokenEndpointFilterInterceptor!=null){
filter = Proxys.intercept(filter, tokenEndpointFilterInterceptor);
}
return filter;
}