我们在开发区和我们当地的WAS之间有所不同
我们的应用程序使用Spring-Security和Spring 3.1.0版本。
它从JSP文件开始,我们试图显示连接的用户名:
在DEV中的WAS中,我们有一个NotReadablePropertyException: “Bean属性'principal'不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配?”
这是因为AuthenticationTag正在使用BeanWrapper(BeanWrapperImpl)
在文件BeanWrapperImpl的第729行(Spring 3.1.0-RELEASE)中抛出错误,
727:PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName);
728:if (pd == null || pd.getReadMethod() == null) {
729: throw new NotReadablePropertyException(getRootClass(), this.nestedPath + propertyName);
730:}
因此pd为null或pd.getReadMethod()为null。 在正常情况下,getReadMethod返回: 对象方法[public java.lang.Object org.springframework.security.authentication.UsernamePasswordAuthenticationToken.getPrincipal()]
更新 在调试并在类中添加一些日志之后,似乎“pd”为空
我在纯java中编写了这段代码来识别错误
logger.info(session.getAttribute("SPRING_SECURITY_CONTEXT").getClass().toString());
final SecurityContextImpl sci = ((SecurityContextImpl) session.getAttribute("SPRING_SECURITY_CONTEXT"));
logger.info(sci.getAuthentication().getClass().toString());
final Authentication auth = sci.getAuthentication();
logger.info(auth.getPrincipal().getClass().toString());
final User u = (User) auth.getPrincipal();
logger.info(u.getUsername());
logger.info(SecurityContextHolder.getContext().getAuthentication().getName());
logger.info("use beanWrapper :");
final BeanWrapperImpl wrapper = new BeanWrapperImpl(auth);
String property = "principal";
Object result = wrapper.getPropertyValue(property);
logger.info("property : " + property + " value :[" + result.toString() + "]");
property = "principal.username";
result = wrapper.getPropertyValue(property);
logger.info("property : " + property + " value :[" + result.toString() + "]");
在dev中登录我们的WAS:
[2012-01-12 12:23:19,843] INFO [WebContainer : 8] [c.b.e.e.w.c.IndexController] class org.springframework.security.core.context.SecurityContextImpl
[2012-01-12 12:23:19,843] INFO [WebContainer : 8] [c.b.e.e.w.c.IndexController] class org.springframework.security.authentication.UsernamePasswordAuthenticationToken
[2012-01-12 12:23:19,843] INFO [WebContainer : 8] [c.b.e.e.w.c.IndexController] class org.springframework.security.core.userdetails.User
[2012-01-12 12:23:19,843] INFO [WebContainer : 8] [c.b.e.e.w.c.IndexController] superadmin
[2012-01-12 12:23:19,843] INFO [WebContainer : 8] [c.b.e.e.w.c.IndexController] superadmin
[2012-01-12 12:23:19,843] INFO [WebContainer : 8] [c.b.e.e.w.c.IndexController] use beanWrapper :
< 500错误>
登录我们当地的是:
[2012-01-13 08:51:10,062] INFO [WebContainer : 4] [c.b.e.e.w.c.IndexController] class org.springframework.security.core.context.SecurityContextImpl
[2012-01-13 08:51:10,062] INFO [WebContainer : 4] [c.b.e.e.w.c.IndexController] class org.springframework.security.authentication.UsernamePasswordAuthenticationToken
[2012-01-13 08:51:10,062] INFO [WebContainer : 4] [c.b.e.e.w.c.IndexController] class org.springframework.security.core.userdetails.User
[2012-01-13 08:51:10,062] INFO [WebContainer : 4] [c.b.e.e.w.c.IndexController] superadmin
[2012-01-13 08:51:10,073] INFO [WebContainer : 4] [c.b.e.e.w.c.IndexController] superadmin
[2012-01-13 08:51:10,073] INFO [WebContainer : 4] [c.b.e.e.w.c.IndexController] use beanWrapper :
[2012-01-13 08:51:10,095] INFO [WebContainer : 4] [c.b.e.e.w.c.IndexController] property : principal value :[org.springframework.security.core.userdetails.User@99ac08b4: Username: superadmin; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: SuperAdmin]
[2012-01-13 08:51:10,095] INFO [WebContainer : 4] [c.b.e.e.w.c.IndexController] property : principal.username value :[superadmin]
我们的开发中的WAS是完整版,
我们的本地WAS是一款轻量级的免费开发版
更新
在编写一个变通方法以获取userName之后,问题就会在未来发生,仍然使用BeenWrapperImpl
org.springframework.beans.NotReadablePropertyException: Invalid property 'codeAndName' of bean class [com.data.model.Country]: Bean property 'codeAndName' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:729)
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:721)
at org.springframework.web.servlet.tags.form.OptionWriter.doRenderFromCollection(OptionWriter.java:216)
at org.springframework.web.servlet.tags.form.OptionWriter.renderFromCollection(OptionWriter.java:186)
at org.springframework.web.servlet.tags.form.OptionWriter.writeOptions(OptionWriter.java:139)
at org.springframework.web.servlet.tags.form.OptionsTag.writeTagContent(OptionsTag.java:169)
更新 2:
问题更加孤立: 没有任何春天安全的依赖性,只有springmvc,
当在beanWrapper中放入没有与getter相对应的setter时,它只会在dev中的is抛出NotReadablePropertyException。
当有一个setter时,就没有问题了。 假设类UsernamePasswordAuthenticationToken是一个来自spring security.class的类。
答案 0 :(得分:0)
问题是由于引入了ExtendedBeanInfo inf spring3.1
...
所以解决方法是采用以前版本的CachedIntrospectionResults 并将其放入“org.springframework.beans”包中,以便覆盖它,
但你必须确保首先采用应用程序类路径。
答案 1 :(得分:0)
尝试使用BeanWrapper设置嵌套属性时遇到了同样的问题。问题是该物业没有一个二传手,尽管我认为这不是必须的。添加setter修复了我的问题。感谢您提供Benoit的更新! 我的豆子:
public class Bean {
private InnerBean innerBean = new InnerBean();
public InnerBean getInnerBean() {
return this.innerBean;
}
}
我的BeanWrapper代码失败了:
BeanWrapper wrapper = new BeanWrapperImpl(new Bean());
wrapper.setPropertyValue("innerBean.property","some value");