Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.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 在servlet中实现HTTP基本身份验证_Java_Servlets_Basic Authentication - Fatal编程技术网

Java 在servlet中实现HTTP基本身份验证

Java 在servlet中实现HTTP基本身份验证,java,servlets,basic-authentication,Java,Servlets,Basic Authentication,我想编写一个servlet,它封装了一组资源,并需要使用基本的HTTP身份验证来保护它们;在提供文件之前,将根据后端数据库检查提交的用户名/密码 有人有这方面的工作实例吗?我在上尝试了该示例,但它在发送错误调用中不断返回非法状态异常。以下是一些返回凭证对象(保存登录名和密码的bean对象)的代码 它工作得很好,即使使用像:&=/?美元这样时髦的密码 下面是使用jMock对类进行的基本单元测试: public void testCredentialsWithBasicAuthentication(

我想编写一个servlet,它封装了一组资源,并需要使用基本的HTTP身份验证来保护它们;在提供文件之前,将根据后端数据库检查提交的用户名/密码


有人有这方面的工作实例吗?我在上尝试了该示例,但它在
发送错误
调用中不断返回
非法状态异常

以下是一些返回凭证对象(保存登录名和密码的bean对象)的代码

它工作得很好,即使使用像:&=/?美元这样时髦的密码

下面是使用jMock对类进行的基本单元测试:

public void testCredentialsWithBasicAuthentication() {
    // Setup
    final HttpServletRequest request = context.mock(HttpServletRequest.class);

    AuthentificationHelper helper = new AuthentificationHelper();
    String login = "mickael";
    String password = ":&=/?é$£";
    String base64Hash = Base64.encodeString(login + ":" + password);
    final String authHeader = "Basic " + base64Hash;

    // Expectations
    context.checking(new Expectations() {
        {
            oneOf (request).getHeader("Authorization");
            will(returnValue(authHeader));
        }   
    });

    // Execute
    Credentials credentials = helper.credentialsWithBasicAuthentication(request);

    // Verify
    assertNotNull(credentials);
    assertEquals(login, credentials.getLogin());
    assertEquals(password, credentials.getPassword());

    context.assertIsSatisfied();
}

我只是在链接中使用了示例。@Roy,你帖子中的示例对我来说很好。我不知道你为什么会犯错误。您能用stacktrace更新您的帖子吗?在我看来,servlet过滤器更适合于此,因为它独立于servlet,并且可以在必要时应用于多个servlet。这可能比你目前所需要的更先进,所以请把它当作友好的指针,而不是批评。对使用“代码< > StuttotoKeisher ”从官方java文档中使用的一点注释:“
StringTokenizer
是一个遗留类,出于兼容性原因而保留,尽管在新代码中不鼓励使用它。建议任何寻求此功能的人使用
String
split
方法或java.util.regex包。“Base64类从何而来?我已经有一段时间没有用java编码了,但Base64不是J2SE的一部分吗?”
public void testCredentialsWithBasicAuthentication() {
    // Setup
    final HttpServletRequest request = context.mock(HttpServletRequest.class);

    AuthentificationHelper helper = new AuthentificationHelper();
    String login = "mickael";
    String password = ":&=/?é$£";
    String base64Hash = Base64.encodeString(login + ":" + password);
    final String authHeader = "Basic " + base64Hash;

    // Expectations
    context.checking(new Expectations() {
        {
            oneOf (request).getHeader("Authorization");
            will(returnValue(authHeader));
        }   
    });

    // Execute
    Credentials credentials = helper.credentialsWithBasicAuthentication(request);

    // Verify
    assertNotNull(credentials);
    assertEquals(login, credentials.getLogin());
    assertEquals(password, credentials.getPassword());

    context.assertIsSatisfied();
}