SpringSecurity(安全框架)

SpringSecurity(安全)

shiro,SpringSecurity;常用的两个安全框架;包括认证、授权,拦截器和过滤器也可以完成框架的功能,但它使用大量的原生代码,比较繁琐复杂,所以安全框架应运而生。

相对于 Shiro,在 SSM/SSH 中整合 Spring Security 都是比较麻烦的操作,所以,Spring Security 虽然功能比 Shiro 强大,但是使用反而没有 Shiro 多(Shiro 虽然功能没有 Spring Security 多,但是对于大部分项目而言,Shiro 也够用了)。

自从有了 Spring Boot 之后,Spring Boot 对于 Spring Security 提供了 自动化配置方案,可以零配置使用 Spring Security。

记住几个类:

  • WebSecurityConfigurerAdapter:自定义Security策略

  • AuthenticationManagerBuilder:自定义认证策略

  • @EnableWebSecurity:开启WebSecurity模式

Spring Secuity的两个主要目标是“认证”和“授权”(访问控制)。

“认证”(Authentication)

"授权"(Authorization)

Spring Secuity官方文档

1.引入依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2.配置文件

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
   //授权
   @Override
   protected void configure(HttpSecurity http) throws Exception {
       //首页所有人可以访问,功能也只有对应有权限的人才能访问

       http.authorizeRequests()
              .antMatchers("/").permitAll() //首页所有人可以访问
              .antMatchers("/level1/**").hasRole("vip1")  //level1下的所有页面只有vip1的角色可以访问
              .antMatchers("/level2/**").hasRole("vip2")   //level2下的所有页面只有vip2的角色可以访问
              .antMatchers("/level3/**").hasRole("vip3");  //level3下的所有页面只有vip3的角色可以访问


      // 没有权限默认会到登录页面,需要开启登录的页面
     
//定制首页
    http.formLogin().loginPage("/toLogin");

        //防止网站攻击,csrf:跨站请求伪造,
       http.csrf().disable();//springboot默认是开启的,需要把它给手动关掉;   登出logout失败可能的原因

       //注销,开启注销功能 回到首页
       http.logout().logoutSuccessUrl("/");
    //记住我
    http.rememberMe().rememberMeParameter("remember");

  }

   //认证,在springboot 2.1.x版本中可以直接使用;在一些新版本中可能会报500错误,提示你密码没有加密
   //密码编码: PasswordEmcoder
   //在Spring Security 5.0+新增了很多的加密方法
   @Override
   protected void configure(AuthenticationManagerBuilder auth) throws Exception {

       //这些数据正常应该从数据库中读,此处是从内存中读
       //密码未进行加密
//       auth.inMemoryAuthentication()
//               .withUser("aaa").password("123456").roles("vip1","vip2")
//               .and()       //and()拼接多个用户
//               .withUser("bbb").password("123").roles("vip3")
//               .and()
//               .withUser("ccc").password("111").roles("vip1");

       //对密码进行加密
       auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
              .withUser("aaa").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2")
              .and()       //and()拼接多个用户
              .withUser("bbb").password(new BCryptPasswordEncoder().encode("123456")).roles("vip3")
              .and()
              .withUser("ccc").password(new BCryptPasswordEncoder().encode("111")).roles("vip1");
  }
}

从数据库中读取数据进行认证详见Spring Secuity官方文档 5.6.2 JDBC Authentication

注销与权限控制

可以根据用户的角色实现动态菜单的功能,不同角色用户登录显示的内容不一样

传送门

原文地址:https://www.cnblogs.com/bxbo/p/13503853.html