【问题标题】:Spring Boot 2: {"error":"unauthorized","error_description":"Full authentication is required to access this resource"}Spring Boot 2:{“error”:“unauthorized”,“error_description”:“访问此资源需要完全身份验证”}
【发布时间】:2020-01-13 19:25:46
【问题描述】:

我正在尝试升级到 Spring Boot 2(包括 Spring Security),但是 all 路径出现上述错误。我试图允许所有请求,但它仍然给出错误。

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception{
        http.authorizeRequests().anyRequest().permitAll();

我也尝试将此添加到application.properties

management.security.enabled=false

它仍然给出同样的错误。

Spring Boot 2.2.2

【问题讨论】:

    标签: java spring spring-boot spring-security


    【解决方案1】:

    最新版本默认启用 CSRF 保护,如果您正在开发端点(供非浏览器客户端使用),您可以在 WebSecurityConfig 中禁用 CSRF

    http.csrf().disable()
    

    【讨论】:

    • CSRF 和 GET 请求有什么关系?
    • 没什么,这就是为什么删除它是安全的(根据Spring配置)
    【解决方案2】:

    据此issue 报道

    management.security.enabled 已在 spring-boot 2 中删除

    对我来说,当我根据 documentation 将默认用户名和密码添加到 application.properties 时,它就起作用了

    spring.security.user.name=test
    spring.security.user.password=test
    

    并在请求标头中包含相同的内容。

    更新:
    有点晚才意识到我的回答不适合这个问题。

    要禁用自动配置,请参考:https://www.baeldung.com/spring-boot-security-autoconfiguration

    @SpringBootApplication(exclude = { SecurityAutoConfiguration.class })
    public class SpringBootSecurityApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBootSecurityApplication.class, args);
        }
    }
    

    【讨论】:

    • 我添加了 spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration(exclude = { SecurityAutoConfiguration.class })spring.security.user.name=testspring.security.user.password=test 并重新启动服务器并使用 curl -i -u test:test localhost:8080/robots.txt 进行测试,它给出了 WWW-Authenticate: Bearer realm="oauth2-resource", error="unauthorized", error_description="Full authentication is required to access this resource" 以及 JSON 错误。
    • 从错误看来您正在使用 OAuth2 。请参考documentation 进行自动配置。
    • 我是。我的授权服务器在 Spring 1.5 中使用 JDBC 令牌存储工作,并且在我专注于(新弃用的)OAuth2 授权服务器之前,我试图让基本的简单非安全请求首先工作。
    猜你喜欢
    • 2016-10-03
    • 2018-09-11
    • 2019-01-01
    • 2020-11-21
    • 2020-10-25
    • 2018-01-07
    • 2016-11-11
    • 2015-01-08
    • 2020-03-30
    相关资源
    最近更新 更多