【发布时间】:2020-05-08 05:41:56
【问题描述】:
我是 Spring Boot 和 Spring Security 的新手,我遇到了这个错误:
" 错误 401 Unauthorized (c.e.l.security.jwt.AuthEntryPointJwt : Unauthorized error: 访问此资源需要完全身份验证)"
我尝试了这个身份验证和注册教程 (https://bezkoder.com/spring-boot-jwt-authentication/?unapproved=2080&moderation-hash=102a62e22b4c04ad25fce7fd2c3617a3#comment-2080) 和一个管理用户界面 (https://www.javaguides.net/2020/01/spring-boot-angular-9-crud-example-tutorial.html) 它可以按预期工作。但是当我将这个 crud 教程添加到身份验证和注册应用程序以获得完整的应用程序我收到此错误:“未经授权的错误:访问此资源需要完全身份验证”。
我有一个问题,当我成功登录时,我想发出请求以获取用户列表。我发送的请求是一个安全的 GET 请求,它是 http://localhost:8084/loginsystem/api/list/employees 这个请求是由我的 Angular 前端发送的。 但我现在的问题是,Spring Boot 告诉我用户未获得授权并发送 401 错误。当我从前端向后端发出请求时,我正在发送一个授权令牌。
任何帮助,我无法解决这个问题我尝试了所有解决方案并没有解决问题。
这是 POM.XML 文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>loginsystem</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>loginsystem</name>
<description>loginsystem projet mvc</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-engine</artifactId>
<version>5.6.1.Final</version>
<exclusions>
<exclusion>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-search-orm -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.6.1.Final</version>
<exclusions>
<exclusion>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这是我在 Eclipse 上运行应用程序时的结果: enter image description here
这是eclipse上完整应用的架构:
这是邮递员的结果: Postman result
这是浏览器(前端)的结果: Browser result
【问题讨论】:
-
在 WebSecurityConfig 文件配置方法中,提到了对“/api/auth/**”和“/api/test/**”以外的任何请求进行身份验证。您正在尝试获取必须经过身份验证的“api/list/employees”。因此,我认为您需要在此请求的标头中传递 JWT 令牌。首先生成一个 JWT 令牌,然后将此请求作为 Bearer 令牌传入。
-
我是 Spring Security 的新手,我真的不知道如何生成 JWT 令牌并将其作为不记名令牌传递到请求中。你能给我举个例子吗,非常感谢为你提供帮助。 @TusharJajodia
-
您正在学习的教程具有用于生成 JWT 令牌的端点。实施并尝试使用该端点来生成令牌。一旦您将令牌作为授权在请求 HEADER 中传递:承载“令牌值”或查看如何在邮递员中传递承载令牌。 (您也可以在邮递员中使用授权选项卡)
-
我这样做了,但浏览器仍然向我显示此错误:CORS 策略已阻止从源 'localhost:8084/loginsystem/api/list/employees' 访问 XMLHttpRequest:响应预检请求没有通过访问控制检查:它没有 HTTP ok 状态。 @TusharJajodia
-
请您尝试一下邮递员的请求。
标签: java spring spring-boot