2

如果有人能告诉我该身份验证有什么问题,我真的很感激?我从服务器上取消身份验证以在没有它的情况下进行测试,但 javascript 已损坏。

 $('#btnSignIn').click(function() {
            var username = $("#username").val();
            var password = $("#password").val();

    function make_base_auth(user, password) {
            var tok = user + ':' + password;

            var final = "Basic " + $.base64.encode(tok);
            console.log("FINAL---->" +final);
            alert("FINAL---->" +final);

            return final;
}

            $.ajax({
                        type: "GET",
                        contentType: "application/json",
                        url: "http://localhost:8080/SesameService/webresources/users/secured/login",
                        crossDomain: true,
                        dataType: "text",
                        async: false,
                        data: {},
                        beforeSend: function (xhr){ 
                            xhr.setRequestHeader('authorization', make_base_auth(username, password));
                       },
                        success: function() {
                            alert('Thanks for your signin in! ');

                        },
                        error: function(jqXHR, textStatus, errorThrown) {
                            console.log(textStatus, errorThrown);
                            alert(' Error in signIn-process!! ' +textStatus);
                        }
                    });
        });

错误:如果我采取

beforeSend: function (xhr){ 
    xhr.setRequestHeader('authorization', make_base_auth(username, password));

- 从功能中分离出来,我可以进入 REST 服务。我暂时没有进行身份验证。这可能是原因还是我应该在使用该标头时在服务器中进行身份验证?

contentType: "application/json",

@GET
@Path("/secured/login")
@Produces({"text/plain"})
public String login() {

    return "Is it working or not?";
}

在 JS 中使用 beforeSend-part 时,出现错误:

[Exception... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: http://localhost:8383/Sesame/assets/js/jquery.min.js :: .send :: line 3" data: no] { message="Failure"
, result=2147500037
, name="NS_ERROR_FAILURE"
, 

如果我理解正确,标题中的“授权+基本”告诉 Glassfish 服务器,基本身份验证将完成。身份验证后,它转到 REST 服务,在我的情况下,将 json-object 返回给 HTML5-client。HTML5-client 在 localhost:8383 中运行,其余服务在 localhost:8080 中运行。

如果我直接在 localhost:8080 中运行安全的休息服务,它就可以工作,所以这不是问题。问题是,当我使用或尝试使用来自不同域 localhost:8383 的 rest-services 时,我得到 JS-error console.log('----ERROR IN ------Siging IN-- --------'); 我不是 100% 确定,但我认为问题是 401 未授权,因此跨域身份验证不起作用。

我应该插入 crossDomain: true 吗?我在某个地方看到过,也许是这样?

在服务器端,我有过滤器:

<init-param>
        <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
        <param-value>util.CrossOriginResourceSharingFilter</param-value>
    </init-param>

public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
    response.getHttpHeaders().putSingle("Access-Control-Allow-Origin", "*");
    response.getHttpHeaders().putSingle("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
    response.getHttpHeaders().putSingle("Access-Control-Allow-Headers", "content-type");
    return response;
}

问题

1)如果服务器未配置身份验证并且我仍然使用授权标头会破坏应用程序并导致错误,这是否可能?

2) JS 错误是什么意思?

干杯,萨米

4

0 回答 0