目录
spring-security基础 配置用户名密码
适用场景
配置基础的用户 密码,而非通过数据库等来动态校验,一般来说适用于场景简单,比如给eureka增加个基于base的验证;
基础项目搭建
基于如下配置文件搭建maven项目,启动springboot 项目即完成springboot +spring security 的结合;
pom文件参考:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring-security-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
</project>
启动项目,观察日志中内容:
如上日志中,会将临时密码打印;
默认配置说明(动态密码)
- 引入spring security 时,默认开启安全模块,在不进行任何配置的时候,默认拦截所有请求;
- 默认用户名:user,每次启动密码会自动生成,可在日志中获取。
- 默认登录页:
通过配置文件配置用户名密码
修改项目配置文件,如:application.yml,增加如下配置:
spring:
security:
user:
name: admin
password: admin
重新启动项目,此时控制台不再显示密码,使用配文件中用户名密码以完成登录
*** 说明:***
此场景适用于比如eureka等需要临时鉴权的场景,比如生产环境需要对eureka等项目加密码控制,可通过引入security模块,加配置(前提时配置文件不被公开)的方法。当然还需要额外配置,简单理下思路如下
-
- 配置用户名密码及 开启Http basic认证(security 5.x之后无需配置)
参考配置如下:
spring:
security:
# 开启basic认证方式(已废弃,配置user信息后可以不设置,默认开启)
basic:
enibled: true
# 设置认证信息
user:
password: eureka
name: eureka
-
- 配置鉴权内容
参考代码如下:
- 配置鉴权内容
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers().frameOptions().disable();
// 关闭csrf、 设置鉴权(/actuator/** 开头的不需要认证,其他均需要) httpBasic() 开启basic 认证,默认可不加
http.csrf().disable().authorizeRequests().antMatchers("/actuator/**").permitAll().anyRequest().authenticated().and().httpBasic();
// 关闭basic 认证示例代码:
// http.authorizeRequests().anyRequest().authenticated().and().httpBasic().disable();
}
}
-
- 微服务注册eureka配置文件示例
对于需要注册到eureka的微服务来说,只需要修改eureka地址,即basic方式认证即可
参考配置如下:
- 微服务注册eureka配置文件示例
eureka:
client:
serviceUrl:
# basic 方式认证url: 协议://用户名:密码@ip:端口/资源地址
defaultZone: http://eureka:eureka@localhost:8000/eureka/
通过配置类配置用户名密码
此中场景场景较少,适用于配置文件非明文场景下的代码实现。
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// 关闭basic 认证
// @Override
// protected void configure(HttpSecurity http) throws Exception {
// // 关闭basic认证
// // http.authorizeRequests().anyRequest().authenticated().and().httpBasic().disable();
// }
// 基于配置文件的认证
@Override
protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
// 密码加密
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
String password = bCryptPasswordEncoder.encode("admin1");
// 配置认证信息
authenticationManagerBuilder.inMemoryAuthentication().passwordEncoder(bCryptPasswordEncoder).withUser("admin").password(password).roles("roleA");
}
}
总结
以上三种认证信息设置的适用场景都较为有限,可作为快速实现方案,应注意区分适用场景。
以上内容源码位置:https://gitee.com/master336/spring-security-demo/tree/basic