【问题标题】:"Full authentication is required to access this resource" Unauthorised - 401 error in spring security example“访问此资源需要完全身份验证”未经授权 - 春季安全示例中的 401 错误
【发布时间】:2018-06-04 06:45:39
【问题描述】:

我启用了 Websecurity,但我启用了 anyRequest() 和 permitAll() 方法,但我仍然在邮递员中收到此错误。以下是我在配置中的代码。

@EnableWebSecurity
@Configuration
public class SecurityConfifguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception {

            httpSecurity.authorizeRequests()
                        .anyRequest()
                        .permitAll()
                        .and().httpBasic().disable();

            httpSecurity.csrf().disable();              

    }
}

以下是控制器代码

@RestController
@RequestMapping("/rest/hello")
public class HelloResource {
     @GetMapping
     public String hello() {
         return "Hello World";
     }
}

应用程序类

@ComponentScan
@SpringBootApplication
public class SpringSecurityExampleApplication {

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

邮递员出错

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

推荐但需要帮助

httpSecurity
      .anonymous() //allow anonymous access
      .and()
         .authorizeRequests()
            .antMatchers("**/rest/**").permitAll();

我希望它返回“Hello World”作为响应。

第二次推荐但没有帮助

        httpSecurity
         //some configuration
//            .and()
              .anonymous() //allow anonymous access
              .and()
                 .authorizeRequests()
                    .antMatchers("/rest/**").permitAll();

【问题讨论】:

    标签: java spring spring-boot spring-security


    【解决方案1】:

    配置文件和资源不在同一个包中,所以我得到了错误。现在它把它们移到它开始工作的主包下。

    【讨论】:

    • 这是一个很好的观点。如果要设置 Spring Boot 安全性,所有文件都应该在同一个包中。
    猜你喜欢
    • 2020-09-02
    • 2022-07-22
    • 2018-06-29
    • 2020-12-23
    • 2021-01-24
    • 2020-03-30
    • 2020-10-25
    • 2015-11-03
    • 2022-07-20
    相关资源
    最近更新 更多