标签: basic-authentication

使用Basic Auth时,HTTPClient会发出两个请求吗?

我一直在使用HTTPClient版本4.1.2来尝试访问需要基本身份验证的REST over HTTP API.这是客户端代码:

DefaultHttpClient httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
// Enable HTTP Basic Auth
httpClient.getCredentialsProvider().setCredentials(
    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), 
    new UsernamePasswordCredentials(this.username, this.password));

HttpHost proxy = new HttpHost(this.proxyURI.getHost(), this.proxyURI.getPort());

httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
Run Code Online (Sandbox Code Playgroud)

当我构造一个POST请求时,像这样:

HttpPost request = new HttpPost("http://my/url");
request.addHeader(new BasicHeader("Content-type", "application/atom+xml; type=entry")); // required by vendor
request.setEntity(new StringEntity("My content"));

HttpResponse response = client.execute(request);
Run Code Online (Sandbox Code Playgroud)

我在Charles Proxy中看到有两个请求被发送.一个没有Authorization: Basic ...头和一个它.正如你所料,第一个失败的是401,但是第二个失败的是201.

有谁知道为什么会这样?谢谢!

编辑:

我应该明确表示我已经看过这个问题,但正如你所看到的那样,我设置的AuthScope方式相同而且没有解决我的问题.此外,我HttpClient每次发出请求时都会创建一个新的(虽然我使用相同的ConnectionManager),但即使我HttpClient对多个请求使用相同的内容,问题仍然存在.

编辑2:

所以看起来@LastCoder的建议就是这样做的.看到另一个问题的答案.问题源于我对HTTP规范缺乏了解.我想要做的是"抢先认证", …

java basic-authentication apache-httpclient-4.x

13
推荐指数
1
解决办法
1万
查看次数

通过Spring安全性进行基本身份验证,无需登录表单

我正在尝试创建一个将由其他Web服务使用的restful Web服务.理想情况下,当客户端访问服务并且未经过身份验证时,他们应该获得401.我希望用户能够通过向请求添加身份验证标头来进行身份验证.我不希望用户填写登录表单,然后发布.我也不希望它在cookie中存储任何登录凭证(即保持状态)它应该在每个请求的auth头发送中.我使用spring roo来创建Web服务.

我目前所拥有的(取自一个spring security 3.1教程),当用户获得401时,他们会被提交一个登录页面,然后发布页面,获得他们随每个请求发送的cookie.

这是我的spring security xml.

 <http use-expressions="true">
   <intercept-url pattern="/customers/**" access="isAuthenticated()" />
 <intercept-url pattern="/**" access="denyAll" />
<form-login />
 </http>
<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="alice" password="other" authorities="user" />
            <user name="custome1" password="other" authorities="user" />
        </user-service>
    </authentication-provider>
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)

当我从curl发送请求时,我得到以下内容:

 $ curl -i -H "Accept: application/json" -H "Authorization: Basic Y3VzdG9tZXIxOm90aGVy" http://localhost:8080/Secured/customers

 HTTP/1.1 302 Found
 Server: Apache-Coyote/1.1
 Set-Cookie: JSESSIONID=B4F983464F68199FA0160DBE6279F440; Path=/Secured/; HttpOnly
 Location:      http://localhost:8080/Secured/spring_security_login;jsessionid=B4F983464F68199FA0160DBE6279F440
 Content-Length: 0
 Date: Thu, 25 Apr 2013 17:18:48 GMT
Run Code Online (Sandbox Code Playgroud)

我的基本标头令牌是base64(customer1:other)

如何让Web服务接受auth标头,而不是将我重定向到登录页面?

当我从security.xml中删除时,我得到以下内容:

excpetion:org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
    Configuration problem: No AuthenticationEntryPoint could be …
Run Code Online (Sandbox Code Playgroud)

spring spring-security basic-authentication

13
推荐指数
2
解决办法
2万
查看次数

将安全的Grafana嵌入到Web应用程序中

我想使用AngularJS将Grafana嵌入到我的Web应用程序中.目标是,当用户在我的应用程序中时,她应该能够单击按钮并加载Grafana UI.就其本身而言,这是一项简单的任务.为此,我有apache代理Grafana并返回任何必要的CORS头.apache反向代理配置如下:

    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
    Header always set Access-Control-Max-Age "1000"
    Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, Authorization, accept, client-security-token"


    RewriteEngine on
    RewriteCond %{REQUEST_METHOD} OPTIONS
    RewriteRule ^(.*)$ $1 [R=200,L]
    RewriteCond %{REQUEST_METHOD} OPTIONS
    ProxyPass / http://localhost:3000/
Run Code Online (Sandbox Code Playgroud)

当没有安全措施时,所有工作都完美无缺.以下代码是一个简单的HTML/Javascript,显示了我在做什么:

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="http://requirejs.org/docs/release/2.1.11/minified/require.js"></script>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<body >
<script>
    var app = angular.module('myApp', []);

    app.controller('MainCtrl', function ($scope, $http, $sce) {

    $scope.trustSrc = function(src) {
            console.log(src);
            return $sce.trustAsHtml(src);
    }

    $http({
            method : 'GET',
            url …
Run Code Online (Sandbox Code Playgroud)

javascript basic-authentication angularjs

13
推荐指数
1
解决办法
7214
查看次数

将多个WebSecurityConfigurerAdapter与不同的AuthenticationProviders一起使用(API的基本身份验证和Web应用程序的LDAP)

根据Spring Security Reference 5.7节,应该可以定义多个安全适配器.

我尝试做同样但没有成功.在服务器重新启动之后,API的前x次使用基本身份验证工作正常,但经过几次我被重定向到登录(表单)页面,这应该只针对我们的Web应用程序,而不是API调用.

我的代码:

@EnableWebSecurity
public class MultiHttpSecurityConfig  {

    @Configuration
    @Order(1)
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {

        @Autowired
        private Environment env;

        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().
                withUser("admin").password("pw_test").roles(API_ROLE);
        }

        protected void configure(HttpSecurity http) throws Exception {
            http
              .antMatcher("/services/**")
              .authorizeRequests()
              .anyRequest().hasRole(API_ROLE)
              .and()
              .httpBasic()
              .and()
              .csrf()
              .disable();
        }
    }

    @Configuration
    @Order(2)
    public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

        @Autowired
        private Environment env;

        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
            auth.eraseCredentials(false);
        }

        @Override …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security basic-authentication spring-boot

13
推荐指数
1
解决办法
1万
查看次数

支持或弃用 URL 中的 HTTP 基本身份验证

在一个项目中,我们花费了大量精力来解决基本身份验证(因为 webdriver 测试依赖于它,而 webdriver 没有用于基本身份验证的 api),我记得 URL 中的基本身份验证显然不起作用。即无法加载http://username:password@url

只需谷歌“网址中的基本身份验证”,您就会发现很多人在抱怨:https : //medium.com/@lmakarov/say-goodbye-to-urls-with-embedded-credentials-b051f6c7b6a3

https://www.ietf.org/rfc/rfc3986.txt

不推荐在 userinfo 字段中使用“user:password”格式。

今天我把这个泥潭告诉了一个朋友,他说他们在 webdriver 测试中使用http://username:password@url风格的基本身份验证没有任何问题。我将当前的 Chrome v71 转到了一个演示页面,令我惊讶的是,我发现它确实运行良好:https://guest:guest@jigsaw.w3.org/HTTP/Basic/

这怎么可能??我们是否同时生活在平行维度中?哪一个是正确的:使用 URL 中的凭据的基本身份验证是受支持还是已弃用?(或者这可能是由于我找不到任何参考的投诉而重新添加到 Chrome 中的吗?)

google-chrome http basic-authentication

13
推荐指数
1
解决办法
2382
查看次数

Http Basic Auth的登录表单

我正在运行名为bitlfu 的Perl应用程序.对于登录,它使用类似Apache HTTP Basic Auth但不是表单.我想使用用户名和密码字段为登录创建表单.我已经尝试过JavaScript和PHP,直到现在还没有结果.

所以我需要帮助!

PS:这种网址有效

http://user:password@host.com
Run Code Online (Sandbox Code Playgroud)

forms login http basic-authentication

12
推荐指数
1
解决办法
2万
查看次数

Apache Digest身份验证是否更安全或基本与否?

Authorization intro页面上,Apache告诉我们:

Apache支持另一种身份验证方法:AuthType Digest.此方法由mod_auth_digest实现,并且更加安全.

mod_auth_digest页面上,Apache告诉我们:

此模块实现HTTP摘要式身份验证(RFC2617),并提供mod_auth_basic的替代方法,其中密码不以明文形式传输.但是,与基本身份验证相比,这不会带来显着的安全优势.另一方面,与基本身份验证相比,使用摘要式身份验证的服务器上的密码存储安全性更低.

有人可以为我澄清这些看似矛盾的陈述吗?我知道处理密码的两种方式都容易受到重放攻击(除非你也使用SSL),但这似乎是一个单独的问题.

apache security authentication basic-authentication digest-authentication

12
推荐指数
1
解决办法
8018
查看次数

在Mocha和SuperTest中设置基本身份验证

我正在尝试为我们设置测试,以验证用户名和密码的基本身份验证阻止的路径的用户名和密码.

it('should receive a status code of 200 with login', function(done) {
    request(url)
        .get("/staging")
        .expect(200)
        .set('Authorization', 'Basic username:password')
        .end(function(err, res) {
            if (err) {
                throw err;
            }

            done();
        });
});
Run Code Online (Sandbox Code Playgroud)

javascript mocha.js basic-authentication node.js supertest

12
推荐指数
1
解决办法
9702
查看次数

如何使用浏览器发送授权标头

我已经实现了一个使用基本身份验证(使用 spring 安全性)的 Web 服务器。

我在访问 URL 时禁用了默认身份验证入口点(而不是使用 www-authentication 标头响应 401,它只返回 401),目的是防止浏览器显示身份验证弹出窗口。

我能够使用 javascript 代码和 curl 等命令行工具连接到服务器,但是当我使用浏览器(chrome 和 firefox)对其进行测试时,它们只是不发送标头。

curl -v -u user:password localhost:8080/user

GET /user HTTP/1.1
Host: localhost:8080
Authorization: Basic dXNlcjpwYXNzd29yZA==
User-Agent: curl/7.58.0
Accept: /

Chrome:版本 71.0.3578.98(官方版本)(64 位)
http://user:password@localhost:8080/user

GET /user HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ 71.0.3578.98 Safari/537.36
DNT: 1
Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng, / ;q=0.8
Accept-Encoding: gzip, deflate, br 接受语言:en-AU,en;q=0.9,fr-FR;q=0.8,fr;q=0.7,en-GB;q=0.6,en-US;q=0.5

为什么浏览器不发送身份验证标头。

browser webserver basic-authentication www-authenticate

12
推荐指数
1
解决办法
3万
查看次数

使用基本身份验证的 Google Apps 脚本中 UrlFetchApp.fetch 出现意外错误

我在 Google Apps Script 中有以下代码,它使用基本身份验证通过 HTTP 从网页中检索 CSV 数据并将其放入电子表格中:

CSVImport.gs

function parseCSVtoSheet(sheetName, url)
{
  // Credentials
  var username = "myusername";
  var password = "mypassword";
  var header = "Basic " + Utilities.base64Encode(username + ":" + password);
  
  // Setting the authorization header for basic HTTP authentication
  var options = {
    "headers": {
      "Authorization": header
    }
  };
  
  // Getting the ID of the sheet with the name passed as parameter
  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getSheetByName(sheetName);
  var sheetId = sheet.getSheetId();
  
  // …
Run Code Online (Sandbox Code Playgroud)

javascript basic-authentication urlfetch google-apps-script

12
推荐指数
1
解决办法
1484
查看次数