Spring security学习笔记

打印 上一主题 下一主题

主题 965|帖子 965|积分 2905

1. 概要

Spring Security是一个用于在Java应用步伐中实现 身份验证访问控制 的强大框架。它可以轻松地集成到任何基于Spring的应用步伐中,提供了一套丰富的功能来保护应用步伐的安全性。
2. spring security原理


对于servlet三大组件,spring security位于Filter过滤链中,SpringSecurity 本质就是一个过滤器链

   SpringSecurity 接纳的是责任链的设计模式,它有一条很长的过滤器链
  

2.1 DelegatingFilterProxy

Spring 提供了一个 DelegatingFilterProxy 代理类,DelegatingFilterProxy 实现了Filter,因此它可以被注入到 FilterChain(过滤器链)中,同时,当请求到来时,它会把请求转发到Spring容器 中实现了Filter接口的 Bean 实体,DelegatingFilterProxy会从ApplicationContext中获取FilterBean 实体,然后将请求转发给到它,所以 DelegatingFilterProxy 桥接了 Servlet容器 和 Spring容器。
2.2 FilterChainProxy

DelegatingFilterProxy从Spring容器中获取得到的就是FilterChainProxy实体,而FilterChainProxy也是一个代理类,它最终会将请求转发到 Spring Security 提供的 SecurityFilterChain 中,FilterChainProxy就是 Spring Security 真正的入口起始点
2.3 SecurityFilterChain

SecurityFilterChain作用其实跟Servlet的FilterChain一样,同样维护了很多Filters,这些Filters 是由Spring Security提供的,每个 Security Filter 都有不同的职能,比如登录认证、CSRF防御…
2.4 Spring Security 作用机制


  • 注册尺度Filter:起首,会自动注入一个DelegatingFilterProxy到 Servlet 的FilterChain中。
  • 请求转发到 Spring Security:当请求到来时,DelegatingFilterProxy就会自动在 Spring容器 中搜索名称为springSecurityFilterChain的Filter实体,其实际范例为FilterChainProxy。DelegatingFilterProxy最终会将请求转发给到FilterChainProxy。
  • 找到匹配请求处置惩罚的SecurityFilterChain:FilterChainProxy内部维护了一系列SecurityFilterChains,他会依据请求内容找到对应处置惩罚该请求的SecurityFilterChain。
  • 请求处置惩罚:找到能处置惩罚请求的第一个SecurityFilterChain后,就会遍历该SecurityFilterChain内部维护的一系列Filters,依次让这些 Security Filter 处置惩罚该请求,完成认证、授权等功能。
3.Spring Security快速入门



  • 步骤1:添加Spring Security依靠
    在你的Spring Boot项目的 pom.xml文件中,添加Spring Security的依靠:
    1. <dependency>
    2.     <groupId>org.springframework.boot</groupId>
    3.     <artifactId>spring-boot-starter-security</artifactId>
    4. </dependency>
    复制代码
  • 步骤2:写一个简单地hello接口:

  • 步骤3:欣赏器访问接口

           默认的用户名是 user ,默认的登录暗码 在每次启动项目时随机天生
       

    从项目启动日志中可以看到默认的登录暗码,登录乐成后,就可以访问hello 接口了。
  • 步骤4:自己配置账户名或者暗码

  • 步骤5:创建Spring Security配置类
    创建一个继承自 WebSecurityConfigurerAdapter的配置类,并重写 configure() 方法来配置Spring Security的举动。
    1. @Configuration
    2. @EnableWebSecurity
    3. public class SecurityConfig extends WebSecurityConfigurerAdapter {
    4.     @Override
    5.     protected void configure(HttpSecurity http) throws Exception {
    6.         http
    7.             .authorizeRequests()  //这开始了一个授权请求的配置链。.authorizeRequests() 方法允许你指定哪些 URL 需要认证和授权。
    8.                 .antMatchers("/", "/home").permitAll() // 方法指定了两个 URL 模式,/ 和 /home。.permitAll() 表示这些 URL 可以被任何用户无条件访问,即使他们没有登录。
    9.                 .anyRequest().authenticated() //.anyRequest() 表示所有未被前面规则明确指定的请求。.authenticated() 指定这些请求需要经过身份验证才能访问。
    10.                
    11.                 .and() //.and() 方法用于继续下一个配置步骤。在这里,它用于从授权请求配置切换到其他配置,如表单登录配置。
    12.             
    13.             .formLogin() //.formLogin() 配置了表单登录功能
    14.                 //.loginPage("/login") 指定了登录页面的 URL 注意:配置了.loginPage("/login") ,你必须写这个页面,如果不配置,就用Spring Security 内置的Form登录页面
    15.                 .permitAll()   // 表示这个登录页面可以被任何人访问,不需要身份验证。
    16.                
    17.                 .and()  //同样地,.and() 用于继续到下一个配置步骤,这里是登出配置。
    18.             .logout()   //.logout() 开始配置登出功能。
    19.                 .logoutUrl("/logout")  //设置了处理登出请求的 URL
    20.                 .logoutSuccessUrl("/login") //指定了登出成功后用户会被重定向到的 URL
    21.                 .invalidateHttpSession(true) //表示在登出时会销毁 HttpSession。
    22.                 .deleteCookies("JSESSIONID");   //用于删除 JSESSIONID cookie,以完全清除用户的会话
    23.     }
    24. }
    复制代码
4.高级自定义配置

Spring Security抽象类 WebSecurityConfigAdapter—— 开发者通过继承它就能 得到Spring Security 默认的安全功能。 也可以通过覆盖它提供的方法来自定义自 的安全拦截方案。

  • WebSecurityConfigAdapter 中默认存在的方法
    1. /**
    2. *在此方法中,可以配置认证管理器(AuthenticationManager),包括定义用户、密码、角色等。例如,可以连接到数据库或LDAP来加载用户信*息,也可以配置内存中的用户数据等等.
    3. */
    4. protected void configure(AuthenticationManagerBuilder auth );
    5. /**
    6. *这个方法允许配置不受Spring Security过滤器链保护的资源,比如静态资源、视图解析器资源等。
    7. */
    8. public void configure(WebSecurity web) ;
    9. /**
    10. *configure(HttpSecurity http): 开发者通过重写这个方法来配置HTTP安全相关的所有细节。在这里,可以指定哪些URL需要身份验证、哪些
    11. *可以匿名访问,还可以配置表单登录、HTTP基本认证、JWT认证等多种认证方式,以及自定义过滤器链、异常处理等
    12. */
    13. protected void configure(HttpSecurity http) throws Exception ;
    复制代码
  • 写自己的Security配置类
    ##记得加@EnableWebSecurity注解##
    1. @Configuration
    2. @EnableWebSecurity
    3. public class SecurityConfig extends WebSecurityConfigurerAdapter {
    4.     /**
    5.      * 从 Spring5 开始,强制要求密码要加密
    6.      * BCryptPasswordEncoder是Spring Security提供的一个密码加密工具类,
    7.      * 它实现了PasswordEncoder接口,用于对用户的密码进行安全哈希加密存储,增强了系统的安全性。
    8.      */
    9.     @Bean
    10.     PasswordEncoder passwordEncoder(){
    11.         return new BCryptPasswordEncoder();
    12.     }
    13.     @Override
    14.     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    15.         auth.inMemoryAuthentication()
    16.                 //配置了两个用户,包括用户的用户名、角色、密码,用户密码已经加密(123)
    17.                 .withUser("zhangsan")
    18.                 //角色
    19.                 .roles("BOSS")
    20.                 //密码已经加密,可以通过 passwordEncoder.encode("123")获取
    21.                 .password("$2a$10$dQLFreAJHM0F.4XAWQMTA.kB5W3H2.hjA6xBUJFHTFT7iHRzO0flm")
    22.                 //连接方法
    23.                 .and()
    24.                 .withUser("lisi")
    25.                 .roles("EMPLOYEE")
    26.                 .password("$2a$10$RDdoj3sm/RD7HzqSnU864eEE5kEZZxbyQqnYQJGrO2pgkUGCDutTC");
    27.     }
    28. }
    复制代码
    重启应用就可以通过zhangsan和lisi登陆了,虽然实现认证功能,但是受保护的资源都是一样的,只要登陆了,全部的接口都能访问。
    但实际企业开发过程中我们要根据进行脚色管理,如果要实现这些功能 就需要重写 WebSecurityConfigurerAdapter 中的 HttpSecurity 方法:
    1. @Configuration
    2. @EnableWebSecurity
    3. public class SecurityConfig extends WebSecurityConfigurerAdapter {
    4.     @Override
    5.     protected void configure(HttpSecurity http) throws Exception {
    6.         http.authorizeRequests() //开启登录配置
    7.                 // 使用antMatchers 匹配请求路径
    8.                 .antMatchers("/public/**").permitAll()  // 允许所有人访问以 "/public/" 开头的资源
    9.                 //访问以 "/boss/" 开头的资源需要 "boss" 角色
    10.                 .antMatchers("/boss/**").hasRole("ROLE_boss")
    11.                 //访问以 "/employee/" 开头的资源,必须具备 boos、manager 的角色
    12.                 .antMatchers("/employee/**").access("hasAnyRole('ROLE_boss','ROLE_manager')")
    13.                 // 访问以 "/api/" 开头的资源需要经过身份验证
    14.                 .antMatchers("/api/**").authenticated()
    15.                 // 任何其他请求也需要经过身份验证
    16.                 .anyRequest().authenticated()
    17.                 //开启表单登录
    18.                 .and().formLogin();
    19.     }
    20. }
    复制代码
  • 誊写接口验证

  • 使用注解简化
    上面介绍的认证与授权都是基于 URL 的,也可以通过注解来机动地配置方法安全,要使用相关注解,起首要通过 @EnableGlobalMethodSecurity 注解开启基于注解的安全配置:
    1. @Configuration
    2. @EnableWebSecurity
    3. @EnableGlobalMethodSecurity(prePostEnabled=true,securedEnabled=true)
    4. public class SecurityConfig extends WebSecurityConfigurerAdapter {
    5. }
    复制代码
    (1) prePostEnabled=true
    开启后会解锁 @PreAuthorize@PostAuthorize 两个注解

    • @PreAuthorize
      会在执行方法前验证, 推荐使用 @PreAuthorize 因为支持Spring EL表达式
      1. @PreAuthorize("hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')")  //必须有全部的权限才可以访问
      2. @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER')") //至少有一个即可访问
      3. @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")  // 同上
      复制代码
    • @PostAuthorize
      会在执行方法后验证
    (2) securedEnabled=true
           开启后会解锁 @Secured 注解,是基于脚色的简单访问控制,Spring Security 3.x 之后不推荐。
    判定是否具有脚色,注意这里匹配的字符串需要添加前缀 “ROLE_”
       
    1.     @Secured({ "ROLE_admin", "ROLE_user" })
    2.     @Secured({"ROLE_admin"})
    3.     @GetMapping(value = "/test")
    4.     public String testSecured() {
    5.         return "testSecured";
    6.     }
    复制代码

  • 验证
    得要把之前的 配置移除掉
    1. // .antMatchers("/bossapi/**").hasRole("boss") 都移除掉
    复制代码

5. Spring Security 联合 JWT使用


  • 为什么要联合JWT使用?
    Spring Security与JWT的联合在分布式系统、移动端和单页应用等场景下具备机动性、可扩展性和安全性,因此被广泛应用于当代Web应用步伐的身份验证和授权中。
    JWT有不可替代的上风,但Security也有使用的便利性(已内置了很多权限验证功能),所以我们要联合一起使用,联合的时候,我们只使用Security鉴权功能,登陆功能不需要,因为我们已经自己实现了JWT相关的登陆接口,只是登陆乐成后我们要把用户的脚色Role写进token就可以了。
  • 脚色Role认证
    默认环境下,UsernamePasswordAuthenticationFilter 处置惩罚的是 POST 请求到 /login 路径, 我们可以模仿 UsernamePasswordAuthenticationFilter 誊写自己的过滤器
    1. @Configuration
    2. @EnableWebSecurity
    3. @EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = false) // 开启注解
    4. public class SecurityConfig extends WebSecurityConfigurerAdapter {
    5.     @Override
    6.     protected void configure(HttpSecurity http) throws Exception {
    7.         http.csrf().disable();
    8.         http.logout().disable();
    9.         http.formLogin().disable();
    10.         http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);// 禁用session
    11.         http.authorizeRequests().anyRequest().authenticated();// 所有请求都需要认证
    12.         // 添加自定义的过滤器
    13.         http.addFilterBefore(new JwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
    14.         // 配置HTTP安全模块的异常处理,指定未授权访问时的处理逻辑
    15.         http.exceptionHandling().authenticationEntryPoint((HttpServletRequest request, HttpServletResponse response,
    16.                                                            AuthenticationException authException)->{
    17.             response.setContentType("application/json;charset=utf-8");
    18.             response.getWriter().write("{"code":403,"message":"未授权"}");
    19.         });
    20.     }
    21.     private class JwtTokenFilter extends BasicAuthenticationFilter {
    22.         public JwtTokenFilter() throws Exception {
    23.             super(authenticationManager());
    24.         }
    25.         @Override
    26.         protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
    27.             // 从请求头中获取认证令牌
    28.             String token = request.getHeader("token");
    29.             // 初始化一个空的权限列表
    30.             List<GrantedAuthority> authorityList = new ArrayList<>();
    31.             // 如果令牌存在且不为空
    32.             if (ObjectUtil.isNotEmpty(token)) {
    33.                 // 解析令牌中的载荷信息
    34.                 JSONObject jsonObject = JSONUtil.parseObj(JWTUtil.parseToken(token).getPayload().toString());
    35.                 // 获取用户角色信息
    36.                 JSONArray roles = jsonObject.getJSONArray("roles");
    37.                 // 如果角色信息存在且不为空
    38.                 if (ObjectUtil.isNotEmpty(roles)) {
    39.                     // 遍历角色信息,构建权限列表
    40.                     for (Object role : roles) {
    41.                         SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.toString());
    42.                         authorityList.add(authority);
    43.                     }
    44.                 }
    45.             }
    46.             // 创建认证对象,用于存放权限列表
    47.             UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken("唯一", null, authorityList);
    48.             // 将认证对象设置到当前的安全上下文中
    49.             SecurityContextHolder.getContext().setAuthentication(authenticationToken);
    50.             // 继续执行过滤器链中的下一个过滤器
    51.             chain.doFilter(request, response);
    52.         }
    53.     }
    54. }
    复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

宁睿

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表