Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JUnit测试Spring控制器,这取决于Spring安全性_Java_Spring_Spring Mvc_Junit_Spring Security - Fatal编程技术网

Java 使用JUnit测试Spring控制器,这取决于Spring安全性

Java 使用JUnit测试Spring控制器,这取决于Spring安全性,java,spring,spring-mvc,junit,spring-security,Java,Spring,Spring Mvc,Junit,Spring Security,我有一个Spring应用程序,我正在构建JUnit测试来测试某个控制器 问题是,在控制器中我将此代码称为: final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); final String userName = authentication.getName(); private MockMvc mockMvc; @Test public void

我有一个Spring应用程序,我正在构建
JUnit
测试来测试某个
控制器

问题是,在
控制器中
我将此代码称为:

final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
final String userName = authentication.getName();
private MockMvc mockMvc;

    @Test
    public void getPageTest() throws Exception{
        final ProcessFileController controller = new ProcessFileController();
        mockMvc = standaloneSetup(controller).build();

    mockMvc.perform(get(URI.create("/processFile.html")).sessionAttr("freeTrialEmailAddress", "")).andExpect(view().name("processFile"));
        }
换句话说,我需要在调用此
控制器之前进行身份验证。我用以下代码编写了一个JUnit测试:

final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
final String userName = authentication.getName();
private MockMvc mockMvc;

    @Test
    public void getPageTest() throws Exception{
        final ProcessFileController controller = new ProcessFileController();
        mockMvc = standaloneSetup(controller).build();

    mockMvc.perform(get(URI.create("/processFile.html")).sessionAttr("freeTrialEmailAddress", "")).andExpect(view().name("processFile"));
        }
当我运行它时,它在
最终字符串userName=authentication.getName()的右边给我一个
NullPointerException
因为我的
身份验证
空的
,因为我没有登录

问题是:有没有办法模拟身份验证?欢迎提出任何意见


谢谢。

理想情况下,您会使用
@AuthenticationPrincipal
,但如果这不是一个选项,您需要使用
验证
实例设置
SecurityContext
,该实例将在测试中可用

您可以在助手类中使用一个静态方法来执行此操作

public static void setupSecurityContext(String username, String password, String... groups)
{
  List<GrantedAuthority> authorities = new ArrayList<>();
  for (String group : groups)
  {
    authorities.add(new SimpleGrantedAuthority(group));
  }

  UserDetails user = new UserDetails(username, password, authorities);
  UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, password);
  SecurityContextHolder.getContext().setAuthentication(token);
}
public static void setupSecurityContext(字符串用户名、字符串密码、字符串…组)
{
列表权限=新建ArrayList();
用于(字符串组:组)
{
添加(新的SimpleGrantedAuthority(集团));
}
UserDetails user=新的UserDetails(用户名、密码、权限);
UsernamePasswordAuthenticationToken=新的UsernamePasswordAuthenticationToken(用户,密码);
SecurityContextHolder.getContext().setAuthentication(令牌);
}
然后在测试中,您只需调用


SecurityHelper.setupSecurityContext(“用户”、“密码”、“g1”、“g2”)

SpringSecurityVersion4在这方面引入了一些简洁的改进

首先,确保类路径中有测试框架用于测试,使用Maven,它看起来像:


org.springframework.security
弹簧安全性试验
4.0.4.1发布
测试
有用的导入:

import static org.springframework.security.test.web.servlet.setup.SecurityMockMVCConfiguers.*;
导入静态org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
测试设置:

mockMvc=webAppContextSetup(applicationContext).apply(springSecurity()).build();
(我认为您需要使用WebApplicationContext,而不是单个控制器。)

然后进行如下测试:

mockMvc.perform(get(…).with(user(“username”).roles(“user”)).andExpect(…);

你为什么要手动操作而不是使用
@AuthenticationPrincipal
?chrylis,该体系结构不是为使用
@AuthenticationPrincipal
而设计的,我需要一个想法,一种处理当前体系结构的方法。谢谢您的回答。在测试后重置
SecurityContextHolder.getContext().setAuthentication(令牌)
以使测试独立,这将是非常好的。由于此SecurityContextHolder是一个单例,因此您在所有单元测试中共享相同的信息。@Dherik您可以在\@After方法中设置为null。哦,是的,我只是警告这一点,因为人们在测试中使用单例是一个常见问题,在测试过程中可能会导致奇怪的问题