欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot依靠security實現(xiàn)digest認證的實踐

 更新時間:2025年09月28日 15:15:39   作者:knight郭志斌  
HTTP摘要認證通過加密參數(shù)(如nonce、response)驗證身份,避免明文傳輸,但存在密碼存儲風險,相比基本認證更安全,卻因?qū)崿F(xiàn)復雜且不支持Remember-me,未被廣泛采用,測試案例顯示其工作流程

概述

HTTP 摘要認證使用對通信雙方都可知的口令進行校驗,最終的傳輸數(shù)據(jù)并非明文形式。

HTTP 摘要基本認證意在解決 HTTP 基本認證存在的大部分嚴重漏洞,但不應將其認為是Web安全的最終解決方案。

參數(shù)

HTTP摘要認證的回應與HTTP基本認證相比要復雜得多,下面看看HTTP摘要認證中涉及的一些參數(shù):

  • username:用戶名。
  • password:用戶密碼。
  • realm:認證域,由服務器返回。
  • opaque:透傳字符串,客戶端應原樣返回。
  • method:請求的方法。
  • nonce:由服務器生成的隨機字符串。
  • nc:即nonce-count,指請求的次數(shù),用于計數(shù),防止重放攻擊。qop被指定時,nc也必須被指定。
  • cnonce:客戶端發(fā)給服務器的隨機字符串,qop被指定時,cnonce也必須被指定。
  • qop:保護級別,客戶端根據(jù)此參數(shù)指定摘要算法。若取值為auth,則只進行身份驗證;若取值為auth-int,則還需要校驗內(nèi)容完整性。
  • uri:請求的uri。
  • response:客戶端根據(jù)算法算出的摘要值。
  • algorithm:摘要算法,目前僅支持MD5。
  • entity-body:頁面實體,非消息實體,僅在auth-int中支持。

通常服務器攜帶的數(shù)據(jù)包括realm、opaque、nonce、qop等字段,如果客戶端需要做出驗證回應,就必須按照一定的算法計算得到一些新的數(shù)據(jù)并一起返回。

總結(jié):

  • HTTP摘要認證與HTTP基本認證一樣,都是基于HTTP層面的認證方式,不使用session,因而不支持Remember-me。
  • 雖然解決了HTTP基本認證密碼明文傳輸?shù)膯栴},但并未解決密碼明文存儲的問題,依然存在安全隱患。
  • HTTP 摘要認證與 HTTP 基本認證相比,僅僅在非加密的傳輸層中有安全優(yōu)勢,但是其相對復雜的實現(xiàn)流程,使得它并不能成為一種被廣泛使用的認證方式。

Demo

pom.xml依賴

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 測試包,當我們使用 mvn package 的時候該包并不會被打入,因為它的生命周期只在 test 之內(nèi)-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

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

Digest1Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author gzb
 */
@RestController
@SpringBootApplication
public class Digest1Application {

    public static void main(String[] args) {
        SpringApplication.run(Digest1Application.class, args);
    }

    @GetMapping("/demo1")
    public String demo1() {
        return "Hello battcn";
    }

}

MyPasswordEncoder.java

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @author gzb
 * @date 2021/10/1315:06
 */
@Component
public class MyPasswordEncoder implements PasswordEncoder {
    @Override
    public String encode(CharSequence charSequence) {
        return charSequence.toString();
    }

    @Override
    public boolean matches(CharSequence charSequence, String s) {
        return s.equals(charSequence.toString());
    }
}

WebSecurityConfig.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
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;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.www.DigestAuthenticationEntryPoint;
import org.springframework.security.web.authentication.www.DigestAuthenticationFilter;

/**
 * @author gzb
 * @date 2021/10/1313:41
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private DigestAuthenticationEntryPoint myDigestEntryPoint;
    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public DigestAuthenticationEntryPoint digestEntryPoint() {
        DigestAuthenticationEntryPoint digestAuthenticationEntryPoint = new DigestAuthenticationEntryPoint();
        digestAuthenticationEntryPoint.setKey("https://blog.csdn.net/zhanwuguo8346");
        digestAuthenticationEntryPoint.setRealmName("spring security");
        digestAuthenticationEntryPoint.setNonceValiditySeconds(500);
        return digestAuthenticationEntryPoint;
    }

    public DigestAuthenticationFilter digestAuthenticationFilter() {
        DigestAuthenticationFilter filter = new DigestAuthenticationFilter();
        filter.setAuthenticationEntryPoint(myDigestEntryPoint);
        filter.setUserDetailsService(userDetailsService);
        return filter;
    }

    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .csrf().disable()
                .exceptionHandling().authenticationEntryPoint(myDigestEntryPoint)
                .and()
                .addFilter(digestAuthenticationFilter());
    }
}

application.properties

server.port=9090
server.servlet.context-path=/ditest

spring.security.user.name=name
spring.security.user.password=password

測試

  • 瀏覽器F12打開開發(fā)者界面
  • 啟動項目,瀏覽器訪問:http://localhost:9090/ditest/demo1
  • 輸入用戶名、密碼:name、password
  • 界面返回:Hello battcn

查看請求數(shù)據(jù):

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java創(chuàng)建線程的五種寫法總結(jié)

    Java創(chuàng)建線程的五種寫法總結(jié)

    本文主要為大家詳細介紹一下Java實現(xiàn)線程創(chuàng)建的五種寫法,文中的示例代碼講解詳細,對我們學習有一定的幫助,感興趣的可以跟隨小編學習一下
    2022-08-08
  • java語言中封裝類代碼示例

    java語言中封裝類代碼示例

    這篇文章主要介紹了java語言中封裝類,涉及相關(guān)代碼示例及結(jié)果分析,以及封裝的好處簡單介紹,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Java操作pdf的工具類itext的處理方法

    Java操作pdf的工具類itext的處理方法

    這篇文章主要介紹了Java操作pdf的工具類itext,iText是一種生成PDF報表的Java組件,通過在服務器端使用Jsp或JavaBean生成PDF報表,客戶端采用超鏈接顯示或下載得到生成的報表,需要的朋友可以參考下
    2022-04-04
  • JavaWeb利用郵箱幫用戶找回密碼

    JavaWeb利用郵箱幫用戶找回密碼

    這篇文章主要為大家詳細介紹了JavaWeb利用郵箱幫用戶找回密碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • JPA中@ElementCollection使用示例詳解

    JPA中@ElementCollection使用示例詳解

    在JPA中,@ElementCollection注解主要用于映射集合屬性,例如List、Set或數(shù)組等集合屬性,以及Map結(jié)構(gòu)的集合屬性,每個屬性值都有對應的key映射,這篇文章主要介紹了JPA中@ElementCollection使用,需要的朋友可以參考下
    2023-11-11
  • java中的反射及其優(yōu)點說明

    java中的反射及其優(yōu)點說明

    這篇文章主要介紹了java中的反射及其優(yōu)點說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • JUnit4 Hamcrest匹配器常用方法總結(jié)

    JUnit4 Hamcrest匹配器常用方法總結(jié)

    這篇文章主要介紹了JUnit4 Hamcrest匹配器常用方法總結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11
  • SpringBoot基于Redis實現(xiàn)短信登錄的操作

    SpringBoot基于Redis實現(xiàn)短信登錄的操作

    驗證碼登錄是非常常見的一種登錄方式,能夠簡化用戶登錄的過程,本文主要介紹了SpringBoot基于Redis實現(xiàn)短信登錄的操作,具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • JVM中ClassLoader類加載器的深入理解

    JVM中ClassLoader類加載器的深入理解

    這篇文章主要給大家介紹了關(guān)于JVM中ClassLoader類加載器的相關(guān)資料,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • 使用Java實現(xiàn)MapReduce詞頻統(tǒng)計示例代碼

    使用Java實現(xiàn)MapReduce詞頻統(tǒng)計示例代碼

    這篇文章主要介紹了使用Java實現(xiàn)MapReduce詞頻統(tǒng)計的相關(guān)資料,通過詞頻統(tǒng)計示例來展示MapReduce的運行機制,涵蓋了Mapper和Reducer的實現(xiàn),并說明了如何配置和執(zhí)行MapReduce作業(yè),需要的朋友可以參考下
    2024-11-11

最新評論