SpringBoot整合SpringSecurity

161 阅读2分钟

SpringSecurity(安全)

在web开发中,安全第一位,过滤器,拦截器-----

不是功能性需求,而是一种安全机制.

做网站:安全问题应该在设计之初就应该考量!

  • 漏洞:隐私漏洞~
  • 如何搭建架构~

shiro springSecurity: 很相似,除了类和名字不一样.

认证,授权(v1,role1......)

  • 功能权限
  • 访问权限
  • 菜单权限
  • 拦截器,过滤器: 大量的原生代码 过于冗余了......

简介:

Spring Security是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入spring-boot-starter-security模块,进行少量的配置,即可实现强大的安全管理! 记住几个类:

  • WebSecurityConfigurerAdapter:自定义Security策略·

  • AuthenticationManagerBuilder:自定义认证策略·

  • @EnableWebSecurity:开启WebSecurity模式 Spring Security的两个主要目标是“认证"和“授权”(访问控制)。

    • “认证”(Authentication)
    • “授权”(Authorization)

    这个概念是通用的,而不是只在Spring Security中存在。

参考官网:spring.io/projects/sp…

用户验证和授权

创建配置类,搭建框架

继承WebSecurityConfigurerAdapter类

package com.springsecurity.config;
​
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;
/**
 * @ClassName: SecurityConfig
 * @Package: com.springsecurity
 * @CreateBy: 2022/12/11 - 14:06
 * @Version: V1.0
 * @Author: KK
 *//**
 * 加注解 @EnableWebSecurity托管整个配置类
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
​
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
    }
}

用户认证和授权

认证

重写configure(AuthenticationManagerBuilder auth) 方法,

授权

重写configure(HttpSecurity http) 方法

功能:

  • 登录

  • 注销及权限控制

    package com.springsecurity.config;
    ​
    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;
    ​
    /**
     * @ClassName: SecurityConfig
     * @Package: com.springsecurity
     * @CreateBy: 2022/12/11 - 14:06
     * @Version: V1.0
     * @Author: KK
     *//**
     * 加注解 @EnableWebSecurity托管整个配置类
     */
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    ​
        /**
         * 授权
         * @param http
         * @throws Exception
         */
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // 首页所有人可以访问 功能页只有对应有权限的人才能访问
            // 请求授权的规则~
            http.authorizeRequests().antMatchers("/").permitAll()
                    // function1只对vip1用户开放;function2只对vip2开放
                    .antMatchers("/function1/**").hasRole("vip1")
                    .antMatchers("/function2/**").hasRole("vip2");
    ​
            // 登录页面(login),首页不设置权限,需要开启登录的页面
            http.formLogin();
    //        // 设置自定义页面    表单默认为username password(源码) 如果要自定义,则要先申明
    //        http.formLogin().loginPage("page")
    //                .usernameParameter("user").passwordParameter("pwd")
    //                .loginProcessingUrl("跳转页");
    ​
            // 注销
            http.logout();
    ​
            // 关闭跨站脚本攻击
            http.csrf().disable();
    ​
            // 开启记住我功能 COOKIE  默认保存两周; 自定义接收前端参数
            http.rememberMe().rememberMeParameter("remember");
        }
    ​
        /**
         * 认证   springboot 2.1.x 可以直接使用
         * 密码编码:PassWordEncoder
         * 在Spring Security 5.0+ 新增了很多加密方式;没有编码会报服务器异常
         *      There is no PasswordEncoder mapped for the id "null" at org.springframework.security.crypto
         * @param auth
         * @throws Exception
         */
        @Override
        public void configure(AuthenticationManagerBuilder auth) throws Exception {
            // 这些数据应该从数据库读取 这里仅仅是为了学习语法
            auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                    .withUser("kk").password(new BCryptPasswordEncoder().encode("123")).roles("vip1")
                    .and()
                    .withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip2");
        }
    }