Spring Security

概述

SpringSecurity 两个核心功能:

  • 用户认证(Authentication):系统确认用户是否登录。

  • 用户授权(Authorization):系统判断用户是否有权限去做某些事。

SpringSecurity 本质是一个过滤链

  • FilterSecurityInterceptor:是一个方法级的权限过滤器,位于过滤链的最底部。

  • ExceptionTranslationFilter:是一个异常过滤器,用来处理在认证授权过程中抛出的异常。

  • UsernamePasswordAuthenticationFilter:对 login 的 POST 请求做拦截,校验表单中的用户名和密码。

项目启动

mvn 依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.5</version>
    </parent>    

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</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-test</artifactId>
            <scope>test</scope>
        </dependency>

Controller

@RestController
@RequestMapping("/test")
public class TestController {
   
   

    @GetMapping("hello")
    public String hello() {
   
   
        return "hello security";
    }
}

想要访问接口需要登录认证

  • 用户名:user
  • 密码:

登录成功后,才能访问到密码

设置登录密码

  1. 通过配置文件
  2. 通过配置类
  3. 自定义编写实现类

配置文件

spring.security.user.name=dyf
spring.security.user.password=dyf

配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
   
   
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
   
   
        // 对密码加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = passwordEncoder.encode("dzh");

        auth.inMemoryAuthentication().withUser("dzh").password(password).roles("admin");
    }

    @Bean
    PasswordEncoder password() {
   
   
        return new BCryptPasswordEncoder();
    }
}

自定义编写实现类

UserDetailsService 接口:查询数据库中用户名和密码。

自己实现 UserDetailsService 接口,后续需要从数据库中查询用户信息做校验,也是在此处处理。

@Service("userDetailService")
public class MyUserDetailsService implements UserDetailsService {
   
   
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
   
   
        List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("role");
        return new User("mary", new BCryptPasswordEncoder().encode("123"), auths);
    }
}

将 MyUserDetailsService 注入到 Spring-Security 框架

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
   
   
    @Autowired
    private UserDetailsService userDetailService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
   
   
        auth.userDetailsService(userDetailService).passwordEncoder(password());
    }

    @Bean
    PasswordEncoder password() {
   
   
        return new BCryptPasswordEncoder();
    }
}

数据库

引入数据库 mvn 依赖

    <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值