springboot 2.7版本后的Spring security 如何进行配置
时间: 2024-03-24 19:37:11 浏览: 201
很抱歉,Spring Security目前最新的版本是5.x系列,并没有2.7版本。如果您想使用Spring Boot 2.x版本的Spring Security进行配置,可以按照下面的步骤进行:
1.在pom.xml文件中添加Spring Security的依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
```
2.创建一个继承自WebSecurityConfigurerAdapter的配置类,并覆盖configure方法进行配置。示例代码如下:
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/public/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("{noop}password").roles("USER");
}
}
```
上述代码中,我们通过configure方法配置了安全规则,使得/public/**的请求不需要认证,其他请求需要认证。我们还配置了一个简单的内存认证,其中用户的用户名为user,密码为password。
3.在application.properties或application.yml文件中配置登录页面的路径。示例代码如下:
```yml
spring:
security:
login:
page: /login
```
这样,当未认证的用户访问需要认证的资源时,系统会自动跳转到登录页面。
需要注意的是,以上只是一个简单的示例,在实际应用中,您可能需要更加复杂的安全规则,也可以使用数据库或LDAP等方式进行认证。您可以参考Spring Security的官方文档来进行更深入的学习和理解。
阅读全文
相关推荐















