当前位置: 首页 > news >正文

Spring Security无法调用接口错误解决

之前在写程序的时候,发现有个接口使用postmapping发送请求一直无法进行调用

@PostMapping("/user/login")public ResponseResult login(User user){//登录,这里登录的时候需要传入用户名和密码System.out.println("user = "+user.toString());return loginService.login(user);//调用LoginServiceImpl中的login方法}

并且返回的一直是forbidden401,应该是没有权限,但是奇怪的是,我在SecurityConfig之中权限已经打开了

public class SecurityConfig extends WebSecurityConfigurerAdapter {@Beanpublic PasswordEncoder passwordEncoder(){//只需要把BCryptPasswordEncoder对象注入Spring容器中//SpringSecurity就会使用PasswordEncoder来进行校验return new BCryptPasswordEncoder();}//自定义AuthenticationManager@Overrideprotected void configure(HttpSecurity http) throws Exception {System.out.println("###begin configure");http//关闭csrf.csrf().disable()//不通过Session获取SecurityContext.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests()//对于登录接口允许匿名访问.antMatchers("/user/login").anonymous()//除上面外所有请求需要鉴权认证.anyRequest().authenticated();}//调用//暴露自定义的AuthenticationManager@Bean@Overridepublic AuthenticationManager authenticationManagerBean() throws Exception{return super.authenticationManagerBean();}
}

经过排查之后发现,这里的SecurityConfig上面没有加上@Configuration这个注解,导致SecurityConcig没有被注入到Spring的容器之中,因此报错
ps:如果发现传入的User结果为null,尝试着在参数前面加入@RequestBody这一个标签

@PostMapping("/user/login")
public ResponseResult login(@RequestBody User user){//登录,这里登录的时候需要传入用户名和密码return loginService.login(user);//调用LoginServiceImpl中的login方法
}
http://www.lryc.cn/news/146964.html

相关文章:

  • 运维Shell脚本小试牛刀(二)
  • 飞天使-python的模块与包与装饰器
  • linux shell脚本利用 kill -0 检查进程是否存在
  • 抖音视频删了怎么在电脑上找回来
  • 方面级别情感分析之四元组预测
  • 算法 稀疏数组 数组优化 数组压缩 二维数组转稀疏数组 算法合集(二)
  • 交换机端口安全实验
  • c# 本地化中英文切换
  • rabbitmq的优先级队列
  • SpringBoot的Cacheable缓存注解
  • uniapp的 picker 日期时间选择器
  • element ui-Pagination
  • [开发|java] 将数组使用环境变量传递配置给typesafe配置示例
  • MAC苹果电脑如何压缩rar文件?
  • 浅析编程中的语法糖
  • 【【萌新的STM32学习23----数据通信的基本类型】】
  • 标准库STL容器使用值语义
  • dockerfile 命令详解(三)
  • 使用这个插件,fiddler抓包直接生成httprunner脚本
  • 干翻Dubbo系列第十五篇:Rest协议基于SpringBoot的规范化开发
  • 文件上传后端处理页面
  • 小红书母婴类产品同质化严重,如何在市场中脱颖而出?
  • Typora上使用Mermaid语法展示流程图、时序图、甘特图
  • css中文本阴影特效
  • ITIL帮助台怎样帮助企业建设IT服务?
  • 解释区块链技术的应用场景和优势
  • 从编程语言的角度来理解正则表达式
  • DP读书:鲲鹏处理器 架构与编程(十四)ACPI与软件
  • C#,《小白学程序》第六课:队列(Queue)的应用,《实时叫号系统》
  • mysql profiling profiles profile