gpt4 book ai didi

java - 无法在 Spring-boot 中使用自签名 https 验证来自 Postman 的 POST/GET 请求方法

转载 作者:行者123 更新时间:2023-12-02 02:48:55 24 4
gpt4 key购买 nike

我正在我的 Spring Boot 应用程序中创建一个休息端点,但无法获取我的:

@RequestMapping(method = RequestMethod.POST) 

或者:

@RequestMapping(value = "/test", method = RequestMethod.GET)

逃离 postman 。这是安全配置文件:

@Override
public void configure(HttpSecurity httpSecurity) throws Exception{
System.out.println("configure method entered");
httpSecurity.requiresChannel()
.antMatchers("/test").requiresSecure()
.and()
.authorizeRequests()
.anyRequest()
.authenticated();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
System.out.println("global configurer entered");
auth.inMemoryAuthentication()
.withUser(******).password(*****).roles("USER")
.and()
.withUser(******).password(*****).roles("USER", "ADMIN");
}

没有我们实现的自签名证书,也没有授权,postman中的POST方法一直工作正常。我不明白授权如何阻止我的 POST 方法运行。我已经在 postman 的授权下在基本身份验证中输入了正确的用户凭据。当前 header 输出:

Cache-Control →no-cache, no-store, max-age=0, must-revalidate
Content-Length →0
Date →Tue, 23 May 2017 10:40:31 GMT
Expires →0
Pragma →no-cache
Strict-Transport-Security →max-age=31536000 ; includeSubDomains
X-Content-Type-Options →nosniff
X-Frame-Options →DENY
X-XSS-Protection →1; mode=block

我的配置方法一定有问题,因为如果我提供不正确的密码,它不应该返回 404,但它仍然如此。执行应用程序时,我在控制台中得到以下输出:

2017-05-23 14:05:19.460  INFO 6984 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test],methods=[GET]}" onto public java.lang.String executor.rr.test.text()

这仅用于测试目的,以检查其余端点是否响应。这是代码:

@RestController
public class test {
@RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public String text(){
return "test test";
}
}

我还更新了我的配置方法以使用 https,并且我正在尝试使用以下 url:

https://localhost:5434/test

从 postman 运行 POST 方法,会给出以下 IDE 控制台输出:

2017-05-23 20:01:48.289  INFO 18843 --- [nio-5434-exec-7] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-05-23 20:01:48.289 INFO 18843 --- [nio-5434-exec-7] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-05-23 20:01:48.307 INFO 18843 --- [nio-5434-exec-7] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 18 ms

关注此post我添加了:

@ComponentScan(basePackages={"package name of where the controller is"})

这将我的安全性恢复为默认值,并且控制台打印了一次性密码。当我写了错误的密码时,在 postman 中尝试此操作返回 401(未经授权),但当凭据正确时仍然返回 404。这个post有类似的问题,但他们都没有帮助。

这是我的应用程序类和其余类的项目结构:

enter image description here

--------------------------------更新------------------------- ----------------------------

所以我将问题范围缩小到身份验证。在 securityConfig 文件中的某个位置,全局或其他配置模式以某种方式给出 404 状态。

inMemoryAuthentication 与 postman 中的基本身份验证不同吗?或者 httpSecurity 方法是否错误?

当我删除整个 securityConfig 类时,spring 会生成一个默认密码,该密码工作正常,并且我从运行 POST 中获得所需的输出。

最佳答案

尝试以下操作:

  1. 转到 postman 中的“授权”选项卡。

  2. 选择基本身份验证

  3. 输入用户名和密码。

  4. 点击正确网址的帖子。

关于java - 无法在 Spring-boot 中使用自签名 https 验证来自 Postman 的 POST/GET 请求方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44131257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com