Java 在spring安全应用程序中测试身份验证的最简单方法

Java 在spring安全应用程序中测试身份验证的最简单方法,java,spring,junit,spring-security,Java,Spring,Junit,Spring Security,我有一个简单的方法来验证web应用程序上的用户 public Authentication authenticate(Authentication authentication) throws AuthenticationException { String name = authentication.getName(); String password = authentication.getCredentials().toString(); if (name.cont

我有一个简单的方法来验证web应用程序上的用户

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String name = authentication.getName();
    String password = authentication.getCredentials().toString();
    if (name.contains("@mydomain.com")) {
        List<GrantedAuthority> grantedAuths = new ArrayList<>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
        Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
        return auth;
    } else {
        throw new BadCredentialsException("Invalid username/password");
    }
}
公共身份验证(身份验证)引发AuthenticationException{
String name=authentication.getName();
字符串密码=authentication.getCredentials().toString();
if(name.contains(@mydomain.com))){
List grantedAuths=new ArrayList();
grantedAuths.add(新的SimpleGrantedAuthority(“角色用户”);
Authentication auth=新用户名PasswordAuthenticationToken(名称、密码、授权验证);
返回auth;
}否则{
抛出新的BadCredentialsException(“无效用户名/密码”);
}
}

我想知道是否有一种方法可以在不使用诸如MockMvc()之类的编译工具的情况下对其进行测试(最好是在JUnit中进行测试)

您能准确地解释一下要测试什么吗?你的验收测试是什么?你想测试你的算法还是集成中的spring安全行为?我想测试我的算法HM。基本上,我想确保任何包含“*@mydomain.com”的用户名都允许我进行身份验证。您可以模拟或存根身份验证对象。在第一个测试中,您将使用认证成功的预期数据进行测试。在第二个测试中,您将使用身份验证失败的预期数据进行测试。在单元测试中,您将使用预定义的行为传输模拟/存根身份验证。请看莫基托的照片example@davidhxxx但是如何将它与我的自定义身份验证功能结合使用?您能准确地解释一下您想要测试什么吗?你的验收测试是什么?你想测试你的算法还是集成中的spring安全行为?我想测试我的算法HM。基本上,我想确保任何包含“*@mydomain.com”的用户名都允许我进行身份验证。您可以模拟或存根身份验证对象。在第一个测试中,您将使用认证成功的预期数据进行测试。在第二个测试中,您将使用身份验证失败的预期数据进行测试。在单元测试中,您将使用预定义的行为传输模拟/存根身份验证。请看莫基托的照片example@davidhxxx但是如何将它与我的自定义身份验证功能一起使用呢