📜  Spring Security - 自定义登录

📅  最后修改于: 2022-05-13 01:54:24.558000             🧑  作者: Mango

Spring Security - 自定义登录

Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架。它是保护基于 Spring 的应用程序的事实标准。 Spring Security 是一个专注于为Java应用程序提供身份验证和授权的框架。与所有 Spring 项目一样,Spring Security 的真正强大之处在于它可以轻松扩展以满足自定义需求。 Spring Security 的一些关键特性是:

  • 对身份验证和授权的全面且可扩展的支持
  • 防止会话固定、点击劫持、跨站点请求伪造等攻击
  • Servlet API 集成
  • 与 Spring Web MVC 的可选集成。

SpringSecurity 使用默认用户名和密码提供了自己的登录配置,但我们可以通过创建自定义登录配置来覆盖 SpringSecurity 的默认配置。我们可以添加多个用户,也可以允许他们不同的角色。这样我们就可以轻松地对安全 API 执行授权。让我们讨论如何在 Spring Security 中创建自定义登录。

分步实施

第 1 步:使用 https://start.spring.io/ 创建 Spring Boot 项目

Project: Maven
Language: Java
Spring Boot: 2.2.8
Packaging: JAR
Java: 8
Dependencies: Spring Web,Spring Security

第 2 步:单击 Generate 将下载启动项目。

第三步:解压压缩包。现在打开一个合适的 IDE,然后转到 File > New > Project from existing sources > Spring-boot-app 并选择 pom.xml。点击提示导入更改,等待项目同步,如下图所示:

第 4 步:现在转到 src > main > Java > com.gfg.Spring.boot.app 并创建两个Java文件,一个是控制器。 Java ,另一个是配置。Java

控制器。Java

Java
@RestController
public class controller {
  
    @GetMapping("/delete") public String delete()
    {
        return "This is the delete request";
    }
}


Java
@EnableWebSecurity
public class config extends WebSecurityConfigurerAdapter {
  
    // Adding the roles
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("Zack")
                .password("aayush")
                .roles("admin_role")
                .and()
                .withUser("Aayush")
                .password("Saini")
                .roles("student_role");
    }
    
    // Configuring the api 
      // according to the roles.
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.
                httpBasic()
                .and()
                .authorizeRequests()
                .antMatchers("/delete").hasRole("admin_role")
                .and()
                .formLogin();
    }
    
      // Function to encode the password
      // assign to the particular roles.
    @Bean
    public PasswordEncoder getPasswordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }
}


上面的Java文件用于设置控制器以处理来自客户端的传入请求。现在我们必须配置我们将使用配置的请求。 Java文件。

配置。 Java: 此配置文件用于在 Spring 项目中创建自定义安全性。

Java

@EnableWebSecurity
public class config extends WebSecurityConfigurerAdapter {
  
    // Adding the roles
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("Zack")
                .password("aayush")
                .roles("admin_role")
                .and()
                .withUser("Aayush")
                .password("Saini")
                .roles("student_role");
    }
    
    // Configuring the api 
      // according to the roles.
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.
                httpBasic()
                .and()
                .authorizeRequests()
                .antMatchers("/delete").hasRole("admin_role")
                .and()
                .formLogin();
    }
    
      // Function to encode the password
      // assign to the particular roles.
    @Bean
    public PasswordEncoder getPasswordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }
}

WebSecurityConfigureAdapter 类用于配置传入的请求,主要有两种方法用于配置。第一种方法用于为spring应用服务器添加角色,另一种方法用于根据角色区分请求。现在运行spring应用程序的主应用程序

在 Postman 中测试 API。转到邮递员并输入localhost:8080/delete

使用管理员角色:

输出:

使用学生角色:

输出:

这样我们就可以在 Spring Application 中创建自定义登录。