Spring Security

Spring Security

SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。

为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

其核心就是一组过滤器链,项目启动后将会自动配置。

最核心的就是 Basic Authentication Filter 用来认证用户的身份,一个在spring security中一种过滤器处理一种认证方式。

几个重要的类

  • webSecurityConfiguration : 自定义 security 策略
  • AuthenticationManagerBuilder : 自定义认证策略
  • @EnableWebSecurity : 开启 WebSecurity 模式

测试

1.导入依赖

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

基本框架

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
   

    @Override
    protected void configure(HttpSecurity http) throws Exception {
   
        super.configure(http);
    }
}

2.根据权限认证

@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();
}
//认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
   
    //jdbcAuthentication(),在数据库中认证
    //inMemoryAuthentication()内存中认证
    //passwordEncoder(new BCryptPasswordEncoder())给密码加密
    auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
            .withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip3");
}

如果注销404了,就是因为它默认防止csrf跨站请求伪造,因为会产生安全问题,我们可以将请求改为post表单提交,或者在spring security中关闭csrf功能;我们试试:在 配置中增加 http.csrf().disable();

http.csrf().disable();//关闭csrf功能:跨站请求伪造,默认只能通过post方式提交logout请求
http.logout().logoutSuccessUrl("/");
//没有权限默认到登录页
http.formLogin().loginProcessingUrl("/login");
//注销
http.logout().logoutSuccessUrl("/login");

动态权限控制

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
sec:authorize="hasRole('vip3')"

/www.thymeleaf.org/thymeleaf-extras-springsecurity5"


sec:authorize=“hasRole(‘vip3’)”


相关推荐

  1. SpringSecurity

    2023-12-28 15:50:04       35 阅读
  2. SpringSecurity】2. 初学SpringSecurity

    2023-12-28 15:50:04       52 阅读
  3. SpringBoot-SpringSecurity

    2023-12-28 15:50:04       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-28 15:50:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-28 15:50:04       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-28 15:50:04       87 阅读
  4. Python语言-面向对象

    2023-12-28 15:50:04       96 阅读

热门阅读

  1. 数组和字符串

    2023-12-28 15:50:04       45 阅读
  2. 学习中的零碎的记录

    2023-12-28 15:50:04       61 阅读
  3. EsayExcel读取合并单元格

    2023-12-28 15:50:04       57 阅读
  4. EMR集群迁移自建Hadoop(元数据及HDFS数据)

    2023-12-28 15:50:04       52 阅读
  5. 2022年12月10日-2023年12月28日

    2023-12-28 15:50:04       60 阅读
  6. Hadoop集成对象存储和HDFS磁盘文件存储

    2023-12-28 15:50:04       58 阅读
  7. WPF 显示gif动态图

    2023-12-28 15:50:04       66 阅读