From b006f3fc406b0c9e7858611b816819619e79ec19 Mon Sep 17 00:00:00 2001 From: Francois Papon Date: Tue, 28 Jan 2020 22:14:26 +0100 Subject: [PATCH 1/2] [SHIRO-739] Bean reflection property failed with Enum values --- .../shiro/config/ReflectionBuilder.java | 34 ++++++++++++------- samples/web/src/main/webapp/WEB-INF/shiro.ini | 6 +++- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/config/ogdl/src/main/java/org/apache/shiro/config/ReflectionBuilder.java b/config/ogdl/src/main/java/org/apache/shiro/config/ReflectionBuilder.java index 1c3df1f7af..e88e5430eb 100644 --- a/config/ogdl/src/main/java/org/apache/shiro/config/ReflectionBuilder.java +++ b/config/ogdl/src/main/java/org/apache/shiro/config/ReflectionBuilder.java @@ -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; @@ -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 @@ -123,7 +123,17 @@ public ReflectionBuilder() { public ReflectionBuilder(Map 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(); diff --git a/samples/web/src/main/webapp/WEB-INF/shiro.ini b/samples/web/src/main/webapp/WEB-INF/shiro.ini index bd9fb7cc5a..1e3885ac06 100644 --- a/samples/web/src/main/webapp/WEB-INF/shiro.ini +++ b/samples/web/src/main/webapp/WEB-INF/shiro.ini @@ -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== @@ -56,4 +59,5 @@ goodguy = winnebago:drive:eagle5 /login.jsp = authc /logout = logout /account/** = authc -/remoting/** = authc, roles[b2bClient], perms["remote:invoke:lan,wan"] \ No newline at end of file +/remoting/** = authc, roles[b2bClient], perms["remote:invoke:lan,wan"] + From c105a4d644e77bb3f9fdc407d41f86c2fc082c52 Mon Sep 17 00:00:00 2001 From: Brian Demers Date: Tue, 28 Jan 2020 18:25:00 -0500 Subject: [PATCH 2/2] Add unit test for INI parsing with an enum --- .../shiro/config/ReflectionBuilderTest.groovy | 20 ++++++++++++++++ .../org/apache/shiro/config/SimpleBean.groovy | 1 + .../org/apache/shiro/config/SimpleEnum.groovy | 24 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleEnum.groovy diff --git a/config/ogdl/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy b/config/ogdl/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy index 211e2fadc4..500fb34abb 100644 --- a/config/ogdl/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy +++ b/config/ogdl/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy @@ -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 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 diff --git a/config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleBean.groovy b/config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleBean.groovy index 244ef481cb..6bc0cf2156 100644 --- a/config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleBean.groovy +++ b/config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleBean.groovy @@ -27,6 +27,7 @@ class SimpleBean { List simpleBeans; List stringList; Map mapProp = new LinkedHashMap(); + SimpleEnum simpleEnum; public SimpleBean(){} public SimpleBean(String name) { diff --git a/config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleEnum.groovy b/config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleEnum.groovy new file mode 100644 index 0000000000..ea18938887 --- /dev/null +++ b/config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleEnum.groovy @@ -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 +} \ No newline at end of file