奶茶精Gaaa 2025-01-04 19:31 采纳率: 46.2%
浏览 44

401错误,springboot

为什么报401错误?怎么解决

img

代码如下

package com.itheima.springbootquickstart;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication

public class SpringbootQuickstartApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringbootQuickstartApplication.class, args);
    }

}




#数据源
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis
    username: root
    password: 123456
mybatis:
  configuration:
    map-underscore-to-camel-case: true #开启驼峰命名和下划线命名的转换


package com.itheima.springbootquickstart.service;

import com.itheima.springbootquickstart.pojo.User;

public interface UserService  {
    public User findById(Integer id);


}


package com.itheima.springbootquickstart.service.impl;
import org.springframework.stereotype.Service;

import com.itheima.springbootquickstart.mapper.UserMapper;

import com.itheima.springbootquickstart.pojo.User;
import com.itheima.springbootquickstart.service.UserService;
import com.itheima.springbootquickstart.utils.Md5Util;
import com.itheima.springbootquickstart.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import com.itheima.springbootquickstart.pojo.User;
import com.itheima.springbootquickstart.service.UserService;
import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findById(Integer id){

        return  userMapper.findById(id);
    }

}


package com.itheima.springbootquickstart.pojo;

public class User {

    private Integer id;
    private String name;
    private Short age;
    private Short gender;
    private String phone;

    public User() {
    }

    public User(Integer id, String name, Short age, Short gender, String phone) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.phone = phone;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Short getAge() {
        return age;
    }

    public void setAge(Short age) {
        this.age = age;
    }

    public Short getGender() {
        return gender;
    }

    public void setGender(Short gender) {
        this.gender = gender;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender=" + gender +
                ", phone='" + phone + '\'' +
                '}';
    }
}


package com.itheima.springbootquickstart.mapper;
import com.itheima.springbootquickstart.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper {
    @Select("select * from user where id= #{id}")
    public User findById(Integer id);

}



package com.itheima.springbootquickstart.controller;
import com.itheima.springbootquickstart.pojo.User;
import com.itheima.springbootquickstart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.itheima.springbootquickstart.pojo.Result;

import com.itheima.springbootquickstart.utils.JwtUtil;
import com.itheima.springbootquickstart.utils.Md5Util;
import com.itheima.springbootquickstart.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;

import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@RestController
@Validated

public class UserController {
        @Autowired
    private UserService userService;
    @RequestMapping("/findById")

    public User findById(Integer id){
        return userService.findById(id);

    }



}



  • 写回答

4条回答 默认 最新

  • 关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    HTTP 401错误通常表示请求未经授权。在Spring Boot应用中,这通常意味着客户端(浏览器或其他客户端)试图访问一个需要身份验证的资源,但并未提供正确的身份验证凭据。下面是一些可能导致HTTP 401错误的原因以及相应的解决方案:

    可能的原因和解决方案:

    1. 缺少认证信息

    如果你的API需要认证(例如,基于token的认证),而请求没有附带正确的认证信息(如token),服务器会返回401错误。确保你的请求头包含了正确的认证信息。

    2. 认证信息无效

    即使请求包含了认证信息,如果这些信息无效(例如,token已过期),服务器也会返回401错误。确保你使用的认证凭据是有效的。

    3. Spring Security配置问题

    如果你的应用使用了Spring Security,可能是安全配置的问题。检查你的Spring Security配置,确保它允许你的请求通过。

    4. 跨域问题(CORS)

    在某些情况下,跨域请求可能会引发401错误。确保你的应用正确处理了CORS请求,并且允许来自你的前端或其他客户端的跨域请求。

    代码检查点:

    • 检查UserController中的findById方法是否需要进行身份验证。如果需要,确保请求包含了正确的认证信息。
    • 检查Spring Security的配置(如果有的话),确保相关端点配置正确,允许无认证访问或带有正确凭据的访问。
    • 检查数据库连接和其他服务层配置,确保它们正常运行并且配置正确。

    额外注意事项:

    • 确保数据库连接信息(如用户名、密码、URL等)是正确的,并且数据库服务是可用的。
    • 检查任何自定义的过滤器、拦截器或Spring Security的配置,确保它们不会意外地阻止合法的请求。
    • 查看应用的日志,寻找任何可能的错误或警告信息,这可能会提供关于问题的更多线索。

    根据你提供的代码和描述,可能需要进一步调查以确定问题的具体原因。如果问题仍然存在,你可能需要更详细地检查代码和配置,或者考虑使用调试工具来逐步跟踪问题。

    评论

报告相同问题?

问题事件

  • 创建了问题 1月4日