Spring MVC Unauthorized问题
Spring MVC Unauthorized问题
接口源码:
@RestController
@RequestMapping("user/")
@Api(tags = "用户管理")
public class UserController {private final UserService userService;@Autowiredpublic UserController(UserService userService) {this.userService = userService;}@PostMapping("login")@ApiOperation("用户登陆")public Message login(@RequestBody UserEntity loginUser) {return userService.login(loginUser);}
}
使用postman访问接口,得到的信息:
{"timestamp": "2020-12-14 02:00:55","status": 401,"error": "Unauthorized","message": "Unauthorized","path": "/user/test_token"
}
使用浏览器访问接口,自动跳转到的页面:
问题排查:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>
系统使用了Spring Security,而Spring Security默认对所有路径进行权限认证,并且提供默认的登陆页面。如果系统最终没有使用到Spring Security,将该依赖移除即可解决问题;如果系统确实需要使用Spring Security,那么可以自定义路径鉴权方式:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity httpSecurity) throws Exception {httpSecurity.authorizeRequest() // 直接放行.antMatchers("/auth/**", "/error/**", "/dev/**").permitAll()// 权限认证.anyRequest().authenticated();}
}
Spring Security默认的configure(HttpSecurity httpSecurity)实际上等同于如下配置:
@Overrideprotected void configure(HttpSecurity httpSecurity) throws Exception {httpSecurity.authorizeRequest()// 对所有http请求进行权限认证.anyRequest().authenticated().and()// 支持基于表单的登陆.formLogin().and()// 支持基于Basic方式的认证.httpBasic();}
同时,由于没有重载configure(AuthenticationManagerBuilder),所有系统没有用户存储支撑认证过程,所以系统相当于没有用户,所以没有人可以认证成功。
Spring MVC Unauthorized问题相关推荐
- Java-Shiro(三):Shiro与Spring MVC集成
新建Java Daynamic Web项目 导入Spring.SpringMVC依赖包: 导入Spring & Spring MVC包(导入如下所有开发包): Spring AOP依赖扩展包: ...
- Java之Spring mvc详解(非原创)
文章大纲 一.Spring mvc介绍 二.Spring mvc代码实战 三.项目源码下载 四.参考文章 一.Spring mvc介绍 1. 什么是springmvc springmvc是spri ...
- spring mvc 关键接口 HandlerMapping HandlerAdapter
HandlerMapping Spring mvc 使用HandlerMapping来找到并保存url请求和处理函数间的mapping关系. 以DefaultAnnotationHandle ...
- spring mvc 控制器方法传递一些经验对象的数组
由于该项目必须提交一个表单,其中多个对象,更好的方法是直接通过在控制器方法参数的数组. 因为Spring mvc框架在反射生成控制方法的參数对象的时候会调用这个类的getDeclaredConstru ...
- Spring MVC 4
Spring MVC 4 项目文件结构 pom.xml依赖 <properties><endorsed.dir>${project.build.directory}/endor ...
- java注解返回不同消息,Spring MVC Controller中的一个读入和返回都是JSON的方法如何获取javax.validation注解的异常信息...
Spring MVC Controller中的一个读入和返回都是JSON的方法怎么获取javax.validation注解的错误信息? 本帖最后由 LonelyCoder2012 于 2014-03- ...
- Spring MVC前后端的数据传输
本篇文章主要介绍了Spring MVC中如何在前后端传输数据. 后端 ➡ 前端 在Spring MVC中这主要通过Model将数据从后端传送到前端,一般的写法为: @RequestMapping(va ...
- 番外:Spring MVC环境搭建和Mybatis配置避坑篇
2019独角兽企业重金招聘Python工程师标准>>> web.xml引入对spring mvc的支持: spring-mvc配置spring-mvc: spring-mybatis ...
- spring mvc velocity 配置备忘
2019独角兽企业重金招聘Python工程师标准>>> Spring里面最重要的概念是IOC和AOP,还有两项很重要的模块是事务和MVC,对于IOC和AOP,我们要深究其源码实现,对 ...
最新文章
- java rectangle获取xy_JFreeChart 动态移动线 并求出两条线交点XY
- 调查用QQ企业邮箱的smtp需要添加spf1
- 电脑qq文件服务器地址,在电脑上接收QQ地理位置打不开,提示获取不到详细地址...
- MySQL给用户设置密码(修改密码)、创建用户,grant为某个IP、某个网段或所有IP地址创建访问用户并授权
- C++中如何读取一个数的位数_求1000以内的水仙花数
- C++(23)--多态性与虚函数
- 【计算机系统】指令流水线
- netcore docker_深入浅出 ASP.NET Core 与 Docker 入门课程目标说明
- Webrct之demo运行
- php连接阿里云mysql
- 内核中的TCP的追踪分析-9-TCP(IPV4)的socket的地址绑定--续2
- HYSPLIT 模型 传输轨迹 使用指南
- NRF24L01/SI24R1广播通讯
- 深入解读Redis之数据类型解析-SDS
- 英特尔cpu天梯图排名(附台式机CPU天梯图2022年3月份最新版精简图)
- PIM是什么意思,如何做好企业产品信息管理?
- gradle配置全局镜像
- Cryptarithmetic Problem ‘ODD+ODD == EVEN’;map()函数,reduce()
- 【BYM】Android 仿百度搜索列表滑动效果,又到一年金三银四
- 基于微信小程序的婚纱影楼门户小程序
热门文章
- sdut-String+array1(Collection-Map)-1 读中国载人航天史,汇航天员数量,向航天员致敬
- springboot毕设项目多层级架构的工程项目安全监管系统的设计与实现tx75u(java+VUE+Mybatis+Maven+Mysql)
- Weblogic Xa数据源和非Xa数据源的选择
- 《学姐教我写代码(二)》一行代码一个算法(上)
- 浅析企业为什么办理涉密资质?湖北通过涉密资质的企业有哪些?
- OSI七层模型和TCP/IP协议
- Gephi 手动安装插件
- golang strings包使用
- Secret vs. Whisper: 谁才是最好的匿名社交APP?
- c语言程序设计河北,2020春C语言程序设计赵玲玲单元测试参考答案查询,工程经济与应用...