springboot 整合 spring security,实现用户鉴权

简介: springboot 整合 spring security,实现用户鉴权

在 Spring Boot 项目中整合 Spring Security 实现用户鉴权涉及多个步骤,包括依赖配置、配置类创建、安全配置、用户详情服务的实现等。下面是一个详细的步骤和关键代码示例,帮助你理解如何在 Spring Boot 中整合 Spring Security。

1. 添加依赖

首先,在你的 pom.xml 文件中添加 Spring Security 相关的依赖。Spring Boot 的 Spring Security Starter 可以帮助你轻松集成 Spring Security。

<dependencies>
    <!-- Spring Boot Starter Security -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    
    <!-- Spring Boot Starter Web, 如果你的项目还没有的话 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- 数据库相关依赖,如果使用数据库存储用户信息 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    <!-- H2 Database (可选,用于测试或开发环境) -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

2. 配置 Spring Security

Spring Security 的配置主要包括创建一个配置类 SecurityConfig,继承 WebSecurityConfigurerAdapter。在这个类中,你可以配置安全设置,如认证、授权等。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
 
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/public/**").permitAll() // 公开访问路径
                .antMatchers("/admin/**").hasRole("ADMIN") // 只有 ADMIN 角色可以访问
                .anyRequest().authenticated() // 其他所有请求都需要认证
                .and()
            .formLogin() // 表单登录
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }
 
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user")
                .password(passwordEncoder().encode("password"))
                .roles("USER")
                .and()
                .withUser("admin")
                .password(passwordEncoder().encode("admin"))
                .roles("USER", "ADMIN");
    }
 
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

在上面的代码中:


configure(HttpSecurity http) 方法定义了哪些路径是公开的,哪些路径需要特定角色的用户才能访问。还定义了登录页面和登出功能。

configure(AuthenticationManagerBuilder auth) 方法定义了用户的认证方式,这里我们使用了内存中的用户和角色。实际项目中你可能会用数据库来存储用户信息。

passwordEncoder() 方法提供了密码加密的方式,使用 BCryptPasswordEncoder 是推荐的做法。

3. 自定义用户详细信息服务

在实际项目中,你可能需要从数据库加载用户信息。你需要实现 UserDetailsService 接口,并提供自定义的用户加载逻辑。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
 
@Service
public class CustomUserDetailsService implements UserDetailsService {
 
    @Autowired
    private UserRepository userRepository; // 假设你有一个 UserRepository 访问用户数据
 
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserEntity user = userRepository.findByUsername(username)
                .orElseThrow(() -> new UsernameNotFoundException("User not found"));
        
        return org.springframework.security.core.userdetails.User.builder()
                .username(user.getUsername())
                .password(user.getPassword())
                .roles(user.getRoles().toArray(new String[0])) // 将用户角色转换为字符串数组
                .build();
    }
}

你需要有一个 UserRepository 访问数据库中的用户数据以及一个 UserEntity 实体类来表示用户数据。

4. 实体类和存储库

定义一个 UserEntity 类来表示用户信息:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ElementCollection;
import java.util.Set;
 
@Entity
public class UserEntity {
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
 
    private String username;
    private String password;
 
    @ElementCollection
    private Set<String> roles;
 
    // Getters and Setters
}

定义一个 UserRepository 接口:

import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
 
public interface UserRepository extends JpaRepository<UserEntity, Long> {
    Optional<UserEntity> findByUsername(String username);
}

5. 配置登录和登出页面

创建自定义登录页面 /src/main/resources/templates/login.html

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h2>Login Page</h2>
    <form action="/login" method="post">
        <div>
            <label for="username">Username:</label>
            <input type="text" id="username" name="username" />
        </div>
        <div>
            <label for="password">Password:</label>
            <input type="password" id="password" name="password" />
        </div>
        <div>
            <button type="submit">Login</button>
        </div>
    </form>
</body>
</html>

创建登出页面 /src/main/resources/templates/logout.html(可选):

<!DOCTYPE html>
<html>
<head>
    <title>Logout</title>
</head>
<body>
    <h2>You have been logged out</h2>
    <a href="/login">Login Again</a>
</body>
</html>

6. 测试应用

启动你的 Spring Boot 应用,访问 /login 来查看登录页面。使用配置中的用户名和密码进行登录(如 userpasswordadminadmin),并测试不同角色和权限的访问控制。

总结

以上内容提供了一个 Spring Boot 项目中整合 Spring Security 的基本流程和关键代码。实际上,你可能还需要根据项目的具体需求进行更多的自定义配置,如处理不同的认证方式、集成 OAuth2、JWT 等。这个基础的示例应该能帮助你入门 Spring Security 的集成和配置。


相关文章
|
1月前
|
缓存 NoSQL Java
SpringBoot的三种缓存技术(Spring Cache、Layering Cache 框架、Alibaba JetCache 框架)
Spring Cache 是 Spring 提供的简易缓存方案,支持本地与 Redis 缓存。通过添加 `spring-boot-starter-data-redis` 和 `spring-boot-starter-cache` 依赖,并使用 `@EnableCaching` 开启缓存功能。JetCache 由阿里开源,功能更丰富,支持多级缓存和异步 API,通过引入 `jetcache-starter-redis` 依赖并配置 YAML 文件启用。Layering Cache 则提供分层缓存机制,需引入 `layering-cache-starter` 依赖并使用特定注解实现缓存逻辑。
169 1
SpringBoot的三种缓存技术(Spring Cache、Layering Cache 框架、Alibaba JetCache 框架)
|
24天前
|
Java 微服务 Spring
SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
文章介绍了如何利用Spring Cloud Alibaba快速构建大型电商系统的分布式微服务,包括服务限流降级等主要功能的实现,并通过注解和配置简化了Spring Cloud应用的接入和搭建过程。
SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
|
28天前
|
NoSQL 关系型数据库 MySQL
SpringBoot 集成 SpringSecurity + MySQL + JWT 附源码,废话不多直接盘
SpringBoot 集成 SpringSecurity + MySQL + JWT 附源码,废话不多直接盘
69 2
|
27天前
|
安全 Java 数据安全/隐私保护
基于SpringBoot+Spring Security+Jpa的校园图书管理系统
本文介绍了一个基于SpringBoot、Spring Security和JPA开发的校园图书管理系统,包括系统的核心控制器`LoginController`的代码实现,该控制器处理用户登录、注销、密码更新、角色管理等功能,并提供了系统初始化测试数据的方法。
30 0
基于SpringBoot+Spring Security+Jpa的校园图书管理系统
|
30天前
|
JSON 安全 Java
|
2月前
|
Java Spring 容器
Spring Boot 启动源码解析结合Spring Bean生命周期分析
Spring Boot 启动源码解析结合Spring Bean生命周期分析
78 11
|
18天前
|
Java Spring
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
|
28天前
|
安全 Java 关系型数据库
SpringBoot SpringSecurity 介绍(基于内存的验证)
SpringBoot SpringSecurity 介绍(基于内存的验证)
29 0
|
29天前
|
前端开发 Java Spring
Java 新手入门:Spring Boot 轻松整合 Spring 和 Spring MVC!
Java 新手入门:Spring Boot 轻松整合 Spring 和 Spring MVC!
41 0
|
29天前
|
NoSQL 安全 Java
Java Spring Boot中使用Shiro、JWT和Redis实现用户登录鉴权
Java Spring Boot中使用Shiro、JWT和Redis实现用户登录鉴权