问题

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拦截请求

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

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

  1. SpringBoot2.0 基础案例(16):配置Actuator组件,实现系统监控

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Actuator简介 1.监控组件作用 在生产环境中,需要实时 ...

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

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.AOP切面编程 1.什么是AOP编程 在软件业,AOP为Asp ...

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

    一.异常分类 这里的异常分类从系统处理异常的角度看,主要分类两类:业务异常和系统异常. 1.业务异常 业务异常主要是一些可预见性异常,处理业务异常,用来提示用户的操作,提高系统的可操作性. 常见的业务 ...

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

    本文源码:GitHub·点这里 || GitEE·点这里 一.NoSQL简介 1.NoSQL 概念 NoSQL( Not Only SQL ),意即"不仅仅是SQL".对不同于传统 ...

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

    一.Log4j2日志简介 日志打印是了解Web项目运行的最直接方式,所以在项目开发中是需要首先搭建好的环境. 1.Log4j2特点 1)核心特点 相比与其他的日志系统,log4j2丢数据这种情况少:d ...

  6. springboot2.0拦截器和webconfigure配置

    接下来介绍一下springboot如何配置拦截器,很简单,只需要两个配置文件就可以了 首先配置登陆拦截器 @Component public class LoginInterceptor implem ...

  7. 在Springboot2.0项目中使用Druid配置多数据源

    在Springboot出现之前配置数据源以及相关的事物,缓存等内容一直是个繁琐的工作,但是Springboot出现后这些基本都可以靠默认配置搞定,就变得很轻松了.这就是现在推崇模板>配置的原因, ...

  8. SpringBoot2.0+Shiro+JWT 整合

    SpringBoot2.0+Shiro+JWT 整合 JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使用 JWT 在用户和服务器之间传递安全可靠的信息. 我们利用一定的编 ...

  9. SpringBoot2.0集成Shiro

    1.shiro的三个核心概念: 1)Subject:代表当前正在执行操作的用户,但Subject代表的可以是人,也可以是任何第三方系统帐号.当然每个subject实例都会被绑定到SercurityMa ...

随机推荐

  1. 一个6亿的表a,一个3亿的表b,通过外间tid关联,你如何最快的查询出满足条件的第50000到第50200中的这200条数据记录

    1.如果A表TID是自增长,并且是连续的,B表的ID为索引 select * from a,b where a.tid = b.id and a.tid>500000 limit 200; 2. ...

  2. vue 使用sass 和less

    npm i sass-loader --save -dev(-D)

  3. [转帖]IP地址、子网掩码、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?

    IP地址.子网掩码.网络号.主机号.网络地址.主机地址以及ip段/数字-如192.168.0.1/24是什么意思? 2016年03月26日 23:38:50 JeanCheng 阅读数:105674  ...

  4. [转帖]Linux 下如何知道是否有人在使坏?

    Linux 下如何知道是否有人在使坏? 学到了两个最简单的命令 usermod -L username 锁定账户 passwd -s username 查看用户状态. 自己linux 知道的还是少 需 ...

  5. Java案例-用户注册邮箱绑定激活功能实现

    <–start–> 需求描述:当客户打开收到邮箱激活码的邮件,点击激活链接,正确填写激活码后就会完成邮箱激活的步骤. 在后台编程代码编写中,有以下几个要点: ① 接收客户的手机号码和邮箱激 ...

  6. hive权限配置

    基于CDH5.x的Hive权限配置 1.打开权限控制,默认是没有限制的 set hive.security.authorization.enabled=true; 2.配置默认权限 hive.secu ...

  7. 初次启动hive,解决 ls: cannot access /home/hadoop/spark-2.2.0-bin-hadoop2.6/lib/spark-assembly-*.jar: No such file or directory问题

    >>提君博客原创  http://www.cnblogs.com/tijun/  << 刚刚安装好hive,进行第一次启动 提君博客原创 [hadoop@ltt1 bin]$ ...

  8. unsupported time zone specified undefined

    unsupported time zone specified undefined   出现了这个问题,莫名其妙的,上次被我下回去了,真的是下回去的,我一去查看,坐在电脑前面问题就不见了…… 具体出现 ...

  9. Linux的基础命令, django的安装与使用

    一. Linux一些基础指令 cat命令, 用于查看纯文本文件(常用于内容较少的) cat 校花的故事.txt # 查看文件 cat -n 校花的故事.txt # 查看文件并显示行号 -n 显示行号 ...

  10. python之路--MySQL多表查询

    一 介绍 我们在写项目的时候一般都会建一个数据库,数据库里面会存很多的表,不可能把所有的数据都放在一张表里,因为分表来存数据节省空间,数据的组织结构更清晰,解耦和程度更高,但是这些表本质上还不是一个整 ...