小编ali*_*ele的帖子

测试时的spring消息源

我在我的 java 配置中定义了消息源:

@Bean(name = "messageSource")
public MessageSource messageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasenames(
            "/i18n/ir/kia/industry/webapp/entity",
            "/i18n/ir/kia/industry/webapp/formErrors",
            "/i18n/ir/kia/industry/webapp/frontend",
            "/i18n/ir/kia/industry/webapp/frontendPages");
    return messageSource;
}
Run Code Online (Sandbox Code Playgroud)

当使用站点和消息正确显示时它工作正常,但是当尝试使用以下内容编写 spring 测试时:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestContext.class, SpringMVC.class})
@WebAppConfiguration
public abstract class AbstractTestClass {
  protected MockMvc mockMvc;
  @Autowired
  private WebApplicationContext webApplicationContext;

  @Before
  public void setUp() throws Exception {
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
  }
}
Run Code Online (Sandbox Code Playgroud)

和一个简单地扩展它的测试类,我得到错误Can't find bundle for base name /i18n/ir/kia/industry/webapp/entity

它在启动 tomcat 并在 jsp 文件中使用消息源时工作正常,但在测试时没有运气。我曾尝试在 WEB-INF 下移动 i18n 文件夹,但它也没有帮助。

目标文件夹看起来像这样,请不要告诉我将 i18n 文件夹添加到目标资源...

在此处输入图片说明

java spring unit-testing spring-mvc spring-test-mvc

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

在spring配置文件中设置资源

我想在spring配置中配置推土机.当使用xml配置时,它就像

<bean class="org.dozer.spring.DozerBeanMapperFactoryBean">
    <property name="mappingFiles" value="classpath*:dozer/**/*.dzr.xml"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

如何在配置文件中定义资源.我尝试使用ctx.getResource()但我无法访问Configuration类中的ApplicationContext.

我尝试了ContextRefreshedEvent并从那里添加资源,但仍然没有运气.(已调用afterPropertiesSet并且添加的映射不起作用)

public class ContextRefreshedEventBuilder extends ContextRefreshedEvent {
public ContextRefreshedEventBuilder(ApplicationContext ctx) {
    super(ctx);
    DozerBeanMapperFactoryBean mapper = ctx.getBean(DozerBeanMapperFactoryBean.class);
    try {
        mapper.setMappingFiles(ctx.getResources("classpath*:dozer/**/*.dzr.xml"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

还尝试使用ClassPathResource但无法找到正确的方法

DozerBeanMapperFactoryBean mapper = new DozerBeanMapperFactoryBean();
mapper.setMappingFiles(new Resource[]{new ClassPathResource("classpath*:dozer/**/*.dzr.xml")});
return mapper;
Run Code Online (Sandbox Code Playgroud)

如何将ClassPathResource添加为映射位置?

- -回答 - -

@Bean
public DozerBeanMapperFactoryBean configDozer() throws IOException {
    DozerBeanMapperFactoryBean mapper = new DozerBeanMapperFactoryBean();
    Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:dozer/**/*.dzr.xml");
    mapper.setMappingFiles(resources);
    return mapper;
}
Run Code Online (Sandbox Code Playgroud)

configuration resources spring classpath spring-annotations

4
推荐指数
1
解决办法
5981
查看次数

枚举的Spring数据方法名称

有什么办法可以将枚举值放在Spring Data的方法名称中?可以使用以下@Query注释来完成:

  @Query("select t from Ticket as t where t.status != desk.enums.TicketStatus.CLOSED")
  List<Ticket> findActiveTickets();
Run Code Online (Sandbox Code Playgroud)

status是枚举。但是,仅使用方法名称怎么办呢?使用类似的东西List<Ticket> findByStatusIsNotClosed();会导致No property isNotClosed found for type TicketStatus

那么,不用它怎么办@Query呢?

java spring spring-data

4
推荐指数
1
解决办法
1677
查看次数

scpting 安全 requireCsrfProtectionMatcher 与 csrfTokenRepository

我正在尝试禁用特定 url 的 Csrf。这是我到目前为止所做的:

public HttpSessionCsrfTokenRepository csrfTokenRepository() {
    final HttpSessionCsrfTokenRepository tokenRepository = new HttpSessionCsrfTokenRepository();
    tokenRepository.setHeaderName("X-XSRF-TOKEN");
    return tokenRepository;
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    RequestMatcher matcher = request -> !("//j_spring_cas_security_check".equals(request.getRequestURI()));
    http.csrf()
            .requireCsrfProtectionMatcher(matcher)
            .csrfTokenRepository(csrfTokenRepository());
Run Code Online (Sandbox Code Playgroud)

如果我requireCsrfProtectionMatcher在所有匹配器中注释掉或简单地返回 false 将不会有错误,但是使用此配置它会给我:

HTTP Status 403 - Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-XSRF-TOKEN'.
Run Code Online (Sandbox Code Playgroud)

我需要禁用 csrf,j_spring_cas_security_check以便单点注销工作并tokenRepository使用 angularjs。有什么我想念的吗?

cas csrf spring-security

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

spring boot MVC错误编码的POST请求

我无法使请求编码正常工作.为了使编码工作,我添加了过滤器以弹出安全性:

@Bean
public CharacterEncodingFilter characterEncodingFilter() {
    CharacterEncodingFilter filter = new CharacterEncodingFilter();
    filter.setEncoding("UTF-8");
    filter.setForceEncoding(true);
    return filter;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.addFilterBefore(characterEncodingFilter(), CsrfFilter.class);
...
}
Run Code Online (Sandbox Code Playgroud)

将meta添加到我的页面:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
...
Run Code Online (Sandbox Code Playgroud)

将UriEnconding添加到tomcat 8:

<Connector port="8080" protocol="HTTP/1.1" URIEncoding="UTF-8"
   connectionTimeout="20000"
   redirectPort="8443" />
Run Code Online (Sandbox Code Playgroud)

但它没有回应他们.当我发送请求并调试它时,结果是错误的.例如,当我???作为申请表的一部分发送时,我收到باÛ了结果.我缺少配置的任何部分吗?

当发送有效载荷ajax请求(使用rest客户端和ofcource到另一个方法)时,它正常工作,但表单数据没有运气.我的控制器看起来像这样:

@RequestMapping(value = "/test1")
@ResponseBody
public String test1(@RequestBody String req) {
    return req;
}

@RequestMapping(value = "/test2")
@ResponseBody
public String test2(@RequestParam("search") String req) {
    return req;
}
Run Code Online (Sandbox Code Playgroud)

不同的尝试:

  1. 对于test1,Content-Type=application/json …

spring tomcat spring-mvc character-encoding spring-boot

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