Jhipster - JpaRepository "principal.username" @Query - org.springframework.expression.spel.SpelEvaluationException

Jhipster - JpaRepository "principal.username" @Query - org.springframework.expression.spel.SpelEvaluationException

我在用特定方法测试我的休息控制器时遇到错误。我正在使用 @Query 注释来执行我的数据库查询。它使用 "principal.username" 来做到这一点。我没有关于如何在我的应用程序中获取和使用 principal.username 的全部图片。我目前正在查看有关它的 spring-security 文档。但是我的问题是在测试部分,当我执行下面的测试时,由于@Query,我有一个错误"Faillure"。

存储库:

public interface MeetingRepository extends JpaRepository<Meeting,Long> {

  @Query("select m from Meeting m where m.visibility = 'PUBLIC' OR m.user.login = ?#{principal.username}")
  List<Meeting> findOpenAndUserMeetings();

}

休息控制器方法:

@RequestMapping(value = "/api/meetings", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Meeting>> getAll()
{
    List<Meeting> meetings = MeetingRepository.findOpenAndUserMeetings();
    return new ResponseEntity<List<Meeting>>(meetings, HttpStatus.OK);
}

一个测试:

@Test
@Transactional
public void getAllMeetings() throws Exception {
    // Initialize the database
    MeetingRepository.saveAndFlush(Meeting);

    // Get all the Meetinges
    restMeetingMockMvc.perform(get("/api/meetings"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON));
}

还有这个错误:

getAllMeetings(com.ent.web.rest.MeetingResourceTest)  Time elapsed: 0.07 sec  <<< ERROR!
    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 10): Property or field 'username' cannot be found on object of type 'java.lang.String' - maybe not public?
        at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:226)
        at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:93)
        at org.springframework.expression.spel.ast.PropertyOrFieldReference.access[=15=]0(PropertyOrFieldReference.java:46)
        at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:372)
        at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88)
        at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:131)
        at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:299)
        at org.springframework.data.jpa.repository.query.SpelExpressionStringQueryParameterBinder.evaluateExpression(SpelExpressionStringQueryParameterBinder.java:131)
        at org.springframework.data.jpa.repository.query.SpelExpressionStringQueryParameterBinder.potentiallyBindExpressionParameters(SpelExpressionStringQueryParameterBinder.java:89)
        at org.springframework.data.jpa.repository.query.SpelExpressionStringQueryParameterBinder.bind(SpelExpressionStringQueryParameterBinder.java:69)
        at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.doCreateCountQuery(AbstractStringBasedJpaQuery.java:109)
        at org.springframework.data.jpa.repository.query.AbstractJpaQuery.createCountQuery(AbstractJpaQuery.java:190)
        at org.springframework.data.jpa.repository.query.JpaQueryExecution$PagedExecution.doExecute(JpaQueryExecution.java:173)
        at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:74)
        at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:97)
        at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:88)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:395)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:373)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:99)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
        at com.sun.proxy.$Proxy148.findOpenAndUserMeetings(Unknown Source)
        at com.ent.web.rest.MeetingResource.getAll(MeetingResource.java:77)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
        at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
        at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
        at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
        at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:144)
        at com.ent.web.rest.MeetingResourceTest.getAllMeetings(MeetingResourceTest.java:151)

编辑

如何才能在测试中使用这个“?#{principal.username}”?经过调查,我发现这里:Spring Security 4.0: WebSocket, Spring Data and Test Support

Spring 数据整合 现在可以使用 SpEL 在 Spring 数据查询中访问当前用户。要使用 Java 配置启用此功能,您可以定义一个 @Bean.

@Bean
public SecurityEvaluationContextExtension securityEvaluationContextExtension(){
    return new SecurityEvaluationContextExtension();
}

那么您可以在查询中参考Spring 安全负责人。例如:

public interface BlogRepository extends JpaRepository<Blog,Long> {
    @Query("select blog from Blog blog where blog.user.login = ?#{principal.username}")
    List<Blog> findAllForCurrentUser();
}

是上下文问题吗?

在我的项目中,我有一个在其中一个查询中使用 #{principal.username} 的存储库。这是它的样子:

public interface BlogRepository extends JpaRepository<Blog, Long> {

    @Query("select blog from Blog blog where blog.user.login = ?#{principal.username}")
    List<Blog> findAllForCurrentUser();
}

我的 BlogResource 控制器调用如下:

 @Timed
 public List<Blog> getAll() {
     log.debug("REST request to get all Blogs for current user");
     return blogRepository.findAllForCurrentUser();
 }

为了测试这个,我升级到 Spring Security 4.0.1 并添加了对 spring-security-test:

的依赖
<spring-security.version>4.0.1.RELEASE</spring-security.version>

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-test</artifactId>
    <version>${spring-security.version}</version>
    <scope>test</scope>
</dependency>

BlogResourceTest中,我添加了对WebApplicationContextUserRepository的依赖:

@Autowired
WebApplicationContext context;

@Inject
UserRepository userRepository;

然后我修改了 getAllBlogs() 测试以使用 Spring 安全的 "with(user(username))" 功能。

 @Test
 @Transactional
 public void getAllBlogs() throws Exception {
    restBlogMockMvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();

     // Initialize the database
     blog.setUser(userRepository.findOneByLogin("user").get());
     blogRepository.saveAndFlush(blog);

     // Get all the blogs
    restBlogMockMvc.perform(get("/api/blogs").with(user("user")))
        .andExpect(status().isOk())
        .andExpect(content().contentType(MediaType.APPLICATION_JSON))
        .andExpect(jsonPath("$.[*].id").value(hasItem(blog.getId().intValue())))
        .andExpect(jsonPath("$.[*].name").value(hasItem(DEFAULT_NAME.toString())))
        .andExpect(jsonPath("$.[*].handle").value(hasItem(DEFAULT_HANDLE.toString())));
 }

我不知道为什么 Spring 安全的测试注释(@WithMockUser 和@WithUserDetails)不起作用。一周前我问了一个关于这个的问题:

Spring MVC Test with RequestPostProcessor vs. Annotations