Security详解

1.jwt,spring security,oauth2.0,shiro区别

jwt:是一个鉴权生成加密token的一个名称

Spring security:权限框架,基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架;可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI。

Shiro:权限框架,可在C/S下运行。 shiro是一套权限管理框架,包括认证、授权等,在使用时直接写相应的接口(小而简单的Shiro就足够)

Shiro 默认是使用Session认证。

oauth2.0 :一种权限实现标准,是一种安全的授权框架,提供了一套详细的授权机制。用户或应用可以通过公开的或私有的设置,授权第三方应用访问特定资源。(如果不介意API的使用依赖于外部的第三方认证提供者,你可以简单地把认证工作留给认证服务商去做。)

2.HttpSecurity常用方法

方法说明
anyRequest            匹配所有请求路径
access                SpringEl表达式结果为true时可以访问
anonymous             匿名可以访问
denyAll               用户不能访问
fullyAuthenticated    用户完全认证可以访问(非remember-me下自动登录)
hasAnyAuthority       如果有参数,参数表示权限,则其中任何一个权限可以访问
hasAnyRole            如果有参数,参数表示角色,则其中任何一个角色可以访问
hasAuthority          如果有参数,参数表示权限,则其权限可以访问
hasIpAddress          如果有参数,参数表示IP地址,如果用户IP和参数匹配,则可以访问
hasRole               如果有参数,参数表示角色,则其角色可以访问
permitAll             用户可以任意访问
rememberMe            允许通过remember-me登录的用户访问
authenticated         用户登录后可访问
cors

配置跨域资源共享( CORS )

在 SpringSecurity 中关闭 CSRF 因为前端向后台发送 post 请求时,必须验证 csrf,否则会报错 403 Forbidden。

csrf添加 CSRF 支持,使用WebSecurityConfigurerAdapter时,默认启用
rememberMe允许配置“记住我”的验证
authorizeRequests允许基于使用HttpServletRequest限制访问
requestCache允许配置请求缓存
exceptionHandling允许配置错误处理
  
logout添加退出登录支持。当使用WebSecurityConfigurerAdapter时,这将自动应用。默认情况是,访问URL”/ logout”,使HTTP Session无效来清除用户,清除已配置的任何#rememberMe()身份验证,清除SecurityContextHolder,然后重定向到”/login?success”
formLogin指定支持基于表单的身份验证。如果未指定FormLoginConfigurer#loginPage(String),则将生成默认登录页面
oauth2Login根据外部OAuth 2.0或OpenID Connect 1.0提供程序配置身份验证
requiresChannel配置通道安全。为了使该配置有用,必须提供至少一个到所需信道的映射
httpBasic配置 Http Basic 验证
addFilterAt在指定的Filter类的位置添加过滤器
openidLogin用于基于 OpenId 的验证
headers将安全标头添加到响应
sessionManagement允许配置会话管理
portMapper允许配置一个PortMapper(HttpSecurity#(getSharedObject(class))),其他提供SecurityConfigurer的对象使用 PortMapper 从 HTTP 重定向到 HTTPS 或者从 HTTPS 重定向到 HTTP。默认情况下,Spring Security使用一个PortMapperImpl映射 HTTP 端口8080到 HTTPS 端口8443,HTTP 端口80到 HTTPS 端口443
jee配置基于容器的预认证。 在这种情况下,认证由Servlet容器管理
x509配置基于x509的认证
securityContext在HttpServletRequests之间的SecurityContextHolder上设置SecurityContext的管理。 当使用WebSecurityConfigurerAdapter时,这将自动应用
servletApi将HttpServletRequest方法与在其上找到的值集成到SecurityContext中。 当使用WebSecurityConfigurerAdapter时,这将自动应用

 

 @Override
    public void configure(HttpSecurity http) throws Exception {
        //忽略hello post接口(post请求不忽略会报错)
        http.csrf().ignoringAntMatchers("/hellopost");

        http.authorizeRequests()
                .antMatchers("/hello", "/hellopost").permitAll()
                .anyRequest().authenticated()
                .antMatchers("/hello2").hasRole("administrator")
                .and().formLogin().and().httpBasic();

    }

    //配置密码:方式1
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = passwordEncoder.encode("111111");
        auth.inMemoryAuthentication().withUser("user").password(password).roles("administrator");
    }

3.requestMatchers()方法与authorizeRequests()区别

        requestMatchers()配置的是哪些url进行安全控制,authorizeRequests()配置的是如何进行控制

例如: requestMatchers().anyRequest()=http.authorizeRequests().anyRequest().access(“permitAll”)=http.authorizeRequests().anyRequest().permitAll();

http.authorizeRequests().anyRequest().authenticated() =  http.authorizeRequests().antMatchers("/").authenticated() ;

4.ResourceServerConfigurerAdapter与WebSecurityConfigurerAdapter区别

 1.ResourceServerConfigurerAdapter是用于spring-security-oauth2, 配置哪些url要进行oauth2认证。

  2.WebSecurityConfigurerAdapter是用于当前这个项目本身的访问控制,它属于spring-security-config。

  3.ResourceServerConfigurerAdapter与WebSecurityConfigurerAdapter都有一个相同的方法,配置url的访问安全控制策略:如果在这个方法中配置了相同的url访问控制,会发现ResourceServerConfigurerAdapter配置控制策略生效,而WebSecurityConfigurerAdapter配置的策略不起作用。是因为ResourceServerConfigurerAdapter的order值小,优先级高。所以ResourceServerConfigurerAdapter起作用。

4.  WebSecurityConfigurerAdapter在springboot 2.7之后就开始过时了,需要使用SecurityFilterChain进行配置。

 

参考:Spring Security中HttpSecurity常用方法及说明_wh柒八九的博客-CSDN博客_httpsecurity

HttpSecurity常用方法详解_糊口度日的小白的博客-CSDN博客_httpsecurity

requestMatchers()方法与authorizeRequests()区别,ResourceServerConfigurerAdapter与WebSecurityConfigurerAdapt_join_null的博客-CSDN博客

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值