【发布时间】: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