1 搭建springboot 2 配置pom依賴(springboot版本為2.1.3) 3 寫一個controller類 4 SpringBootApplication中增加註解ComponentScan,並啟動 5 啟動測試 http://localhost:8080/index 5.1 開啟驗 ...
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); } }View Code