Skip to content

Commit

Permalink
Merge pull request #199 from fpapon/SHIRO-739
Browse files Browse the repository at this point in the history
[SHIRO-739] Bean reflection property failed with Enum values
  • Loading branch information
fpapon committed Jan 29, 2020
2 parents 8c6af13 + c105a4d commit aa126e2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
Expand Up @@ -18,7 +18,18 @@
*/
package org.apache.shiro.config;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.SuppressPropertiesBeanIntrospector;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.codec.Hex;
Expand All @@ -41,17 +52,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
* Object builder that uses reflection and Apache Commons BeanUtils to build objects given a
Expand Down Expand Up @@ -123,7 +123,17 @@ public ReflectionBuilder() {
public ReflectionBuilder(Map<String, ?> defaults) {

// SHIRO-619
beanUtilsBean = new BeanUtilsBean();
// SHIRO-739
beanUtilsBean = new BeanUtilsBean(new ConvertUtilsBean() {
@Override
public Object convert(String value, Class clazz) {
if (clazz.isEnum()){
return Enum.valueOf(clazz, value);
}else{
return super.convert(value, clazz);
}
}
});
beanUtilsBean.getPropertyUtils().addBeanIntrospector(SuppressPropertiesBeanIntrospector.SUPPRESS_CLASS);

this.interpolator = createInterpolator();
Expand Down
Expand Up @@ -561,6 +561,26 @@ class ReflectionBuilderTest {
assertDestroyedEvents("listenerTwo", objects, 3); //2 beans defined after it + its own destroyed event
}

/**
* SHIRO-739
*/
@Test
void testEnum() {
def ini = new Ini()
ini.load '''
simpleBean = org.apache.shiro.config.SimpleBean
simpleBean.name = testEnum
simpleBean.simpleEnum = FOO
'''

ReflectionBuilder builder = new ReflectionBuilder();
Map<String, ?> objects = builder.buildObjects(ini.getSections().iterator().next());
assertThat(objects, aMapWithSize(greaterThan(0)))

SimpleBean bean = objects.get("simpleBean")
assertThat(bean.name, is("testEnum"))
assertThat(bean.simpleEnum, is(SimpleEnum.FOO))
}

/**
* @since 1.4
Expand Down
Expand Up @@ -27,6 +27,7 @@ class SimpleBean {
List<SimpleBean> simpleBeans;
List<String> stringList;
Map<String,Object> mapProp = new LinkedHashMap<String,Object>();
SimpleEnum simpleEnum;

public SimpleBean(){}
public SimpleBean(String name) {
Expand Down
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.shiro.config

enum SimpleEnum {
FOO,
BAR
}
6 changes: 5 additions & 1 deletion samples/web/src/main/webapp/WEB-INF/shiro.ini
Expand Up @@ -27,9 +27,12 @@ shiro.loginUrl = /login.jsp
shiro.postOnlyLogout = true

sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
sessionManager.sessionIdCookie.sameSite = NONE

securityManager.sessionManager = $sessionManager
securityManager.sessionManager.sessionIdUrlRewritingEnabled = false


# We need to set the cipherKey, if you want the rememberMe cookie to work after restarting or on multiple nodes.
# YOU MUST SET THIS TO A UNIQUE STRING
securityManager.rememberMeManager.cipherKey = kPH+bIxk5D2deZiIxcaaaA==
Expand All @@ -56,4 +59,5 @@ goodguy = winnebago:drive:eagle5
/login.jsp = authc
/logout = logout
/account/** = authc
/remoting/** = authc, roles[b2bClient], perms["remote:invoke:lan,wan"]
/remoting/** = authc, roles[b2bClient], perms["remote:invoke:lan,wan"]

0 comments on commit aa126e2

Please sign in to comment.