【问题标题】:Can't authenticate a POST/GET request method from Postman, in Spring-boot with self-signed https无法在 Spring-boot 中使用自签名 https 验证来自 Postman 的 POST/GET 请求方法
【发布时间】:2017-10-23 04:43:39
【问题描述】:

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

@RequestMapping(method = RequestMethod.POST) 

或:

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

为了逃避邮递员。这是安全配置文件:

@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 方法运行的。在邮递员的授权下,我已经在基本身份验证中输入了正确的用户凭据。当前标题输出:

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

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

这是我的应用程序类和休息类的项目结构:

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

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

inMemoryAuthentication 和 postman 中的基本认证有区别吗?还是 httpSecurity 方法有问题?

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

【问题讨论】:

  • 您如何从 Postman 进行身份验证(登录)?
  • 基本授权下的授权
  • 404 并不意味着验证请求时出现问题。这意味着该地址上没有任何东西在监听。您是否仔细检查了您的网址,确保服务器确实在工作(并且没有静默崩溃)等?
  • 尝试配置和读取日志并仔细检查您请求的 URL 是否使用适当的 HTTP 方法在您的应用范围内注册

标签: java spring-boot postman


【解决方案1】:

尝试以下操作:

  1. 在邮递员中转到授权选项卡。

  2. 选择基本身份验证

  3. 输入用户名和密码。

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

【讨论】:

  • 我只收到 404 响应,没有其他内容
【解决方案2】:
@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");
    }
}

通过删除 securityConfig 类并用它替换它,身份验证工作正常。

测试类仅用于测试目的,也不需要 ResoucreNotFoundException。

【讨论】:

    【解决方案3】:

    如果您收到 404(找不到资源),此时 SSL 握手已经发生,是否自签名无关紧要。 如果身份验证/授权失败,您应该得到 401 或 403。

    您确定您有@RequestMapping(value = "/test", method = RequestMethod.POST) 的映射吗?

    您共享的控制器似乎只映射到 /test - RequestMethod.GET

    【讨论】:

    • 我只添加了测试类来检查我是否可以得到一些工作,对于我用 post 方法检查过的每个代码更新
    猜你喜欢
    • 1970-01-01
    • 2023-02-25
    • 2019-08-23
    • 2018-08-25
    • 2015-06-28
    • 2019-12-11
    • 2020-09-12
    • 2018-01-23
    • 2021-03-17
    相关资源
    最近更新 更多