Skip to content

Latest commit

 

History

History
 
 

spring-boot-webflux-security

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

spring-boot-webflux-security

ตัวอย่างการเขียน Spring-boot WebFlux Security

1. เพิ่ม Dependencies

pom.xml

...
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>

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

...

2. เขียน Main Class

@SpringBootApplication
@ComponentScan(basePackages = {"com.pamarin"}) 
public class AppStarter {

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

}

3. Config Spring-Security

@Slf4j
@EnableWebFluxSecurity
public class SecurityConfig {

    @Bean
    public ReactiveUserDetailsService reactiveUserDetailsService(PasswordEncoder passwordEncoder) {
        return username -> {
            log.debug("login with username => {}", username);
            return Mono.just(
                    User.withUsername(username)
                            .password(passwordEncoder.encode("password"))
                            .authorities(Collections.emptyList())
                            .build()
            );
        };
    }
    
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

}
  • @EnableWebFluxSecurity เป็นการ enable Spring Security
  • method passwordEncoder() เป็นการประกาศใช้ password encoder เป็น BCryptPasswordEncoder
  • method reactiveUserDetailsService() เป็นการประกาศ login service ว่าถ้ามีการ login เข้ามาให้ find user จาก username ที่ส่งมาใน service นี้ ซึ่งปกติจะ find จาก database ถ้าไม่เจอ อาจจะ throw error org.springframework.security.authentication.BadCredentialsException ออกไป
  • เบื้องต้น สิทธิ์ หรือ authorities กำหนดเป็น empty ไปก่อน authorities(Collections.emptyList())

4. เขียน Controller

@RestController
public class HomeController {

    @GetMapping({"", "/"})
    public Mono<String> hello(Authentication authentication) {
        return Mono.just("Hello => " + authentication.getName());
    }
}

5. Build

cd ไปที่ root ของ project จากนั้น

$ mvn clean install

6. Run

$ mvn spring-boot:run

7. เข้าใช้งาน

เปิด browser แล้วเข้า http://localhost:8080

หลังจากนั้นมันจะเด้งเข้าหน้า login (default จาก Spring Security)

Username/Password สำหรับเข้าใช้งาน

  • username = test
  • password = password