Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在没有设置安全性的情况下,为什么spring rest控制器未经授权返回_Spring_Spring Boot - Fatal编程技术网

在没有设置安全性的情况下,为什么spring rest控制器未经授权返回

在没有设置安全性的情况下,为什么spring rest控制器未经授权返回,spring,spring-boot,Spring,Spring Boot,我已经启动了一个spring启动应用程序。我添加了一个像这样的rest控制器 @RestController public class EmailController { @RequestMapping(value = "/2", method = RequestMethod.GET) public int getNumber() { return 2; } } 当我调用url时,我得到一个401异常 {"timestamp":149840920866

我已经启动了一个spring启动应用程序。我添加了一个像这样的rest控制器

@RestController
public class EmailController {

    @RequestMapping(value = "/2", method = RequestMethod.GET)
    public int getNumber() {
        return 2;
    }
}
当我调用url时,我得到一个401异常

{"timestamp":1498409208660,"status":401,"error": 
 "Unauthorized","message":"Full authentication is 
 required to access this resource", "path":"/2"}

我根本就没有安全装置,这是怎么回事。这是一个干净的spring启动项目!为什么我收到未经授权的异常。

您未配置身份验证(表单登录、HTTP Basic等),因此使用默认的AuthenticationEntryPoint,请参阅Spring Security API:

设置要使用的AuthenticationEntryPoint

如果未指定authenticationEntryPoint(authenticationEntryPoint),则将使用defaultAuthenticationEntryPointFor(authenticationEntryPoint,RequestMatcher)。将使用第一个AuthenticationEntryPoint,因为默认情况下未找到匹配项

如果未提供,则默认为Http403禁止入口点。
您可以将AuthenticationEntryPoint设置为@ksokol编写或配置身份验证,该身份验证定义了一个AuthenticationEntryPoint。

用于寻找解决方案的人员。我将此添加到我的应用程序中。yml

security: 
  ignored: /**

在主类中添加@EnableWebSecurity注释,它将忽略访问该应用程序的默认安全性

示例代码如下:

@EnableWebSecurity
    @SpringBootApplication

    public class SampleApplication {
        SampleApplication (){}
        public static void main(String[] args) {
            SpringApplication.run(SampleApplication .class, args);
        }

检查
pom.xml
文件中是否未包含依赖项
spring boot starter security

添加依赖项树如果添加spring security,默认情况下spring boot中的基本身份验证处于启用状态。如果不需要安全性,为什么要将spring security添加到应用程序中?我没有!我使用的是spring boot,我认为它在默认情况下会启用它,因为我在我的项目中找不到任何spring安全性。只有在类路径上存在spring安全性依赖项,否则安全性甚至不会激活时,才会启用它。因此,一定有一个依赖关系在拉动Spring安全性。我在最新的Spring启动版本中遇到了相同的错误。我一直在四处寻找,但还没有找到根本原因。它根本不会影响REST控制器。我可以在catalina访问日志中看到请求被记录(40x错误),但在catalina中没有任何记录。[out | log]。这确实使调试变得非常困难。谷歌没有提供任何结构化的方法来调试这个错误。听起来像是配置问题,但是哪种配置?哪一个模块会引入安全性(如果有的话)?如何找出这种虚假的隐式依赖关系?