小编xti*_*ian的帖子

如何使用Spring Security很好地处理文件上载MaxUploadSizeExceededException

我正在使用Spring Web 4.0.5,Spring Security 3.2.4,Commons FileUpload 1.3.1,Tomcat 7,MaxUploadSizeExceededException当我超出上传大小限制时,我会变得很丑,这会导致"500内部服务器错误".我用一个很好的通用弹出窗口来处理它,但我宁愿让我的Controller通过返回原始形式并使用正确的解释消息来处理它.

我已经多次看到类似的问题了,一些解决方案在不使用Spring Security时可能会有效; 没有一个我试过的人为我工作.

问题可能在于使用Spring Security时,CommonsMultipartResolver不会将其添加为"multipartResolver"bean,而是添加为"filterMultipartResolver":

@Bean(name="filterMultipartResolver")
CommonsMultipartResolver filterMultipartResolver() {
    CommonsMultipartResolver filterMultipartResolver = new CommonsMultipartResolver();
    filterMultipartResolver.setMaxUploadSize(MAXSIZE);
    return filterMultipartResolver;
}
Run Code Online (Sandbox Code Playgroud)

如果我设置filterMultipartResolver.setResolveLazily(true);它没有任何区别.

如果我CommonsMultipartResolver使用我自己的子类并使用parseRequest()陷阱的方法覆盖该方法MaxUploadSizeExceededException并返回一个空MultipartParsingResult,我得到一个"403 Forbidden"错误:

public class ExtendedCommonsMultipartResolver extends CommonsMultipartResolver {
    protected MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException {
        String encoding = determineEncoding(request);
        try {
            return super.parseRequest(request);
        } catch (MaxUploadSizeExceededException e) {
            return parseFileItems(Collections.<FileItem> emptyList(), encoding);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,实现某种本地或全局ExceptionHandler是没有意义的,因为它从未被调用过.

如果我找不到更好的解决方案,我只需删除上传大小限制并在控制器中自行处理,但缺点是让用户等到上传完成后再看到有关文件大小的错误消息.我甚至可能会忽略所有这一切,因为在这种情况下它是一个图像,我可以将其调整到适当的值. …

spring file-upload spring-mvc spring-security

18
推荐指数
1
解决办法
3万
查看次数

在Hibernate中映射本地化字符串 - 任何最佳实践?

我正在编写一个必须本地化所有String属性的应用程序,即它们必须为每个可用的Locale存储不同的值.一个快速的解决方案是使用Map,它可以在Hibernate中轻松映射,但对Java程序员来说不是很好:

public class Product {
   private Map<Locale, String> description;
   private Map<Locale, String> note;
Run Code Online (Sandbox Code Playgroud)

因此,我实现了一个LocalString对象,可以为不同的语言环境保存不同的字符串:

public class LocalString {
   private Map<Locale, String> localStrings;
Run Code Online (Sandbox Code Playgroud)

域对象变为

public class Product {
   private LocalString description;
   private LocalString note;
Run Code Online (Sandbox Code Playgroud)

如何使用Hibernate注释最好地映射这些对象?

我认为最好的映射将使用LocalString作为组件完成:

@Embeddable
public class LocalString {
    private Map<Locale, String> localStrings;

    @ElementCollection
    public Map<Locale, String> getLocalStrings() {
       return localStrings;
    }
Run Code Online (Sandbox Code Playgroud)

...

@Entity
public class Product {
   private Long id;
   private LocalString description;
   private LocalString note;

   @Embedded
   public LocalString getDescription() {
          return description;
   }
Run Code Online (Sandbox Code Playgroud)

到目前为止一切正常:hbm2ddl ant任务创建了两个表,一个"Products"表和一个"Products_localStrings"表,其中包含键和值列.当我为第二个属性添加getter时,一切都会中断:

   @Embedded
   public …
Run Code Online (Sandbox Code Playgroud)

schema hibernate localization internationalization hibernate-mapping

8
推荐指数
1
解决办法
3441
查看次数

如何使用Spring提交错误报告?

我无法找到提交错误报告的方法.我只想报告当前Spring Data文档中的错误.

spring spring-data

7
推荐指数
2
解决办法
1644
查看次数

将 logback 从 1.2 升级到 1.3 停止记录

如果我使用任何 1.3.x 或 1.4.x logback-classic 版本,我的应用程序将停止日志记录。logback.xml 文件可能甚至没有加载(如果我插入无效的 xml 或删除它,我不会收到错误)。

它与 logback 1.2.11 配合良好。

我正在使用 gradle 自动依赖解析,所以我不应该弄乱它们。

这有效:

logback-classic-1.2.11.jar + logback-core-1.2.11.jar + slf4j-api-1.7.32.jar

这不起作用:

logback-classic-1.3.3.jar + logback-core-1.3.3.jar + slf4j-api-2.0.1.jar

Eclipse 2022-09 (4.25.0) 中 Windows 10 上的 java 8

我的类路径:

activation-1.1.jar
annotations-13.0.jar
antlr-2.7.7.jar
attoparser-2.0.5.RELEASE.jar
byte-buddy-1.10.22.jar
checker-compat-qual-2.5.5.jar
checkout-sdk-1.0.5.jar
classmate-1.5.1.jar
commons-beanutils-1.9.4.jar
commons-codec-1.11.jar
commons-collections-3.2.2.jar
commons-configuration2-2.8.0.jar
commons-exec-1.3.jar
commons-fileupload-1.3.3.jar
commons-io-2.11.0.jar
commons-jxpath-1.3.jar
commons-lang3-3.12.0.jar
commons-logging-1.2.jar
commons-text-1.9.jar
concurrentlinkedhashmap-lru-1.4.2.jar
error_prone_annotations-2.5.1.jar
failureaccess-1.0.1.jar
FastInfoset-1.2.15.jar
flyway-core-5.0.5.jar
google-api-client-1.32.1.jar
google-api-client-jackson2-1.32.1.jar
google-http-client-1.39.2.jar
google-http-client-apache-v2-1.39.2.jar
google-http-client-gson-1.39.2.jar
google-oauth-client-1.31.5.jar
grpc-context-1.27.2.jar
gson-2.8.6.jar
guava-30.1.1-android.jar
hamcrest-core-1.3.jar
hibernate-commons-annotations-5.1.2.Final.jar
hibernate-core-5.5.0.Final.jar
hibernate-entitymanager-5.5.0.Final.jar
hibernate-validator-5.4.2.Final.jar
httpclient-4.5.13.jar
httpcore-4.4.14.jar
istack-commons-runtime-3.0.7.jar
j2objc-annotations-1.3.jar …
Run Code Online (Sandbox Code Playgroud)

logback

7
推荐指数
1
解决办法
6891
查看次数

从thymeleaf电子邮件模板访问应用程序上下文bean

我遵循了Thymeleaf教程“ Spring中带有Thymeleaf的Rich HTML电子邮件 ”中的内容,以使用Thymeleaf模板生成电子邮件。一切都很好,但是我无法访问在应用程序中其他地方使用的ApplicationContext bean。

更具体地说,在我的电子邮件模板中,我想要类似以下内容:

<span th:text="${@myBean.doSomething()}">
Run Code Online (Sandbox Code Playgroud)

其中“ myBean”是一个@Component。那有可能吗?我不是在寻找类似的解决方法

<span th:text="${myBean.doSomething()}">
Run Code Online (Sandbox Code Playgroud)

其中将bean作为变量添加到模板处理上下文中。

配置:

@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
public class MyWebConfig extends WebMvcConfigurerAdapter {
[....]
public ClassLoaderTemplateResolver emailTemplateResolver() {
    ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
    resolver.setPrefix("emailtemplates/");
    resolver.setSuffix(".html");
    resolver.setCharacterEncoding("UTF-8");
    resolver.setTemplateMode("HTML5");
    return resolver;
}

@Bean
public SpringTemplateEngine emailTemplateEngine() {
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.addTemplateResolver(emailTemplateResolver());
    engine.addDialect(new LayoutDialect()); // thymeleaf-layout-dialect
    addSpringSecurityDialect(engine); // thymeleaf-SpringSecurity-dialect
    return engine;
}
[....]
}
Run Code Online (Sandbox Code Playgroud)

电子邮件服务:

@Service
public class MyEmailService {
@Resource SpringTemplateEngine emailTemplateEngine;
[....]
public boolean sendHtmlEmail(...) {
    final Context ctx …
Run Code Online (Sandbox Code Playgroud)

spring thymeleaf

5
推荐指数
1
解决办法
4900
查看次数