无名阁,只为技术而生。流水不争先,争的是滔滔不绝。

(enablewebsecurity) SpringSecurity中的EnableWebSecurity注解启用Web安全详解 @EnableWebSecurity: Spring Security注解 全网首发(图文详解1)

前沿技术 Micheal 3个月前 (06-24) 44次浏览 已收录 扫描二维码

(enablewebsecurity) SpringSecurity中的EnableWebSecurity注解启用Web安全详解

@EnableWebSecurity 是Spring Security中用于启用Web层面安全性的注解。使用这个注解可以开启Spring Security的安全设置,实际上就是为了引入WebSecurityConfigurer类型的类——这个类中定义了安全的配置信息。

使用方法如下:

  • 创建一个配置类:首先,需要创建一个配置类,这个类需要使用 @EnableWebSecurity 注解来启用Web安全,并且这个类需要继承 WebSecurityConfigurerAdapter
  • 重写配置方法:在这个配置类里面,我们可以重写一些方法,比如 configure(HttpSecurity http),以定义我们的安全策略。
  • 用户详情服务(UserDetailsService):可以自定义一个服务来加载用户特定的数据,在这个服务中,你需要提供用户信息给Spring Security,例如用户名、密码和角色权限。
  • 密码编码器(PasswordEncoder):出于安全考虑,通常不会存储用户的明文密码,而是存储密码的哈希值。因此,在Spring Security中通常会使用密码编码器来处理密码的加密和匹配。
  • 认证与授权:可以通过自定义配置来实现对不同URL的认证和授权。

以下是一个简单的Spring Security配置例子:

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;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.password.PasswordEncoder;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    // 自定义配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            // 定义哪些URL需要被保护、哪些不需要被保护
            .authorizeRequests()
                // 例如,允许所有用户访问"/"和"/home"
                .antMatchers("/", "/home").permitAll()
                // 其他任何请求都需要验证
                .anyRequest().authenticated()
                .and()
            // 定义当需要用户登录时候,转到的登录页面
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    // 在内存中创建一些用户
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            // 使用inMemoryAuthentication加入内存中的用户
            .inMemoryAuthentication()
            .withUser("user").password(passwordEncoder().encode("password")).roles("USER")
            .and()
            .withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN");
    }

    // 设置密码编码器,这里使用BCryptPasswordEncoder
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

在上面的代码中,我们创建了一个名为 WebSecurityConfig 的类,它启用了Web安全,并定义了几个用户以及相关的安全策略。这就是Spring Security的基本设置,你可以根据不同的需求去修改和配置这个配置类。
(spring initializingbean) Spring中InitializingBean的使用详细解析 Spring中的 InitializingBean 接口使用方法 全网首发(图文详解1)
(driver booster pro) IObit Driver Booster Pro v11.3.0.43 中文安装版 附图文教程 IObit Driver Booster Pro中文版快速安装使用指南 全网首发(图文详解1)

喜欢 (0)
[]
分享 (0)
关于作者:
流水不争先,争的是滔滔不绝