22/09/2018, 18:51

[Spring boot + Spring Security] Security with Digest Authentication

1. Prepare Tools IDE: Netbean 8.2 JDK: 1.8 Maven: 3.5.0 2. Target Build project thỏa mãn các yêu cầu sau: Sử dụng spring boot + spring security Sử dụng digest authentication để bảo mật tài nguyên qua http Sử dụng annotation configuration thay cho xml configuration. ...

1. Prepare Tools

  • IDE: Netbean 8.2
  • JDK: 1.8
  • Maven: 3.5.0

2. Target

Build project thỏa mãn các yêu cầu sau:

  • Sử dụng spring boot + spring security
    
  • Sử dụng digest authentication để bảo mật tài nguyên qua http
    
  • Sử dụng annotation configuration thay cho xml configuration.
    

3. Maven dependency

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

4. Project setup and description

1. Basic Project

https://viblo.asia/p/spring-boot-spring-security-basic-project-1VgZvEmpKAw

2. Digest Authentication

Tương tự như Basic Authentication, Digest Authentication cũng là một tiêu chuẩn bảo vệ tài nguyên qua http. Khác với Basic, Digest bảo mật hơn bằng cách ngăn chặn được việc decode chuỗi Authorization trên header của bản tin http. Cụ thể quá trình xác thực như thế nào thì mọi người google nhé.

3. DigestAuthenticationFilter

Tương tự như Basic Authentication, việc xử lý digest authen cũng được thông qua một filter. Sping cũng hỗ trợ luôn một class làm chức năng trên DigestAuthenticationFilter

    DigestAuthenticationFilter digestAuthenticationFilter() throws Exception {
        DigestAuthenticationFilter digestAuthenticationFilter = new DigestAuthenticationFilter();
        digestAuthenticationFilter.setUserDetailsService(userDetailsService);
        digestAuthenticationFilter.setAuthenticationEntryPoint(digestEntryPoint());
        return digestAuthenticationFilter;
    }

3. DigestAuthenticationEntryPoint

Tương tự Basic Authentication, Digest cũng cần một cái entryEndpoint để xây dựng cấu trúc header response lại client cho đúng flow. Spring cũng hỗ trợ luôn một class làm việc này

    @Bean
    DigestAuthenticationEntryPoint digestEntryPoint() { 
        DigestAuthenticationEntryPoint dAuth = new DigestAuthenticationEntryPoint();
        dAuth.setRealmName("soithatha");
        return dAuth;
    }

Việc của chúng ta là dùng lại.

4. SecurityConfig

Add thêm filter vào trong cấu hình httpsecurity

  http.addFilter(digestAuthenticationFilter()) 
                .exceptionHandling().authenticationEntryPoint(digestEntryPoint()) 
                .and()
                .authorizeRequests()
                .antMatchers("/login").permitAll() 
                .anyRequest().authenticated()

5. Demo

6. Full Source

spring-security-digest-authentication https://github.com/soithattha/spring-security.git

0