Spring Security中的401未经授权的错误

萨马尔:

我是Spring Boot和Spring Security的新手,并且出现此错误:

“错误401未经授权(celsecurity.jwt.AuthEntryPointJwt:未经授权的错误:访问此资源需要完全身份验证)

我尝试了此身份验证和注册教程(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,它可以按需工作。但是,当我将此原始教程添加到身份验证并注册应用程序以获得完整的应用程序时,我得到了错误:“未经授权的错误:访问此资源需要完全身份验证”。

我有一个问题,当我成功登录后,我想发出一个请求以获取用户列表。我发送的请求是一个安全的GET请求,该请求是我的Angular前端发送的http:// localhost:8084 / loginsystem / api / list / employees 但是现在我的问题是,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上运行应用程序时的结果:在此处输入图像描述

这是eclipse上完整应用程序的体系结构:

建筑

这是邮递员结果:邮递员结果

这是浏览器(前端)上的结果浏览器结果

邮递员头

邮递员中的授权标签

萨马尔:

问题已解决,我将此代码添加到webSecurityConfig类的configure()方法中

.antMatchers("/**").hasAnyRole("ADMIN","USER","MODERATOR").anyRequest().authenticated().and()
			
			.logout()
			.invalidateHttpSession(true)
			.clearAuthentication(true)
			.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
			.logoutSuccessUrl("/login?logout")
			.permitAll();
http.addFilterBefore( authenticationJwtTokenFilter(),  UsernamePasswordAuthenticationFilter.class);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当他们尝试在Spring Boot中访问WebApp时如何记录未经授权的用户(401)

如何在Spring Boot Webapp REST API控制器中以未经授权的用户身份发送401,如果数据库查询返回空对象,则发送404?

Spring Security针对未经授权的请求阻止CSS资源

为什么我在Maven中收到“ 401未经授权”错误?

Spring Boot Security不会引发401未经授权的异常,但找不到404

如何在Spring Boot上修复“错误401未经授权”

在Spring Security中处理未经授权的基本身份验证错误消息

Spring Boot测试中发生HTTP 401未经授权的错误

Spring Security抛出未经授权而不是重定向到登录

尽管提供了正确的凭据,Spring Security仍会返回401未经授权的代码

为什么Spring Security antmatcher总是未经授权返回?

Java Spring 401未经授权

Spring Security 401在不安全的端点上未经授权

迁移到Spring Boot 1.5.1和OAuth2 + JWT令牌-未经授权的错误401

Spring Security-401未经授权的访问

curl 401未经授权的错误

使用curl测试时,Grails Spring Security Rest未经授权(401)

Spring Security REST-单元测试因HttpStatusCode 401未经授权而失败

R中的401未经授权的错误Binance API

Spring Security的许可证全部未经授权

标题中的Spring RestTemplate凭证/授权在未经授权的情况下获得401,在邮递员那里工作正常

用于访问资源服务器的Spring OAuth2RestTemplate给出401未经授权

Spring Boot Admin Server无法访问端点(未经授权的401)

JWT令牌错误401在.net Core 3.1中未经授权

401在authenticationManager.authenticate()上未经授权(Spring Security)

在Grails应用中使用Spring Security Rest插件调用登录时出现401未经授权的错误

具有PreAuthenticationFilter的Spring Security AnonymousAuthFilter允许未经授权的请求

Java:Spring Boot、REST 的单元测试用例中的 401 未经授权的测试用例问题

Spring Security 中的授权问题