栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring boot依靠security实现digest认证demo

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot依靠security实现digest认证demo

概述

HTTP 摘要认证使用对通信双方都可知的口令进行校验,最终的传输数据并非明文形式。HTTP 摘要基本认证意在解决 HTTP 基本认证存在的大部分严重漏洞,但不应将其认为是Web安全的最终解决方案。

参数

HTTP摘要认证的回应与HTTP基本认证相比要复杂得多,下面看看HTTP摘要认证中涉及的一些参数:
◎ username:用户名。
◎ password:用户密码。
◎ realm:认证域,由服务器返回。
◎ opaque:透传字符串,客户端应原样返回。
◎ method:请求的方法。
◎ nonce:由服务器生成的随机字符串。
◎ nc:即nonce-count,指请求的次数,用于计数,防止重放攻击。qop被指定时,nc也必须被指
定。
◎ cnonce:客户端发给服务器的随机字符串,qop被指定时,cnonce也必须被指定。
◎ qop:保护级别,客户端根据此参数指定摘要算法。若取值为auth,则只进行身份验证;若取
值为auth-int,则还需要校验内容完整性。
◎ uri:请求的uri。
◎ response:客户端根据算法算出的摘要值。
◎ algorithm:摘要算法,目前仅支持MD5。
◎ entity-body:页面实体,非消息实体,仅在auth-int中支持。
通常服务器携带的数据包括realm、opaque、nonce、qop等字段,如果客户端需要做出验证回应,就必须按照一定的算法计算得到一些新的数据并一起返回。

总结

HTTP摘要认证与HTTP基本认证一样,都是基于HTTP层面的认证方式,不使用session,因而不支持Remember-me。虽然解决了HTTP基本认证密码明文传输的问题,但并未解决密码明文存储的问题,依然存在安全隐患。HTTP 摘要认证与 HTTP 基本认证相比,仅仅在非加密的传输层中有安全优势,但是其相对复杂的实现流程,使得它并不能成为一种被广泛使用的认证方式。

Demo pom.xml依赖
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
			org.springframework.boot
			spring-boot-starter-security
		

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;


@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;


@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;


@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打开开发者界面

启动项目,浏览器访问:http://localhost:9090/ditest/demo1

输入用户名、密码:name、password

界面返回:Hello battcn

查看请求数据

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322814.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号