SpringSecurity6从入门到实战之SpringSecurity6自定义认证规则

SpringSecurity6从入门到实战之SpringSecurity6自定义认证规则

Spring Security 中默认所有的 http 请求都需要先认证通过后,才能访问。那么, 如何指定不需要认证就可以直接访问的资源呢?比如 用户的登录页面和注册页面,都是不需要认证就必须能被直接访问的。这就需要设置自定义的URL认证规则

SpringSecurity5.x自定义认证与6.x

# 在 SpringSecurity5.x中( 了解,已被废弃 )
    // 自定义配置类 继承 WebSecurityConfigurerAdapter 类覆盖 configure() 方法
    @Configuration
    public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
       @Override
       protected void configure(HttpSecurity http) throws Exception {
           http.authorizeHttpRequests()
                .mvcMatchers("/hello").permitAll()
                .anyRequest().authenticated()
                .and().formLogin();
       }
    }
# 在 SpringSecurity6.x 中
    // 自定义配置类 使用注解 @EnableWebSecurity 配置 SpringSecurity

开发示例

创建一个新的controller

/test

package com.example.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
        System.out.println("/hello");
        return "hello...";
    }

    @RequestMapping("/test")
    public String test() {
        System.out.println("/test");
        return "test...";
    }
}

方便与/hello对比进行测试

根据SpringSecurity6.x自定义认证规则配置

新建MyWeSecurityConfig自定义配置类

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class MyWeSecurityConfig {
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        //放行hello和test
        http.authorizeHttpRequests().requestMatchers("/hello", "test").permitAll()
            //所有请求都需要进行认证
                .anyRequest().authenticated()
            //进行表单登录验证
            .and().formLogin();
        return http.build();

    }
}

测试

image.png

image.png

结论

最终可以发现我们可以自定义认证规则,让例如注册等不需要认证的请求直接放行,让其他请求进行认证操作后再进行放行

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-06-15 16:08:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-15 16:08:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-15 16:08:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-15 16:08:03       20 阅读

热门阅读

  1. AcWing算法基础课笔记——质数

    2024-06-15 16:08:03       6 阅读
  2. 彻底卸载SQL Server,步骤

    2024-06-15 16:08:03       7 阅读
  3. 英语啐啐念-(三)

    2024-06-15 16:08:03       5 阅读
  4. js笛卡尔积数组整理实现

    2024-06-15 16:08:03       7 阅读
  5. 深入探索 Spring Boot 自定义启动画面

    2024-06-15 16:08:03       5 阅读
  6. 解析xml文件获得元素并修改最后保存

    2024-06-15 16:08:03       4 阅读
  7. C++ 字符串处理5-手机号邮箱如何脱敏处理

    2024-06-15 16:08:03       8 阅读
  8. MongoDB 正则表达式

    2024-06-15 16:08:03       7 阅读
  9. C#中数组ProtoBuf使用问题

    2024-06-15 16:08:03       6 阅读