简体   繁体   English

如何访问HTTP Basic用户名?

[英]How can I access the HTTP Basic username?

I'm using this ContainerRequestFilter to check HTTP Basic credentials. 我正在使用此ContainerRequestFilter来检查HTTP Basic凭据。

private class Filter implements ResourceFilter, ContainerRequestFilter {

    @Override
    public ContainerRequest filter(ContainerRequest request) {
        String auth = request.getHeaderValue("Authorization");

        if (auth == null || !auth.startsWith("Basic ")) {
            throw new NotAuthorizedException("FAILED\n");
        }

        auth = Base64.base64Decode(auth.substring("Basic ".length()));
        String[] vals = auth.split(":");
        String username = vals[0];
        String password = vals[1];

        boolean validUser = database.Users.validate(username, password);

        if (!validUser) {
            throw new NotAuthorizedException("FAILED\n");
        }

        return request;
    }

    ...

}

So by the time I get to this point, I've authenticated the user. 所以当我到达这一点时,我已经对用户进行了身份验证。 Now how I can get the username? 现在我如何获得用户名?

@GET
@Path("some_kind_of_report_or_something")
@Produces(MediaType.TEXT_PLAIN)
public String fetchAReportOrSomething() {
    // At this point, I know that the user has provided good credentials,
    // now I need get the user's username as a String
    String username = ???;
}

I suppose I could use HttpContext.getRequest() and do the same thing as in the AuthFilter (I'd move that username/password extraction logic to its own method). 我想我可以使用HttpContext.getRequest()并执行与AuthFilter相同的操作(我将用户名/密码提取逻辑移动到它自己的方法)。 In the filter, can I somehow store the extracted username somewhere in the request object so it gets passed on to this handler? 在过滤器中,我可以以某种方式将提取的用户名存储在请求对象中的某处,以便将其传递给此处理程序吗?

(By the way, is there a better way to extract the username and password than what I've done in the filter? If so, let me know in a comment.) (顺便说一下,有没有比我在过滤器中更好的方法来提取用户名和密码?如果有,请在评论中告诉我。)

Take a look how it's done in a working application: www.s3auth.com . 看看它是如何在一个有效的应用程序中完成的: www.s3auth.com The source code is available at github . 源代码可以在github上找到 As you can see on the site, facebook and google authentication mechanisms are used. 正如您在网站上看到的,使用了Facebook和Google身份验证机制。 The application is using JAX-RS/Jersey. 该应用程序使用JAX-RS / Jersey。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用HTTP Basic时如何检查登录时用户名和密码是否正确? - How to check if username and password is correct on login when using HTTP Basic? 如何在Android中发送HTTP基本身份验证标头? - How can I send HTTP Basic Authentication headers in Android? 如何存储MySQL数据库密码和用户名来访问数据库? - How can i store MySQL database password and username to access the database? 使用Spring,Basic Auth和LDAP,如何获取提供的用户名? - Using Spring, Basic Auth and LDAP, how can I get the supplied username? 如何访问Cloudbees中的http访问日志 - How can I access the http access logs in Cloudbees 如何使用我未使用的特定用户名和密码向用户授予对我的数据库的访问权限? - How can I grant access of my database to a user with a certain username and password which I am not using? 如何访问用户的登录用户名并将其存储在要在常规 java class 中使用的变量中 - How can I access the user's login username and store it in a variable to be used in a regular java class 如何使用用户名和密码来构造URL以使用基本身份验证Java访问Rest API? - How to frame URL with username and password to access Rest API using basic authentication Java? 如何从 JWT 获取用户名? - How can I get the username from the JWT? 如何使用Struts 2 / Spring 3进行HTTP基本身份验证? - How do I do HTTP basic auth using Struts 2/ Spring 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM