Home > AI > Backend > SpringBoot > spring-boot-starter-security >

Spring Security 1 – Intro

When you add Spring Security to your project, you will have

The simple taste of Spring Security

Step 1: install dependency

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

Step 2: config inMemory username and password

Method 1: simply config the application.properties

(not working!!)

spring.security.user.name = user # Default user name.
spring.security.user.password = pass # Password
spring.security.user.role = # A comma separated list of roles

Method 2: use WebSecurityConfigurerAdapter

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("user")
                .password(passwordEncoder().encode("pass"))
                .roles("USER");
    }
}

Leave a Reply