SpringSecurity

1、项目依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    <version>2.7.14</version>
</dependency>
<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt-api</artifactId>
    <version>0.12.3</version>
</dependency>
<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt-impl</artifactId>
    <version>0.12.3</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt-jackson</artifactId>
    <version>0.12.3</version>
    <scope>runtime</scope>
</dependency>

2、修改Security
(1)编写oep.serviceImpl.UserDetailsServiceImpl类,实现自UserDetailsService接口,用来接入数据库信息。

(2)编写oep.config.SecurityConfig类,用来实现用户密码的加密存储。

@Configuration
@EnableWebSecurity
public class SecurityConfig {
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

(3)创建jwt工具类oep.utils.JwtUtil类,用来创建、解析jwt token
(4)配置oep.config.filter.JwtAuthenticationTokenFilter,用来验证jwt token,如果验证成功,则将User信息注入上下文中
(5)创建oep.config.SecurityConfig类,放行登录、注册等接口

posted @ 2024-02-09 12:57  惊朝  阅读(14)  评论(0编辑  收藏  举报