无法通过自签名https在Spring-boot中对Postman的POST / GET请求方法进行身份验证

时间:2017-05-23 09:45:03

标签: java spring-boot postman

我在我的春季启动应用程序中创建了一个休息终点并且无法获取我的:

@RequestMapping(method = RequestMethod.POST) 

或:

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

从邮递员那里跑。这是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");
}

如果没有我们实施的自签名证书且没有授权,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,我正在尝试使用以下网址:

https://localhost:5434/test

从邮递员运行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有一个类似的问题,但没有帮助。

这是mye应用程序类和其他类的项目结构:

enter image description here

--------------------------------- UPDATE ------------- ----------------------------

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

inMemoryAuthentication与postman中的基本身份验证不同吗?或者httpSecurity方法是否以某种方式出错?

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

3 个答案:

答案 0 :(得分:1)

@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类并将其替换为whit,认证工作正常。

测试类仅用于测试目的,并且ResoucreNotFoundException也不是必需的。

答案 1 :(得分:0)

请尝试以下操作:

  1. 转发邮递员的授权标签。

  2. 选择基本身份验证

  3. 输入用户名和密码。

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

答案 2 :(得分:0)

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

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

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