一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Springboot配置security basic path無效解決方案

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

2020-09-19 15:49賈樹丙 Java教程

這篇文章主要介紹了Springboot配置security basic path無效解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

問題

springcloud 版本 為 Finchley.RELEASE

springboot 版本為 2.0.3.RELEASE

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

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

?
1
2
3
4
5
6
7
8
9
 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 中添加依賴:

?
1
2
3
4
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>

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

?
1
2
3
4
5
spring:
 security:
 user:
  name: admin
  password: admin

3、添加自定義的配置類,注解@Configuration @EnableWebSecurity

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: aa一级护士医生毛片 | 国产成人亚洲精品乱码在线观看 | 香蕉久久久久 | 日日操视频 | 美女在线看永久免费网址 | 好湿好紧太硬了我太爽了h 好湿好滑好硬好爽好深视频 | 无码一区二区三区视频 | 亚洲 综合 自拍 精品 在线 | jk制服喷水 | 超级毛片 | 成人欧美一区二区三区 | 四虎国产欧美成人影院 | 四虎最新永久免费视频 | 日本片免费观看一区二区 | 久久精品小视频 | 色花堂国产精品首页第一页 | 国产午夜一区二区在线观看 | 亚洲天堂精品视频 | 午夜性爽视频男人的天堂在线 | 星空无限传媒xk8027穆娜 | 欧美一区二区三区不卡视频 | 成年人在线视频免费观看 | 福利国产片 | 99精品视频在线观看免费 | 175m美女被网友灌醉啪啪玩脚 | 娜娜啪| 欧美国产在线观看 | 福利国模私拍视频在线观看 | 好舒服好爽再快点视频 | 欧美一级视频在线观看 | 91看片淫黄大片.在线天堂 | 韩国免费特一级毛片 | japanesexxxx在线播放 | 国产一级片视频 | 免费岛国| www.亚洲色图| 草莓香蕉绿巨人丝瓜榴莲18 | 日本高清无吗 | 亚洲六月丁香婷婷综合 | 亚洲天堂色视频 | 99热久热这里只精品 |