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


猜你喜欢
- Spring整合Myabtis思路的分析引入相关依赖SpringMyabtismysqlMybatsi-spring…
- JSONObject toJSONString错误1.com.alibaba.fastjson.JSONObject 继承了JSON可以使用
- 项目框架采用spring+hibernate+springMVC如果上传文件不想使用flash那么你可以采用HTML5;截图前段模块是boo
- Java当中的类和对象1. 类和对象的初步认知java 是一门面向对象的语言,所谓面向对象有别于面向过程,面向对象是只需对象之间的交互即可完
- 本文以C#及VB.NET后端程序代码示例展示如何将HTML转为XML文件。转换时,调用Word API -Free Spire.Doc fo
- 废话不多说,直接上代码Main代码package processdemo.example.administrator.processbard
- TabControl控件中TabPage选项卡切换时的触发事件选项卡切换触发的是TabControl控件的SelectedIndexChan
- 这里介绍一个简易的音乐播放器,供大家参考,具体内容如下效果图如下:但是,由于这是一个简易版的音乐播放器,所播放的音乐只有一首,且被写死,但,
- 代码如下一、创建CheckCode.xaml代码如下<ResourceDictionary xmlns="http
- 一、首先我们要获取Logcat中的日志如何获取呢?首先我们要先定义一个String[]数组,里面的代码是//第一个是Logcat ,也就是我
- 前言相信对于RxJava,大家应该都很熟悉,他最核心的两个字就是异步,诚然,它对异步的处理非常的出色,但是异步绝对不等于并发,更不等于线程安
- 分页application.ymlspring: datasource: url: jdbc:mysql://127.0.0.1/jpa?u
- OkHttp流程图OkHttp基本使用gradle依赖implementation 'com.squareup.okhttp3:ok
- 一、NIO基本简介NIO (New lO)也有人称之为java non-blocking lO是从Java 1.4版本开始引入的一个新的IO
- 这篇文章主要介绍了spring cloud Ribbon用法及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 目录什么是Feign为什么使用Feign为什么要使用HTTP client为什么要使用Feign如何使用Feign项目环境说明引入依赖入门例
- 一、下载Unity首先去官网下载对应版本的 UnityHubUnity官网地址: https://unity.cn/releases&nbs
- 多对多表之间关系表models.py文件代码from django.db import models# Create your models
- 这篇文章主要介绍了java文件下载代码实例(单文件下载和多文件打包下载),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考
- java解决动态配置字段需求是否在开发中遇到有像下图一样管理员配置多个字段让用户填写的需求我的实现方式是通过数据库存储动态json的显示实现