[英]Can't authenticate a POST/GET request method from Postman, in Spring-boot with self-signed https
我在Spring Boot应用程序中创建了一个rest端点,但无法获取我的信息:
@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");
}
没有我们实现的自签名证书,也没有授权,邮递员中的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
我的configure方法一定存在问题,因为如果我提供了错误的密码,它应该不会返回404,但仍然会返回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";
}
}
我还更新了configure方法以与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
在这篇文章之后,我添加了:
@ComponentScan(basePackages={"package name of where the controller is"})
这将我的安全性恢复为默认值,并且控制台打印了一次密码。 当我输入错误的密码时,在邮递员中尝试此操作会返回401(未授权),但是当凭据正确时仍返回404。 这篇文章有类似的问题,但没有一个人提供帮助。
这是mye应用程序类和rest类的项目结构:
---------------------------------更新---------------- -------------------------
因此,我将问题范围缩小到身份验证。 在securityConfig文件中的某个位置,全局或其他配置模式都会以某种方式给出404状态。
inMemoryAuthentication是否与邮递员的基本身份验证不同? 还是httpSecurity方法不正确?
当我删除整个securityConfig类时,spring会生成一个默认密码,该密码可以正常工作,并且我通过运行POST获得所需的输出。
请尝试以下操作:
邮递员中的“转到授权”选项卡。
选择基本身份验证
输入用户名和密码。
点击具有正确网址的帖子。
@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。
如果您收到404(找不到资源),则此时SSL握手已经发生,无论是否自签名都无关紧要。 并且,如果身份验证/授权失败,则应该获得401或403。
您确定您有@RequestMapping(value = "/test", method = RequestMethod.POST)
的映射吗?
似乎您仅共享的控制器映射到/ test-RequestMethod.GET
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.