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

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

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

概述

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

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

參數(shù)

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

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

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

總結(jié):

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

Demo

pom.xml依賴

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 測(cè)試包,當(dāng)我們使用 mvn package 的時(shí)候該包并不會(huì)被打入,因?yàn)樗纳芷谥辉?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

測(cè)試

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

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

總結(jié)

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

相關(guān)文章

  • Idea中Springboot熱部署無(wú)效問(wèn)題解決

    Idea中Springboot熱部署無(wú)效問(wèn)題解決

    這篇文章主要介紹了Idea中Springboot熱部署無(wú)效問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • App登陸java后臺(tái)處理和用戶權(quán)限驗(yàn)證

    App登陸java后臺(tái)處理和用戶權(quán)限驗(yàn)證

    這篇文章主要為大家詳細(xì)介紹了App登陸java后臺(tái)處理和用戶權(quán)限驗(yàn)證,感興趣的朋友可以參考一下
    2016-06-06
  • java代碼mqtt接收發(fā)送消息方式

    java代碼mqtt接收發(fā)送消息方式

    這篇文章主要介紹了java代碼mqtt接收發(fā)送消息方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Java中HashMap和Hashtable的區(qū)別淺析

    Java中HashMap和Hashtable的區(qū)別淺析

    這篇文章主要介紹了Java中HashMap和Hashtable的區(qū)別淺析,本文總結(jié)了6條它們之間的不同之處,需要的朋友可以參考下
    2015-03-03
  • SpringBoot2升級(jí)到SpringBoot3后Nacos熱更新失效問(wèn)題分析

    SpringBoot2升級(jí)到SpringBoot3后Nacos熱更新失效問(wèn)題分析

    SpringBoot升級(jí)至SpringBoot3后Nacos配置熱更新失效的原因原因及解決方法,本文詳細(xì)分析了問(wèn)題產(chǎn)生的原因原因原因并提供了具體解決方案,包括移除spring-cloud-starter-bootstrap依賴、統(tǒng)一配置文件格式及注解配置等確保配置熱更新功能正常運(yùn)作,需要的朋友可以參考下
    2026-05-05
  • Spring Cloud @EnableFeignClients注解的屬性字段basePacka詳解

    Spring Cloud @EnableFeignClients注解的屬性字段basePacka詳解

    這篇文章主要介紹了Spring Cloud @EnableFeignClients注解的屬性字段basePacka詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringDataJpa:JpaRepository增刪改查操作

    SpringDataJpa:JpaRepository增刪改查操作

    這篇文章主要介紹了SpringDataJpa:JpaRepository增刪改查操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 深入了解Java線程池的原理使用及性能優(yōu)化

    深入了解Java線程池的原理使用及性能優(yōu)化

    JAVA線程池是一種管理和復(fù)用線程資源的機(jī)制,可以提高程序的效率和響應(yīng)速度。本文將介紹線程池的原理、使用方法和性能優(yōu)化技巧,幫助讀者深入了解和應(yīng)用JAVA線程池
    2023-04-04
  • Java中CRUD的快速實(shí)現(xiàn)方法

    Java中CRUD的快速實(shí)現(xiàn)方法

    Java中的CRUD(Create、Read、Update、Delete)操作是數(shù)據(jù)持久化的基礎(chǔ)操作,通常用于與數(shù)據(jù)庫(kù)交互,這篇文章主要介紹了Java CRUD的快速實(shí)現(xiàn),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2025-10-10
  • java實(shí)現(xiàn)簡(jiǎn)單的搜索引擎

    java實(shí)現(xiàn)簡(jiǎn)單的搜索引擎

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單的搜索引擎的相關(guān)資料,需要的朋友可以參考下
    2016-02-02

最新評(píng)論

锡林郭勒盟| 沙坪坝区| 文昌市| 五大连池市| 枝江市| 丰镇市| 尉氏县| 巴林右旗| 泰和县| 芷江| 四子王旗| 朔州市| 屏东市| 东宁县| 克山县| 鹤山市| 新乡市| 潼南县| 许昌县| 正镶白旗| 金阳县| 南木林县| 白山市| 鲁甸县| 广东省| 从化市| 邢台县| 灵石县| 宁阳县| 汉寿县| 潜江市| 台南市| 锦屏县| 安福县| 丰台区| 堆龙德庆县| 依兰县| 故城县| 河源市| 霍山县| 衡水市|