状态:401 在邮递员中未经授权

Posted

技术标签:

【中文标题】状态:401 在邮递员中未经授权【英文标题】:status:401 unauthorized in postman 【发布时间】:2021-03-19 19:08:45 【问题描述】:

我在springboot应用程序中工作并且我试图将数据保存在数据库中,代码正在正确执行并且在执行过程中没有出现任何错误但是当我试图在邮递员中发布url时我得到状态:401未授权

任何快速建议

控制台

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.4.RELEASE)
2020-12-08 17:34:07.094  INFO 7236 --- [           main] c.regestration.RegestrationApplication   : Starting RegestrationApplication on Darshan with PID 7236 (C:\Users\admin\Desktop\regestration\target\classes started by admin in C:\Users\admin\Desktop\regestration)
2020-12-08 17:34:07.111  INFO 7236 --- [           main] c.regestration.RegestrationApplication   : No active profile set, falling back to default profiles: default
2020-12-08 17:34:08.584  INFO 7236 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-12-08 17:34:08.803  INFO 7236 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 202ms. Found 1 JPA repository interfaces.
2020-12-08 17:34:10.751  INFO 7236 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8085 (http)
2020-12-08 17:34:10.788  INFO 7236 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-12-08 17:34:10.788  INFO 7236 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-12-08 17:34:11.158  INFO 7236 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-12-08 17:34:11.158  INFO 7236 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3827 ms
2020-12-08 17:34:11.617  INFO 7236 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-12-08 17:34:11.815  INFO 7236 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-12-08 17:34:11.935  WARN 7236 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-12-08 17:34:12.006  INFO 7236 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.21.Final
2020-12-08 17:34:13.075  INFO 7236 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations 5.1.0.Final
2020-12-08 17:34:13.554  INFO 7236 --- [         task-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-12-08 17:34:14.050  INFO 7236 --- [           main] .s.s.UserDetailsServiceAutoConfiguration : 
Using generated security password: 7406d0eb-72dc-4ce4-a8cc-220d3c523098
2020-12-08 17:34:14.273  INFO 7236 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@574cd322, org.springframework.security.web.context.SecurityContextPersistenceFilter@55e2fe3c, org.springframework.security.web.header.HeaderWriterFilter@64f1fd08, org.springframework.security.web.csrf.CsrfFilter@7187bac9, org.springframework.security.web.authentication.logout.LogoutFilter@5c8e67b9, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@1174a305, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@43e1692f, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@45c2e0a6, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@1f3165e7, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@12c60152, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@692e028d, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@119c745c, org.springframework.security.web.session.SessionManagementFilter@282ffbf5, org.springframework.security.web.access.ExceptionTranslationFilter@6c15e8c7, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@3a08078c]
2020-12-08 17:34:14.874  INFO 7236 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8085 (http) with context path ''
2020-12-08 17:34:14.884  INFO 7236 --- [           main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-12-08 17:34:14.889  INFO 7236 --- [         task-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-12-08 17:34:14.948  INFO 7236 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.mysql5InnoDBDialect
2020-12-08 17:34:18.480  INFO 7236 --- [         task-1] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-12-08 17:34:18.495  INFO 7236 --- [         task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-12-08 17:34:18.881  INFO 7236 --- [           main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-12-08 17:34:18.890  INFO 7236 --- [           main] c.regestration.RegestrationApplication   : Started RegestrationApplication in 13.562 seconds (JVM running for 19.799)
2020-12-08 17:34:42.030  INFO 7236 --- [nio-8085-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-12-08 17:34:42.031  INFO 7236 --- [nio-8085-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-12-08 17:34:42.050  INFO 7236 --- [nio-8085-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 19 ms

控制器

package com.regestration.controller;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.regestration.repository.Repository;
import com.regestration.user.User;

@RestController
public class Controller 

    @Autowired
    Repository repo;
    
    @RequestMapping(value="/saveReg", method= RequestMethod.POST)
    public HashMap<String, Object>saveRegestration(@RequestBody User user)
        HashMap<String, Object> map = new HashMap<String, Object>();
        repo.save(user);
        map.put("code", "200");
        map.put("code", "saved");
        return map;
    

application.properties

spring.datasource.url= jdbc:mysql://localhost:3306/myshadi?useSSL=false
spring.datasource.username= root
spring.datasource.password= root

spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto= update
server.port=8085

【问题讨论】:

【参考方案1】:

Spring 默认激活安全性。 请参阅行:使用生成的安全密码:7406d0eb-72dc-4ce4-a8cc-220d3c523098。

如果您不需要,您必须停用安全性:Spring boot Security Disable security

【讨论】:

以上是关于状态:401 在邮递员中未经授权的主要内容,如果未能解决你的问题,请参考以下文章

NodeJs:错误401(未经授权)我正在使用passport-jwt

为啥我的未经授权的控制器不返回 401 ASP.Net Core?

Keycloak PUT 请求返回 401(未经授权)

401 未经授权使用带有 Laravel Passport 的不记名令牌

不断收到 401 未经授权的“无效凭据”Laravel Passport

Phonegap错误:401未经授权 - 在Phonegap中使用基本身份验证的Rest API