资讯详情

SpringSecurity启动器

Spring Security 启动器

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

编写 SpringSecurityConfig 安全配置类

SpringSecurityConfig 安全控制配置类作为安全控制中心, 实现身份认证和授权配置功能:

基于内存存储认证

@Bean public PasswordEncoder passwordEncoder() { 
             // 明文 加密储存随机盐值     return new BCryptPasswordEncoder(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
             // 数据库中存储的密码必须加密,否则会报错:There is no PasswordEncoder mapped for the id "null"     String password = passwordEncoder().encode("123456");     logger.info("加密后存储的密码:"   password);     auth.inMemoryAuthentication().withUser("user1")         .password(password).authorities("ADMIN");
}

HttpForm表单认证

@Override
protected void configure(HttpSecurity http) throws Exception { 
        
    // 表单登录方式
    http.formLogin()
        // 登陆页面地址
        .loginPage("/login/page")
        // 表单提交地址
        .loginProcessingUrl("/login/form")
        // 用户名属性名称
        .usernameParameter("username")
        // 密码属性名称
        .passwordParameter("password")
        .and()
        .authorizeRequests() // 认证请求
        //登陆页面放行
        .antMatchers("/login/page").permitAll()
        //所有访问该应用的http请求都要通过身份认证才可以访问
        .anyRequest().authenticated()
        ; // 注意不要少了分号
}

静态资源放行

@Override
public void configure(WebSecurity web) { 
        
    web.ignoring().antMatchers("/dist/**", "/modules/**", "/plugins/**");
}

SpringSecurity底层认证流程

SpringSecurity采用过滤器链实现认证与授权

请求过滤器链:

  • UsernamePasswordAuthenticationFilter:请求中包括用户和密码则进行认证,认证成功标记认证成功,否则进行下一个认证过滤器。

  • BasicAuthenticationFilter:请求头有basic开头的信息,base64解密后认证,认证成功标记认证成功,否则进入下一认证过滤器。

  • 其他认证过滤器

ExceptionTranslationFilter:捕获异常处理后续处理。

FilterSecurityInterceptor:认证通过后,根据资源权限配置来判断当前请求是否可以访问对应资源

标签: lt固态继电器s216zklt固态继电器s312zklt固态继电器s303zl

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台