前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Thymeleaf异常处理Calling methods is forbidden for type

Thymeleaf异常处理Calling methods is forbidden for type

作者头像
路过君
发布2023-05-01 10:07:39
2700
发布2023-05-01 10:07:39
举报

版本

thymeleaf 3.0.15

现象

在thymeleaf模板中通过${session.SPRING_SECURITY_LAST_EXCEPTION.getMessage()}获取异常消息时报错

Caused by: org.springframework.expression.EvaluationException: Calling methods is forbidden for type ‘java.lang.RuntimeException’ in Thymeleaf expressions. Blacklisted classes are: [java.util.concurrent.RunnableFuture, java.util.concurrent.Executor, java.lang.Runtime, java.util.concurrent.FutureTask, java.util.concurrent.ListenableFuture, java.lang.Runnable, java.util.concurrent.Future, java.lang.Thread, java.lang.reflect.Executable, java.lang.Class, java.lang.ClassLoader, java.sql.DriverManager].

原因

在thymeleaf 3.0.15中将java.lang.Runtime前缀的类加入了黑名单不允许在模板中访问

解决

异常以Exception抛出不要以RuntimeException抛出,其他类似问题通解

源码

  • 包thymeleaf-spring5-3.0.15

org.thymeleaf.spring5.expression.ThymeleafEvaluationContext

代码语言:javascript
复制
static final class ThymeleafEvaluationContextACLMethodResolver extends ReflectiveMethodResolver {

        ThymeleafEvaluationContextACLMethodResolver() {
            super();
        }

        @Override
        public MethodExecutor resolve(
                final EvaluationContext context, final Object targetObject,
                final String name, final List<TypeDescriptor> argumentTypes) throws AccessException {

            final Class<?> type = (targetObject instanceof Class ? (Class<?>) targetObject : targetObject.getClass());
            // 判断对象是否时允许访问的类型
            if (!ExpressionUtils.isTypeAllowed(type.getName())) {
                // We will only specifically allow calling "Object.getClass()" and "Class.getName()"
                if (!(Class.class.equals(type) && "getName".equals(name))
                        && !(Object.class.equals(type) && "getClass".equals(name))) {
                    throw new EvaluationException(
                            String.format(
                                    "Calling methods is forbidden for type '%s' in Thymeleaf expressions. " +
                                    "Blacklisted classes are: %s.",
                                    type.getName(), ExpressionUtils.getBlacklist()));
                }
            }
            return super.resolve(context, targetObject, name, argumentTypes);
        }

    }
  • 包thymeleaf-3.0.15 org.thymeleaf.util. ExpressionUtils
代码语言:javascript
复制
public final class ExpressionUtils {
	// 所有黑名单类名前缀
    private static final Set<String> BLOCKED_CLASS_NAME_PREFIXES =
            new HashSet<String>(Arrays.asList(
                    "java.lang.Runtime", "java.lang.Thread", "java.lang.Class", "java.lang.ClassLoader",
                    "java.lang.Runnable", "java.lang.reflect.Executable",
                    "java.util.concurrent.Future", "java.util.concurrent.FutureTask",
                    "java.util.concurrent.RunnableFuture", "java.util.concurrent.ListenableFuture",
                    "java.util.concurrent.Executor",
                    "java.sql.DriverManager"));

    public static boolean isTypeAllowed(final String typeName) {
        Validate.notNull(typeName, "Type name cannot be null");
        final int i0 = typeName.indexOf('.');
        if (i0 >= 0) {
            final String package0 = typeName.substring(0, i0);
            if ("java".equals(package0)) { // This is the only prefix that might be blocked
                for (final String prefix : BLOCKED_CLASS_NAME_PREFIXES) {
                    if (typeName.startsWith(prefix)) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
    public static List<String> getBlockedClasses() {
        final List<String> blocked = new ArrayList<String>();
        blocked.addAll(BLOCKED_CLASS_NAME_PREFIXES);
        return blocked;
    }
    private ExpressionUtils() {
        super();
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-04-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 版本
  • 现象
  • 原因
  • 解决
  • 源码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档