简体   繁体   English

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

[英]Can't authenticate a POST/GET request method from Postman, in Spring-boot with self-signed https

I'm making a rest endpoint in my spring boot application and can't get my: 我在Spring Boot应用程序中创建了一个rest端点,但无法获取我的信息:

@RequestMapping(method = RequestMethod.POST) 

or: 要么:

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

To run from postman. 从邮递员那里跑。 Here is the security-config file: 这是security-config文件:

@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");
}

Without the self-signed certificate we implemented and no authorization, the POST method in postman has been working fine. 没有我们实现的自签名证书,也没有授权,邮递员中的POST方法一直运行良好。 I can't figure out how the authorization is preventing my POST method from running. 我不知道授权如何阻止我的POST方法运行。 I have already entered the correct user credentials in basic auth, under authorization in postman. 在邮递员的授权下,我已经在基本身份验证中输入了正确的用户凭据。 Current header output: 当前标题输出:

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

There must be something wrong with my configure method since if I provide incorrect password it should not return a 404, but it still does. 我的configure方法一定存在问题,因为如果我提供了错误的密码,它应该不会返回404,但仍然会返回404。 When executing the application i get the following output in the console: 执行应用程序时,我在控制台中得到以下输出:

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()

This is just for test purposes to check if the rest endpoint responds. 这只是出于测试目的,以检查其余端点是否响应。 Here is the code: 这是代码:

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

I also updated my configure method to work with https, and i am trying with the following url: 我还更新了configure方法以与https一起使用,并且尝试使用以下网址:

https://localhost:5434/test

Running the POST method from postman, gives the following IDE console output: 从邮递员运行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

Following this post i added the: 在这篇文章之后,我添加了:

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

This restored my security to default and the console printed a one time password. 这将我的安全性恢复为默认值,并且控制台打印了一次密码。 Trying this in postman returned a 401(unauthorized) when i wrote the wrong password, but still 404 when the credentials where correct. 当我输入错误的密码时,在邮递员中尝试此操作会返回401(未授权),但是当凭据正确时仍返回404。 This post had a similar issue, but non of them helped. 这篇文章有类似的问题,但没有一个人提供帮助。

Here is my project structure for mye application classes and rest classes: 这是mye应用程序类和rest类的项目结构:

在此处输入图片说明

---------------------------------UPDATE----------------------------------------- ---------------------------------更新---------------- -------------------------

So i narrowed the issue down to the authentication. 因此,我将问题范围缩小到身份验证。 Somewhere in the securityConfig file, either the global or other configure mode is somehow giving the 404 status. 在securityConfig文件中的某个位置,全局或其他配置模式都会以某种方式给出404状态。

Is the inMemoryAuthentication different from the basic authentication in postman? inMemoryAuthentication是否与邮递员的基本身份验证不同? Or is the httpSecurity methods wrong somehow? 还是httpSecurity方法不正确?

When i remove the entire securityConfig class, spring generates a default password which works fine and i get the desired output from running POST. 当我删除整个securityConfig类时,spring会生成一个默认密码,该密码可以正常工作,并且我通过运行POST获得所需的输出。

Try following: 请尝试以下操作:

  1. Goto Authorization tab in postman. 邮递员中的“转到授权”选项卡。

  2. Select basic auth 选择基本身份验证

  3. Enter username and password. 输入用户名和密码。

  4. Hit post with correct url. 点击具有正确网址的帖子。

@Configuration
public class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {

    // overides deafult password, here you can add additional users
    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("*******").password("******").roles("USER")
                .and()
                .withUser("*****").password("*****").roles("USER", "ADMIN");
        System.out.println("global configurer finished");
    }
}

By removing the securityConfig class, and replacing it whit this, the authentication worked fine. 通过删除securityConfig类,并替换它,身份验证工作正常。

The test class was only for test purposes and ResoucreNotFoundException was not necessary either. 测试类仅用于测试目的,也不需要ResoucreNotFoundException。

If you are getting 404 (Resource not found), at that point the SSL handshake already happened, shouldn't matter if self-signed or not. 如果您收到404(找不到资源),则此时SSL握手已经发生,无论是否自签名都无关紧要。 And if authentication / authorization fails, you should be getting 401 or 403. 并且,如果身份验证/授权失败,则应该获得401或403。

Are you sure you have a mapping for @RequestMapping(value = "/test", method = RequestMethod.POST) ? 您确定您有@RequestMapping(value = "/test", method = RequestMethod.POST)的映射吗?

Seems the controller you shared only maps to /test - RequestMethod.GET 似乎您仅共享的控制器映射到/ test-RequestMethod.GET

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Spring Boot 2.0 中使用自签名证书启用 HTTPS - Enable HTTPS with self-signed certificate in Spring Boot 2.0 无法从 postman 向我的 docker spring-boot 容器发送请求 - Can't send request from postman to my docker spring-boot container 无法在控制器Spring-boot中测试身份验证所需的方法 - Can't test authenticate-required method in controller Spring-boot Spring Boot-具有自签名证书的客户端服务器REST API - Spring Boot - client server REST API with self-signed certificate Spring 引导 - 自签名 mTLS - 必要的证书 - Spring Boot - self-signed mTLS - necessary certificate 带有 Android 和自签名服务器证书的 HTTPS GET (SSL) - HTTPS GET (SSL) with Android and self-signed server certificate Spring-boot rest api当前请求不是邮递员的多部分请求 - Spring-boot rest api current request is not multipart request with postman 带有 csrf 标头的 Spring-Boot Post 请求 - Spring-Boot Post request with csrf header 通过Axios发送发布请求会在Spring-Boot后端生成一个空的RequestBody。 在Postman中工作,但不能通过Axios发布请求工作 - Sending a post request through Axios is generating an empty RequestBody in Spring-Boot backend. Works in Postman but not through Axios post request kotlin连接到自签名https服务器 - kotlin connect to self-signed https server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM