泽西岛:创建用于 wadl 处理的 JAXBContext 时出错



我有一个用球衣编码的 REST API,需要身份验证。要进行身份验证,您需要发布到此 url :http://localhost:8080/login 正文中的凭据。

从 paw 等 API 工具发布时它工作正常,但我无法让它与 Angular 5 一起使用,我总是在浏览器控制台中收到此错误:

Response to preflight request doesn't pass access control check: No 
Access-Control-Allow-Origin' header is present on the requested resource

而这个 服务器控制台 :

WARNING [http-nio-8080-exec-11] 
org.glassfish.jersey.internal.Errors.logErrors The following warnings 
have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 2
javax.ws.rs.ProcessingException: Error creating a JAXBContext for 
wadl processing.

完整的堆栈跟踪

这是我的配置

资源:

@POST
@Path("login")
public Response getSession (Login login, @Context HttpServletRequest req) {
    System.out.println(login.getUsername());
    System.out.println(login.getPassword());
    LDAP ldap = new LDAP();
    String role = "none";
    try {
        if (ldap.auth(login.getUsername(), login.getPassword())) {
            role = ldap.getRole(login.getUsername());
        }
    } catch (Exception e) {
        log.error(e);
        throw new NotAuthorizedException(e.getMessage(), Response.Status.UNAUTHORIZED);
    }

    HttpSession session = req.getSession(true);
    session.setAttribute("user", login.getUsername());
    session.setAttribute("role", role);
    return Response.status(Response.Status.OK)
            .entity(session.getId())
            .build();
}

Pom.xml :

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.26</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-bean-validation</artifactId>
        <version>2.26</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.10.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.10.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
</dependencies>
<properties>
    <jersey.version>2.26</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

角度代码 :

const httpOptions = {
    headers: new HttpHeaders({
        'Content-Type':  'application/json'
    })
};
login(username, password) {
    const body = JSON.stringify({'username' : username, 'password' : password});
    return this.http.post(baseURI, body, httpOptions);
}

我正在使用Java SE 9.0.1和angular 1.7.3

这指出了 CORS 的问题。

Access-Control-Allow-Origin' header is present on the requested resource

出于安全原因,服务器仅接受具有相同来源的请求。您可以尝试为服务器响应设置Access-Control-Allow-Origin: *

查看这篇文章 https://www.html5rocks.com/en/tutorials/cors/

希望这对你有帮助

相关内容

  • 没有找到相关文章

最新更新