我将bugzilla从3.4.6升级到5.0.3。 bugzilla 5.0.3升级后运行正常。我正在尝试使用bugzilla的REST API在java应用程序中使用bugzilla创建bug。我可以使用Sping的RestTemplate使用api_key进行身份验证,成功地从java中搜索bug。但是错误创建不起作用我得到401(授权错误),即使我在请求的标头中传递了正确的授权。 Java代码如下。我还添加了bugzilla和.htaccess文件中的错误日志。我在头文件中尝试了几乎可能的授权组合 - 比如传递Bugzilla_api_key或api_key或Bugzilla_api_token或者所有这些,没有任何作用总是得到401.凭据是有效的,因为我可以使用Advanced Rest Client成功创建错误。
public void createBug(JSONObject json) {
String url = bugzillaRestUrl + "bug";
Class<String> responseType = String.class;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Bugzilla_api_key",TOKEN);
headers.add("Bugzilla_api_token",this.login());
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.add("Authorization", "Basic " + getBase64Credentials());
HttpEntity<String> request = new HttpEntity<String>(json.toString(), headers);
RestTemplate restTemplate = new RestTemplate();
try {
ResponseEntity<String> result = restTemplate.postForEntity(url, request, responseType);
System.out.println(result.getBody());
} catch(Exception e) {
e.printStackTrace();
} finally {
}
}
private String getBase64Credentials(){
String plainCreds = "<user>" + ":" + "<password>";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = org.apache.commons.codec.binary.Base64.encodeBase64(plainCredsBytes);
return new String(base64CredsBytes);
}
[19239] 01/24/18 12:22:20 global/user-error.html.tmpl login_required 192.168.1.48
[19239] $param(POSTDATA) = "{\"summary\":\"New Test bug - please disregard 2\",\"product\":\"ASIPay\",\"component\":\"Account Search\",\"alias\":\"SomAlias2\",\"priority\":\"P1\",\"version\":\"unspecified\",\"op_sys\":\"All\",\"rep_platform\":\"All\"}";
[19239] $env(BZ_CACHE_CONTROL) = 1;
[19239] $env(CONTENT_LENGTH) = 191;
[19239] $env(CONTENT_TYPE) = "application/json";
[19239] $env(DOCUMENT_ROOT) = "/var/www/bugzilla";
[19239] $env(GATEWAY_INTERFACE) = "CGI/1.1";
[19239] $env(HTTP_ACCEPT) = "application/json";
[19239] $env(HTTP_ACCEPT_CHARSET) = "big5, big5-hkscs, cesu-8, euc-jp, euc-kr, gb18030, gb2312, gbk, ibm-thai, ibm00858, ibm01140, ibm01141, ibm01142, ibm01143, ibm01144, ibm01145, ibm01146, ibm01147, ibm01148, ibm01149, ibm037, ibm1026, ibm1047, ibm273, ibm277, ibm278, ibm280, ibm284, ibm285, ibm290, ibm297, ibm420, ibm424, ibm437, ibm500, ibm775, ibm850, ibm852, ibm855, ibm857, ibm860, ibm861, ibm862, ibm863, ibm864, ibm865, ibm866, ibm868, ibm869, ibm870, ibm871, ibm918, iso-2022-cn, iso-2022-jp, iso-2022-jp-2, iso-2022-kr, iso-8859-1, iso-8859-13, iso-8859-15, iso-8859-2, iso-8859-3, iso-8859-4, iso-8859-5, iso-8859-6, iso-8859-7, iso-8859-8, iso-8859-9, jis_x0201, jis_x0212-1990, koi8-r, koi8-u, shift_jis, tis-620, us-ascii, utf-16, utf-16be, utf-16le, utf-32, utf-32be, utf-32le, utf-8, windows-1250, windows-1251, windows-1252, windows-1253, windows-1254, windows-1255, windows-1256, windows-1257, windows-1258, windows-31j, x-big5-hkscs-2001, x-big5-solaris, x-euc-jp-linux, x-euc-tw, x-eucjp-open, x-ibm1006, x-ibm1025, x-ibm1046, x-ibm1097, x-ibm1098, x-ibm1112, x-ibm1122, x-ibm1123, x-ibm1124, x-ibm1166, x-ibm1364, x-ibm1381, x-ibm1383, x-ibm300, x-ibm33722, x-ibm737, x-ibm833, x-ibm834, x-ibm856, x-ibm874, x-ibm875, x-ibm921, x-ibm922, x-ibm930, x-ibm933, x-ibm935, x-ibm937, x-ibm939, x-ibm942, x-ibm942c, x-ibm943, x-ibm943c, x-ibm948, x-ibm949, x-ibm949c, x-ibm950, x-ibm964, x-ibm970, x-iscii91, x-iso-2022-cn-cns, x-iso-2022-cn-gb, x-iso-8859-11, x-jis0208, x-jisautodetect, x-johab, x-macarabic, x-maccentraleurope, x-maccroatian, x-maccyrillic, x-macdingbat, x-macgreek, x-machebrew, x-maciceland, x-macroman, x-macromania, x-macsymbol, x-macthai, x-macturkish, x-macukraine, x-ms932_0213, x-ms950-hkscs, x-ms950-hkscs-xp, x-mswin-936, x-pck, x-sjis_0213, x-utf-16le-bom, x-utf-32be-bom, x-utf-32le-bom, x-windows-50220, x-windows-50221, x-windows-874, x-windows-949, x-windows-950, x-windows-iso2022jp";
[19239] $env(HTTP_AUTHORIZATION) = "Basic dWNob3dkaHVyeUBpbm5vdmVzdHN5c3RlbXMuY29tOlRlc3R4ITIz";
[19239] $env(HTTP_BUGZILLA_API_KEY) = "srZeBYyELYP4ddVwpnmvhgLS2NzdjD4d5elYzB8h";
[19239] $env(HTTP_BUGZILLA_API_TOKEN) = "60-yxl1VnhvBz";
[19239] $env(HTTP_CONNECTION) = "keep-alive";
[19239] $env(HTTP_HOST) = "192.168.1.103";
[19239] $env(HTTP_USER_AGENT) = "Java/1.8.0_60";
[19239] $env(PATH) = "";
[19239] $env(PATH_INFO) = "/bug";
[19239] $env(PATH_TRANSLATED) = "/var/www/bugzilla/bug";
[19239] $env(QUERY_STRING) = "";
[19239] $env(REDIRECT_BZ_CACHE_CONTROL) = 1;
[19239] $env(REDIRECT_HTTP_AUTHORIZATION) = "Basic dWNob3dkaHVyeUBpbm5vdmVzdHN5c3RlbXMuY29tOlRlc3R4ITIz";
[19239] $env(REDIRECT_SCRIPT_URI) = "http://192.168.1.103/bugzilla/rest/bug";
[19239] $env(REDIRECT_SCRIPT_URL) = "/bugzilla/rest/bug";
[19239] $env(REDIRECT_STATUS) = 200;
[19239] $env(REDIRECT_URL) = "/bugzilla/rest/bug";
[19239] $env(REMOTE_ADDR) = "192.168.1.48";
[19239] $env(REMOTE_PORT) = 50834;
[19239] $env(REQUEST_METHOD) = "POST";
[19239] $env(REQUEST_URI) = "/bugzilla/rest/bug";
[19239] $env(SCRIPT_FILENAME) = "/var/www/bugzilla/rest.cgi";
[19239] $env(SCRIPT_NAME) = "/rest.cgi";
[19239] $env(SCRIPT_URI) = "http://192.168.1.103/bugzilla/rest/bug";
[19239] $env(SCRIPT_URL) = "/bugzilla/rest/bug";
[19239] $env(SERVER_ADDR) = "192.168.1.103";
[19239] $env(SERVER_ADMIN) = "root\@localhost";
[19239] $env(SERVER_NAME) = "192.168.1.103";
[19239] $env(SERVER_PORT) = 80;
[19239] $env(SERVER_PROTOCOL) = "HTTP/1.1";
[19239] $env(SERVER_SIGNATURE) = "<address>Apache/2.2.3 (Red Hat) Server at 192.168.1.103 Port 80</address>\n";
[19239] $env(SERVER_SOFTWARE) = "Apache/2.2.3 (Red Hat)";
[19239] $env(TEST2_ACTIVE) = 1;
[19239] $env(TEST_ACTIVE) = 1;
.htaccess文件
# Don't allow people to retrieve non-cgi executable files or our private data
<FilesMatch (\.pm|\.pl|\.tmpl|localconfig.*)$>
<IfModule mod_version.c>
<IfVersion < 2.4>
Order allow,deny
Deny from all
</IfVersion>
<IfVersion >= 2.4>
Require all denied
</IfVersion>
</IfModule>
<IfModule !mod_version.c>
Deny from all
</IfModule>
</FilesMatch>
Options -Indexes
<IfModule mod_expires.c>
<IfModule mod_headers.c>
<IfModule mod_env.c>
<FilesMatch (\.js|\.css)$>
ExpiresActive On
# According to RFC 2616, "1 year in the future" means "never expire".
# We change the name of the file's URL whenever its modification date
# changes, so browsers can cache any individual JS or CSS URL forever.
# However, since all JS and CSS URLs involve a ? in them (for the changing
# name) we have to explicitly set an Expires header or browsers won't
# *ever* cache them.
ExpiresDefault "now plus 1 years"
Header append Cache-Control "public"
</FilesMatch>
# This lets Bugzilla know that we are properly sending Cache-Control
# and Expires headers for CSS and JS files.
SetEnv BZ_CACHE_CONTROL 1
</IfModule>
</IfModule>
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteOptions inherit
RewriteRule ^rest/(.*)$ rest.cgi/$1 [NE]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
bugzilla.vhost文件
Alias /bugzilla /var/www/bugzilla
<Directory /var/www/bugzilla>
AddHandler cgi-script .cgi
Options +Indexes +ExecCGI +FollowSymLinks
DirectoryIndex index.cgi
AllowOverride All
# Order allow,deny
# Allow from all
</Directory>
<VirtualHost *:80>
DocumentRoot /var/www/bugzilla
ServerName 192.168.1.103:80
ErrorLog logs/bugzilla-error_log
CustomLog logs/bugzilla-access_log common
AddHandler cgi-script .cgi
Options +Indexes +ExecCGI
DirectoryIndex index.cgi
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^TRACE
RewriteRule .* - [F]
</VirtualHost>
答案 0 :(得分:0)
发现了这个问题。即使对于POST请求(比如在Bugzilla中创建一个bug),我们也需要将api_key作为查询参数传递,就像我们对GET请求一样。
public void createBug(JSONObject json) {
String url = bugzillaRestUrl + "bug?api_key=" + API_KEY;
Class<String> responseType = String.class;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> request = new HttpEntity<String>(json.toString(), headers);
RestTemplate restTemplate = new RestTemplate();
try {
ResponseEntity<String> result = restTemplate.postForEntity(url, request, responseType);
System.out.println(result.getBody());
} catch(Exception e) {
e.printStackTrace();
} finally {
}
}
可以从Bugzilla中的Preferences-&gt; API Keys页面生成API_KEY