2 配置pom依赖(springboot版本为2.1.3)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3 写一个controller类
4 SpringBootApplication中增加注解ComponentScan,并启动
5 启动测试 http://localhost:8080/index
5.1 开启验证(默认生效),需要输入用户名和密码
springsecurity默认用户名为user,密码为运行日志中“Using generated security password”,如果需要支持其他用户需要重写抽象类WebSecurityConfigurerAdapter中的configure方法。
5.2 关闭验证
在启动类中排除SecurityAutoConfiguration
package com.gbm.myspringboot; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.ComponentScan; @SpringBootApplication(exclude = SecurityAutoConfiguration.class)
@ComponentScan(basePackages = {"com.gbm.controller"})
public class MyspingbootApplication { public static void main(String[] args) {
SpringApplication.run(MyspingbootApplication.class, args);
}
}