Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.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
Java Spring安全性-尽管允许所有请求,但未经授权_Java_Spring_Spring Boot_Spring Security_Unauthorized - Fatal编程技术网

Java Spring安全性-尽管允许所有请求,但未经授权

Java Spring安全性-尽管允许所有请求,但未经授权,java,spring,spring-boot,spring-security,unauthorized,Java,Spring,Spring Boot,Spring Security,Unauthorized,我在Spring Boot应用程序中有这样一个web配置: import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.we

我在Spring Boot应用程序中有这样一个web配置:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests().anyRequest().permitAll()
        .and()
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
                .httpBasic();
    }
}

当试图访问其中一个URL(localhost:8080/test)时,我得到了未授权。我做错了什么?

我的快照与“.httpBasic();”相关,当您在属性中设置此项时,您似乎希望进行基本身份验证。

我的快照与“.httpBasic();”相关,当您在属性中设置此项时,您似乎希望获得基本身份验证。

我的快照是您的
网络配置是。
如果您的
@SpringBootApplication
注释类位于
com.example.demo
然后,您的
WebConfig
类应该放在
com.example.demo
包(或其他子包,例如:
com.example.demo.config
)下


package com.example.demo.config;// 我的镜头是你的
WebConfig
是。 如果您的
@SpringBootApplication
注释类位于
com.example.demo
然后,您的
WebConfig
类应该放在
com.example.demo
包(或其他子包,例如:
com.example.demo.config
)下


package com.example.demo.config;// 如果您获得未授权(401),则意味着身份验证失败,无论您是否有权访问资源。

您使用的是基本身份验证,spring流有两部分,身份验证和授权,首先它进行身份验证,以了解用户是否有效,然后查看他们是否有权访问资源。在这种情况下,给出错误的原因是它没有身份验证,更确切地说是401。如果您有身份验证但没有授权,您将收到403禁止如果您获得未授权(401),这意味着身份验证失败,无论您是否有权访问资源。
您使用的是基本身份验证,spring流有两部分,身份验证和授权,首先它进行身份验证,以了解用户是否有效,然后查看他们是否有权访问资源。在这种情况下,给出错误是因为它没有身份验证,更确切地说是401,如果您有身份验证但没有授权,您将收到一个403禁止

,因为我错误地创建了一级以上的配置包。。。谢谢你的帮助!由于错误,我创建了一级以上的配置包。。。谢谢你的帮助!