SpringSecurity安全框架整理

一:简单介绍

Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

spring security 的核心功能主要包括:

  • 认证 (你是谁)
  • 授权 (你能干什么)
  • 攻击防护 (防止伪造身份)

目前两大火热的安全框架:SpringSecurity和shrio,他们两个十分相似

主要功能:

  • 功能权限控制
  • 访问权限控制
  • 菜单权限控制

.拦截器,过滤器,也可以达到同样的目的,但是这样会有一个缺点,大量的原生代码,冗余。

SpringSecurity安全框架整理_第1张图片

其核心就是一组过滤器链,项目启动后将会自动配置。最核心的就是 Basic Authentication Filter 用来认证用户的身份,一个在spring security中一种过滤器处理一种认证方式。

三大核心

  • webSecurityConfigurerAdapter:自定义security策略
  • AuthenticationManagerBuilder:自定义认证策略
  • @EnablewebSecurity:开启webSercurity

参考:

官网地址:Spring Security

帮助文档:https://docs.spring.io/spring-security/site/docs/current/reference/html5/

二:如何应用

依赖的引入

        
        
        
            org.springframework.boot
            spring-boot-starter-security
        
       
        
            org.thymeleaf.extras
            thymeleaf-extras-springsecurity4
            3.0.4.RELEASE
        

security配置(有一个固定模式)

WebSecurityConfigurerAdapter 这个接口里面的configure有三个重载

  1. HttpSecurity http 允许基于选择匹配在资源级别配置基于Web的安全性-例如,以下示例将以/ admin /开头的URL限制为具有ADMIN角色的用户,并声明需要使用其他任何URL成功认证。

  2. AuthenticationManagerBuilder auth 用于通过允许轻松添加AuthenticationProviders来建立身份验证机制:例如,以下内容定义了具有内置“用户”和“管理员”登录名的内存中身份验证。

  3. WebSecurity web 用于影响全局安全性的配置设置(忽略资源,设置调试模式,通过实现自定义防火墙定义拒绝请求)。例如,以下方法将导致以/ resources /开头的任何请求都被忽略,以进行身份​​验证。

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
    }
}

具体配置项内容,重写configure方法(因为这里是配置页面跳转限制用户的,所以选用了HttpSecurity )

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页所有人可以访问,功能页只有对应的权限才能进去
        http.authorizeRequests()//授权请求方法
                .antMatchers("/").permitAll()
                .antMatchers("views/level1/*").hasRole("vip1")//什么角色可以访问什么位置 
                                                              //(vip1可以访问views下的level1下的所有页面)
                                                              //配合前端,可以实现权限过滤,该展示那些页面,不该展示那些页面
                .antMatchers("views/level2/*").hasRole("vip2")
                .antMatchers("views/level3/*").hasRole("vip3");



                //其他项的配置
        //没有权限则默认跳转到登录页    "/toLogin"覆盖默认的登录页,访问改路径下的登录页(自己写的页面,比默认的好看) 
        http.formLogin().loginPage("/toLogin");

        //开启网页注销功能
        http.logout().logoutSuccessUrl("/");

        //防止网站攻击  关闭csrf功能
        http.csrf().disable();

        //开启网页记住我按钮功能  本质是cookie,默认保证两周(默认页面)      自定义页面的remember需要传到这来
        http.rememberMe().rememberMeParameter("remember");
    }

(这里配置的是身份验证机制,讲白了就是验证账户密码的所以选用了AuthenticationManagerBuilder )

    //认证功能
    //springSecurity5.0+ 中新增了很多的加密方法,必须要给密码加密,否则报错 no PasswordEncoding
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("kuangshen").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
                .and()
                .withUser("111").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
    }

关于配置方面就至此结束。详细的内容从官网获取

官网地址:Spring Security

帮助文档:https://docs.spring.io/spring-security/site/docs/current/reference/html5/

前端用thymeleaf获取安全的配置,举几个列子

按照权限显示:sec:authorize="hasRole("xxx")  当前登录人有xxx角色才显示该部分内容

是否登陆显示:sec:authorize="!isAuthenticated()" 当前登录了才显示该部分内容

你可能感兴趣的:(java,安全,spring)