Springboot配置security basic path无效解决方案
作者:贾树丙 发布时间:2023-07-12 21:42:50
问题
springcloud 版本 为 Finchley.RELEASE
springboot 版本为 2.0.3.RELEASE
现在有需求,/swagger-ui.html 页面需要添加登录认证,但是本来的接口不需要登录认证
升级springboot之前的做法是直接在application.yml 文件中添加以下配置:
security:
basic:
enabled: true # 启用SpringSecurity的安全配置项
path: /swagger-ui.html
user:
name: aijianzi # 认证用户名
password: course # 认证密码
role: # 授权角色
- USER
升级后这种配置就出错了,连编译都出错,如下图:
解决过程
查找源代码,找到如下:
来自:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide
Security
Spring Boot 2 greatly simplifies the default security configuration and makes adding custom security easy. Rather than having several security-related auto-configurations, Spring Boot now has a single behavior that backs off as soon as you add your own WebSecurityConfigurerAdapter.You are affected if you were using any of the following properties:
security.basic.authorize-mode
security.basic.enabled
security.basic.path
security.basic.realm
security.enable-csrf
security.headers.cache
security.headers.content-security-policy
security.headers.content-security-policy-mode
security.headers.content-type
security.headers.frame
security.headers.hsts
security.headers.xss
security.ignored
security.require-ssl
security.sessions
翻译:Spring Boot 2极大地简化了默认的安全配置,并使添加定制安全性变得更加容易。Spring Boot并没有使用几个与安全相关的自动配置,而是在添加自己的WebSecurityConfigurerAdapter时就有了一个单独的行为。如果您使用以下属性,您将受到影响
再找到:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-Security-2.0
Security Auto-configuration
Spring Boot 2.0 does not provide separate auto-configuration for user-defined endpoints and actuator endpoints. When Spring Security is on the classpath, the auto-configuration secures all endpoints by default. It adds the @EnableWebSecurity annotation and relies on Spring Security's content-negotiation strategy to determine whether to use httpBasic or formLogin. A user with a a default username and generated password is added, which can be used to login.
翻译:Spring Boot 2.0没有为用户定义的端点和执行器端点提供单独的自动配置。当Spring Security在类路径上时,自动配置默认为所有端点。它添加了@EnableWebSecurity 注释,并依赖于Spring Security的内容协商策略来决定是否使用httpBasic或formLogin。添加了一个默认用户名和生成密码的用户,这可以用来登录。
解决
对于不同的URL,安全性是不同的,关键在于重载WebSecurityConfigurerAdapter 类的configure(HttpSecurity) 方法。具体可以参考以上的两个链接
我的完整实现如下:
1、pom.xml 中添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、application.yml 文件中配置登录用户名和密码(如果只到这里,那么所有的请求都会被拦截)
spring:
security:
user:
name: admin
password: admin
3、添加自定义的配置类,注解@Configuration @EnableWebSecurity
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/**
* @author jiashubing
* @since 2018/7/16
*/
@Configuration
@EnableWebSecurity
public class ActuatorWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
//普通的接口不需要校验
.antMatchers("/courseApi/**").permitAll()
// swagger页面需要添加登录校验
.antMatchers("/swagger-ui.html").authenticated()
.and()
.formLogin();
}
}
当然也可以配置成需要某个角色的用户才能查看某些URL
来源:https://www.cnblogs.com/acm-bingzi/p/springboot-security.html
猜你喜欢
- Java接口(interface)的概念及使用在抽象类中,可以包含一个或多个抽象方法;但在接口(interface)中,所有的方法必须都是抽
- 返回值类型:System.Int64表示为 Windows 文件时间的当前 DateTime 对象的值。例如:DateTime.Now.To
- 树的同构备忘!定义:给定两棵树r1、r2,如果r1可以通过若干次的左子树和右子树互换,使之与r2完全相同,这说明两者同构。举例树的构造树可以
- Java 执行 JS 脚本工具用途:为了便于系统扩展,提供了 JS 脚本的功能,可以通过在系统中执行脚本来获得更复杂的功能。例如:系统提供了
- 程序如下:View Code /* * Hanoi塔游戏 问题描述: * 汉诺塔:汉诺塔(又称河内塔)问
- 正常maven依赖jar包的pom.xml写法如下:<!-- https://mvnrepository.com/artifact/o
- 经过几天的折腾,终于到了学习一个重量级的查询方式上,使用@Query注解,使用注解有两种方式,一种是JPQL的SQL语言方式,一种是原生SQ
- Java异常是Java提供的一种识别及响应错误的一致性机制。Java异常机制可以使程序中异常处理代码和正常业务代码分离,保证程序代码更加优雅
- Java为TCP协议提供了两个类,分别在客户端编程和服务器端编程中使用它们。在应用程序开始通信之前,需要先创建一个连接,由客户端程序发起;而
- Lambda 表达式Lambda 表达式是现代 C++ 中最重要的特性之一,而 Lambda 表达式,实际上就是提供了一个类似匿名函数的特性
- 本文实例为大家分享了Unity shader实现遮罩效果的具体代码,供大家参考,具体内容如下效果:shader代码:Shader "
- 我就废话不多说了,大家还是直接看代码吧~ @Test void testJava8ForeachMap() { Map<String,
- 这篇文章主要介绍了SpringBoot Jpa分页查询配置方式解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价
- 群主发普通红包,某群有多名成员,群主给成员发普通红包,普通红包的规则:群主的一笔金额,从群主余额中扣除,平均分成n等份,让成员领取;成员领取
- 这篇文章主要介绍了springboot跨域CORS处理代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 下面给大家介绍下mybatis结果生成键值对的实例代码,具体内容如下所示:在实际应用中我们经常会遇到这样的情况,需要给下拉框赋值,这个时候就
- 1、super的使用:(1)super是一个关键字。(2)super和this很类似,我们对比着学习。2、先复习一下this关键字的使用。(
- 效果图片重写DataGridView的OnRowPostPaint方法或者直接在DataGridView的RowPostPaint事件里写,
- 本文实例讲述了Java二叉搜索树基础原理与实现方法。分享给大家供大家参考,具体如下:前言:本文通过先通过了解一些二叉树基础知识,然后在转向学
- 本文实例总结了Android开发中Toast显示消息的方法。分享给大家供大家参考,具体如下:Android中提供一种简单的Toast消息提示