Spring系列之——spring security

1 搭建springboot


2 配置pom依赖(springboot版本为2.1.3)

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

3 写一个controller类


4 SpringBootApplication中增加注解ComponentScan,并启动

Spring系列之——spring security


5 启动测试 http://localhost:8080/index

5.1 开启验证(默认生效),需要输入用户名和密码

  springsecurity默认用户名为user,密码为运行日志中“Using generated security password”,如果需要支持其他用户需要重写抽象类WebSecurityConfigurerAdapter中的configure方法。

Spring系列之——spring security

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);
}
}

Spring系列之——spring security

上一篇:(六)SpringBoot2.0基础篇- Redis整合(JedisCluster集群连接)


下一篇:在Eclipse中使用JUnit4进行单元測试(0基础篇)