最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot配置security basic path無效解決方案

 更新時(shí)間:2020年09月19日 11:31:35   作者:賈樹丙  
這篇文章主要介紹了Springboot配置security basic path無效解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

問題

springcloud 版本 為 Finchley.RELEASE

springboot 版本為 2.0.3.RELEASE

現(xiàn)在有需求,/swagger-ui.html 頁面需要添加登錄認(rèn)證,但是本來的接口不需要登錄認(rèn)證

升級(jí)springboot之前的做法是直接在application.yml 文件中添加以下配置:

security:
 basic:
  enabled: true # 啟用SpringSecurity的安全配置項(xiàng)
  path: /swagger-ui.html
 user:
  name: aijianzi # 認(rèn)證用戶名
  password: course # 認(rèn)證密碼
  role:    # 授權(quán)角色
  - USER

升級(jí)后這種配置就出錯(cuò)了,連編譯都出錯(cuò),如下圖:

解決過程

查找源代碼,找到如下:

來自: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極大地簡化了默認(rèn)的安全配置,并使添加定制安全性變得更加容易。Spring Boot并沒有使用幾個(gè)與安全相關(guān)的自動(dòng)配置,而是在添加自己的WebSecurityConfigurerAdapter時(shí)就有了一個(gè)單獨(dú)的行為。如果您使用以下屬性,您將受到影響

再找到: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沒有為用戶定義的端點(diǎn)和執(zhí)行器端點(diǎn)提供單獨(dú)的自動(dòng)配置。當(dāng)Spring Security在類路徑上時(shí),自動(dòng)配置默認(rèn)為所有端點(diǎn)。它添加了@EnableWebSecurity 注釋,并依賴于Spring Security的內(nèi)容協(xié)商策略來決定是否使用httpBasic或formLogin。添加了一個(gè)默認(rèn)用戶名和生成密碼的用戶,這可以用來登錄。

解決

對于不同的URL,安全性是不同的,關(guān)鍵在于重載WebSecurityConfigurerAdapter 類的configure(HttpSecurity) 方法。具體可以參考以上的兩個(gè)鏈接

我的完整實(shí)現(xiàn)如下:

1、pom.xml 中添加依賴:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2、application.yml 文件中配置登錄用戶名和密碼(如果只到這里,那么所有的請求都會(huì)被攔截)

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()
        //普通的接口不需要校驗(yàn)
        .antMatchers("/courseApi/**").permitAll()
        // swagger頁面需要添加登錄校驗(yàn)
        .antMatchers("/swagger-ui.html").authenticated()
        .and()
        .formLogin();
  }
}

當(dāng)然也可以配置成需要某個(gè)角色的用戶才能查看某些URL

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring Boot定時(shí)任務(wù)的使用實(shí)例代碼

    Spring Boot定時(shí)任務(wù)的使用實(shí)例代碼

    這篇文章主要介紹了Spring Boot定時(shí)任務(wù)的使用實(shí)例代碼,需要的朋友可以參考下
    2017-04-04
  • Java算法之堆排序代碼示例

    Java算法之堆排序代碼示例

    這篇文章主要介紹了Java算法之堆排序代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • idea中的lombok不生效的四種解決方法

    idea中的lombok不生效的四種解決方法

    Lombok項(xiàng)目是一個(gè)java庫,它可以自動(dòng)插入到編輯器和構(gòu)建工具中,本文將詳細(xì)給大家介紹idea中的lombok不生效的四種解決方法,需要的朋友可以參考下
    2023-05-05
  • java 分布式與集群的區(qū)別和聯(lián)系

    java 分布式與集群的區(qū)別和聯(lián)系

    本文主要介紹了java分布式與集群的區(qū)別和聯(lián)系,具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • 使用feign調(diào)用接口時(shí)調(diào)不到get方法的問題及解決

    使用feign調(diào)用接口時(shí)調(diào)不到get方法的問題及解決

    這篇文章主要介紹了使用feign調(diào)用接口時(shí)調(diào)不到get方法的問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Spring中bean集合注入的方法詳解

    Spring中bean集合注入的方法詳解

    Spring作為項(xiàng)目中不可缺少的底層框架,提供的最基礎(chǔ)的功能就是bean的管理了。bean的注入相信大家都比較熟悉了,但是有幾種不太常用到的集合注入方式,可能有的同學(xué)會(huì)不太了解,今天我們就通過實(shí)例看看它的使用
    2022-07-07
  • SpringBoot配置嵌入式Servlet容器和使用外置Servlet容器的教程圖解

    SpringBoot配置嵌入式Servlet容器和使用外置Servlet容器的教程圖解

    這篇文章主要介紹了SpringBoot配置嵌入式Servlet容器和使用外置Servlet容器的教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Java正則表達(dá)式之Pattern類實(shí)例詳解

    Java正則表達(dá)式之Pattern類實(shí)例詳解

    Pattern類的作用在于編譯正則表達(dá)式后創(chuàng)建一個(gè)匹配模式,下面這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式之Pattern類的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-01-01
  • java ImmutableMap的使用說明

    java ImmutableMap的使用說明

    這篇文章主要介紹了java ImmutableMap的使用說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • spring裝配bean的3種方式總結(jié)

    spring裝配bean的3種方式總結(jié)

    這篇文章主要給大家介紹了關(guān)于spring裝配bean的3種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03

最新評(píng)論

泰州市| 溆浦县| 油尖旺区| 宜阳县| 梧州市| 浦北县| 盐源县| 南和县| 河东区| 嘉峪关市| 博野县| 科技| 理塘县| 清流县| 扬中市| 顺义区| 大安市| 元氏县| 屏山县| 汤原县| 云南省| 尤溪县| 太谷县| 望城县| 土默特右旗| 金湖县| 孟连| 开江县| 宁都县| 阿克陶县| 丹寨县| 鹤壁市| 卢龙县| 新建县| 白河县| 长宁区| 新乡县| 临沧市| 磐石市| 海门市| 建始县|