22/09/2018, 18:51

[Spring boot + Spring Security] Security with Basic 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 basic 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 basic 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. Basic Authentication

  • Basic Authentication là một chuẩn về bảo mật nguồn tài nguyên thông qua http. Cho phép xác thực bằng cách mã hóa username và password gửi lên dưới dạng base64. Ví dụ cặp username/password sẽ được mã hóa như sau: soithattha:123456 -> c29pdGhhdGhhOjEyMzQ1Ng==
  • Thông tin mã hóa trên sẽ được push vào trong http requestheader với thuộc tính là Authorization.Trên thực chế, basic authentication để lộ khá nhiều nhược điểm, bài viết này m chỉ dừng lại ở việc giới thiệu basicauthentication với spring. không đi sâu vấn đề này.

3. BasicAuthenticationEntryPoint

Theo follow xử lý BasicAuthenticatin, khi có request gửi lên, server cần trả về bản tin response với status=401 và header WWW-Authenticate có chứa chuỗi realm. Spring hỗ trợ một class để xử lý vấn đề này, người dùng chỉ cần extends class này và Override method commence để đặc tả lại logic là được

public class MyAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {

    private String REALM = "soithatha";

    @Override
    public void commence(
            HttpServletRequest request,
            HttpServletResponse response,
            AuthenticationException authException) throws IOException {
        // voi cac request khong xac thuc thanh cong, du lieu se duoc tra ve o day
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        response.addHeader("WWW-Authenticate", "Basic realm=" + REALM + "");
        PrintWriter writer = response.getWriter();
        writer.println("HTTP Status 401 : " + authException.getMessage());

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        setRealmName(REALM);
        super.afterPropertiesSet(); //To change body of generated methods, choose Tools | Templates.
    }

}

4. SecurityConfig

Bật mode xử lý basic authentication ở httpsecurity config http.httpBasic().authenticationEntryPoint(restAuthenticationEntryPoint); Bản chất của việc này là add thêm một filter ở tầng filter theo kiến thúc m đã mô tả ở bài https://viblo.asia/p/spring-boot-spring-security-overview-Ljy5VBgj5ra

5. Demo

6. Full Source

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

0