第一步、在项目pom.xml文件中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
第二步、添加配置测试SpringSecurityConfig文件
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends
WebSecurityConfigurerAdapter {
@Override
protected
void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.logout().permitAll()
.and()
.formLogin();
http.csrf().disable();
}
@Override
public
void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/js/**", "/css/**",
"/images/**");
}
}
第三步:添加接口方法:
@GetMapping("/")
public String index(Long id){
return"欢迎使用!";
}
第四步、增加内存账号配置到配置类MyPasswordEncoder
第五步、访问http://localhost:8080/getList ,输入账号admin,密码123456:
成功