SpringSecurity(安全)-简单学习

时间:2021-09-10
本文章向大家介绍SpringSecurity(安全)-简单学习,主要包括SpringSecurity(安全)-简单学习使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

安全应该在什么时候考虑? 设计之初

漏洞,隐私泄露
架构一旦确定

spring security 的核心功能主要包括:

认证 (你是谁)
授权 (你能干什么)
攻击防护 (防止伪造身份)

核心就是一组过滤器链,项目启动后将会自动配置。最核心的就是 Basic Authentication Filter 用来认证用户的身份,一个在spring security中一种过滤器处理一种认证方式

简介

SpringSecurity 正是 Spring 家族中的成员。SpringSecurity 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案。
对于安全控制,springboot仅需引入spring-boot,starter-security模块。

WebSecurityConfigurerAdapter: 自定义Security策略
AuthenticationManagerBuilder: 自定义认证策略
@EnableWebSecurity: 开启模式

使用

导包
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
编写一个SecurityConfig类继承WebSecurityConfigurerAdapter类

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页所有人可以访问,功能页只有对应权限的人才可以访问
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");
        //无权限,跳转到登录界面
        http.formLogin();
        //注销功能;
        http.logout();
    }

//    认证
//    密码编码:passwordEncoder
//    spring security 5.0+ 新增了很多加密方法
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        数据应由数据库中去获取
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("huang").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1")
                .and()
                .withUser("yong").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2")
                .and()
                .withUser("gui").password(new BCryptPasswordEncoder().encode("123456")).roles("vip3");
    }
}
编写相关html文件
编写RouterConfig类控制页面跳转
@Controller
public class RouterController {

    @RequestMapping("/index.html")
    public String index(){
        return "index";
    }

    @RequestMapping("/login")
    public String tologin(){
        return "login";
    }

    @RequestMapping("/level1/{id}")
    public String level1(@PathVariable("id") int id){
        return "/level1/"+id;
    }
    @RequestMapping("/level2/{id}")
    public String level2(@PathVariable("id") int id){
        return "/level2/"+id;
    }
    @RequestMapping("/level3/{id}")
    public String level3(@PathVariable("id") int id){
        return "/level3/"+id;
    }
}
相关问题
1、无权限时,跳转到登录页面的URL是什么?
    由源码注释可知URL为 "/login"
    ![](https://img2020.cnblogs.com/blog/2205131/202109/2205131-20210910105903480-236238819.png)
2、注销后的结果?
    使用默认的URL("/logout"),注销后会自动跳转到 "/login" 即登录页面,并使httpsession无效


但是使用了 http.logout().logoutSuccessUrl("/"); 后可以重定向到我们给的URL中

3、认证?
    自动获取username和password,并与我们给的数据进行匹配认证


原文地址:https://www.cnblogs.com/keacua/p/15250616.html