直接使用security.basic.path无效|——springboot2.0以上的security的配置

问题

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,百度关键词【SpringSecurity拦截请求

原创文章,欢迎转载,转载请注明出处!

原文地址:https://www.cnblogs.com/acm-bingzi/p/springboot-security.html

时间: 2024-08-30 06:57:35

直接使用security.basic.path无效|——springboot2.0以上的security的配置的相关文章

SpringBoot2.0 基础案例(03):配置系统全局异常映射处理

一.异常分类 这里的异常分类从系统处理异常的角度看,主要分类两类:业务异常和系统异常. 1.业务异常 业务异常主要是一些可预见性异常,处理业务异常,用来提示用户的操作,提高系统的可操作性.常见的业务异常提示:1)请输入xxx2)xxx不能为空3)xxx重复,请更换 2.系统异常 系统异常主要是一些不可预见性异常,处理系统异常,可以让展示出一个友好的用户界面,不易给用户造成反感.如果是一个金融类系统,在用户界面出现一个系统异常的崩溃界面,很有可能直接导致用户流失.常见的系统异常提示:1)页面丢失4

SpringBoot2.0 基础案例(11):配置AOP切面编程,解决日志记录业务

本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.AOP切面编程 1.什么是AOP编程 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP(面向对象编程)的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型.利用AOP可以对业务逻辑的各个部

SpringBoot2.0 基础案例(15):配置MongoDB数据库,实现增删改查逻辑

一.NoSQL简介 1.NoSQL 概念 NoSQL( Not Only SQL ),意即"不仅仅是SQL".对不同于传统的关系型数据库的数据库管理系统的统称.NoSQL用于超大规模数据的存储.这些类型的数据存储不需要固定的模式,无需多余操作就可以横向扩展. 2.NoSQL的优点/缺点 --优点: 高可扩展性 分布式计算 低成本 架构的灵活性,半结构化数据 没有复杂的关系 --缺点: 没有标准化 有限的查询功能(到目前为止) 数据展现不直观 二.MongoDB数据库 1.MongoDB

SpringBoot2.0 基础案例(02):配置Log4j2,实现不同环境日志打印

一.Log4j2日志简介 日志打印是了解Web项目运行的最直接方式,所以在项目开发中是需要首先搭建好的环境. 1.Log4j2特点 1)核心特点 相比与其他的日志系统,log4j2丢数据这种情况少:disruptor技术,在多线程环境下,性能高:并发的特性,减少了死锁的发生. 2)性能测试 2.日志打印之外观模式 每一种日志框架都有自己单独的API,要使用对应的框架就要使用其对应的API,增加应用程序代码和日志框架的耦合性. <阿里巴巴Java开发手册>,其中有一条规范做了『强制』要求: SL

SpringBoot2.0+Shiro+JWT 整合

SpringBoot2.0+Shiro+JWT 整合 JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使用 JWT 在用户和服务器之间传递安全可靠的信息. 我们利用一定的编码生成 Token,并在 Token 中加入一些非敏感信息,将其传递. 安装环境 开发工具:STS Maven版本:apache-maven-3.5.2 java jdk 1.8 MySQL版本:5.7 系统:Windows10 一.新建Maven项目 配置Maven项目的pom.xml文件 <pr

关于springboot2.0与activiti6.0.0集成的问题

spring boot2.0与activiti6.0集成后不能直接使用,会抛出如下异常 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMv

SpringBoot2.0.3 + SpringSecurity5.0.6 + vue 前后端分离认证授权

新项目引入安全控制 项目中新近添加了Spring Security安全组件,前期没怎么用过,加之新版本少有参考,踩坑四天,终完成初步解决方案.其实很简单,Spring Security5相比之前版本少了许多配置,操作起来更轻量 MariaDb登录配置加密策略 SpringSecurity5在执行登录认证时,需预设加密策略. 坑一:加密策略配置,验密始终不通过,报错401 坑二:本地重写的UserDetailsService实现类在注入的时候找不到,目前图省事直接用了 @Qualifier制定 其

spring-boot-2.0.3不一样系列之shiro - 搭建篇

前言 上一篇:spring-boot-2.0.3不一样系列之国际化,讲了如何实现国际化,实际上我工作用的模版引擎是freemaker,而不是thymeleaf,不过原理都是相通的. 接着上一篇,这一篇我来讲讲spring-boot如何整合工作中用到的一个非常重要的功能:安全,而本文的主角就是一个安全框架:shiro. Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人也越来越多,因为它相当简单,对比Spring Security,可能没有Spring Sec

零基础快速入门SpringBoot2.0教程 (四)

一.JMS介绍和使用场景及基础编程模型简介:讲解什么是小写队列,JMS的基础知识和使用场景 1.什么是JMS: Java消息服务(Java Message Service),Java平台中关于面向消息中间件的接口 2.JMS是一种与厂商无关的 API,用来访问消息收发系统消息,它类似于JDBC(Java Database Connectivity).这里,JDBC 是可以用来访问许多不同关系数据库的 API 3.使用场景: 1)跨平台 2)多语言 3)多项目 4)解耦 5)分布式事务 6)流量控