【问题标题】:Spring boot getting 401 unauthorized status code for simple get requestSpring Boot 获取简单获取请求的 401 未授权状态码
【发布时间】:2017-11-19 17:52:15
【问题描述】:

我对 Spring 框架很陌生。我创建了一个包含以下模块的新 Spring Starter 项目:web、mongo、security。

我已经创建了一个简单的控制器

@RestController
@RequestMapping("/users")
public class UserController {

    private UserRepository userRepository;

    @GetMapping("/all")
    public List<User> getAllUsers(){
        List<User> users = this.userRepository.findAll();
        return users;
    }

    @PostMapping("/")
    public void insert(@RequestBody User user){
        this.userRepository.save(user);
    }
}

并将一些原始数据播种到数据库中。当我在 Postman 中向这条路线发出请求时,我得到以下响应:

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

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

<groupId>ngt</groupId>
<artifactId>someArtifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>dermaskin</name>
<description>Demo project for Spring Boot with mongodb</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

是什么导致了未经授权的响应以及如何为/all 路由禁用它?谢谢!

【问题讨论】:

  • 好的,现在从您的 pom 文件中删除或注释 spring-boot-starter-security。然后再试一次。
  • 但如果某些路线需要它?例如登录和注册我不需要它,但对于其他剩余路线我需要它

标签: spring spring-boot spring-security


【解决方案1】:
@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.requestMatchers().antMatchers("/users/all").permitAll();
    }
}

您需要配置 Spring Security,默认情况下所有路由都为授权保护。

以上代码仅对“/users/all”网址禁用安全性。

【讨论】:

    【解决方案2】:

    我在加入时遇到了这个错误

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-oauth2-client</artifactId>
    </dependency>
    

    在我的 pom.xml 文件中。 只需将其删除,然后重试。

    【讨论】:

    • 这不是答案。如果您在尝试提供帮助时遇到错误,应添加评论。
    • @thehandofNOD - 你确定吗?
    • @WaiHaLee:差不多。这篇文章如何回答这个问题?在问题中,作者甚至没有包含 oauth2-client 依赖项。其他答案也提供了信息。
    • @thehandofNOD - 好吧,无论如何,它现在看起来有点不像“我也有这个问题”的答案。我认为这是试图回答这个问题。
    • @WaiHaLee:但我读起来是这样的:“我添加了依赖项...oauth2-client 出错了,所以只需将其删除并重试。它有效。听起来不像是解决问题的解决方案对我来说。
    【解决方案3】:

    你可以保持你的安全依赖,但你必须设置一个用户名和密码。这可以通过将以下内容添加到位于下的 application.properties 文件中来完成 src/main/resources 文件夹

    security.user.name=user # Default user name.
    security.user.password= # your password here
    

    【讨论】:

    • sts 告诉我 spring.security.enabled=false 是一个未知属性
    • 这只是一个警告,不应该阻止它工作。你还有 401 错误吗?
    • @AchillesVan 我听从了你的建议,在模块的资源中创建了一个 application.properties 并添加了你提到的文本,我仍然在所有 API 上得到 401 '
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 2019-03-02
    • 2011-03-18
    • 1970-01-01
    • 2021-08-28
    • 2020-06-27
    相关资源
    最近更新 更多