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,AuthorizationServerConfigurerAdapter
1.ResourceServerConfigurerAdapter:
ResourceServerConfigurerAdapter是用于spring-security-oauth2, 配置哪些url要进行oauth2认证。
2.WebSecurityConfigurerAdapter:
WebSecurityConfigurerAdapter是用于当前这个项目本身的访问控制,它属于spring-security-config。
3.AuthorizationServerConfigurerAdapter:
AuthorizationServerConfigurerAdapter用于oauth2下配置认证服务器。
总结:
- ResourceServerConfigurerAdapter与WebSecurityConfigurerAdapter都有一个相同的方法,配置url的访问安全控制策略:如果在这个方法中配置了相同的url访问控制,会发现ResourceServerConfigurerAdapter配置控制策略生效,而WebSecurityConfigurerAdapter配置的策略不起作用。是因为ResourceServerConfigurerAdapter的order值小,优先级高。所以ResourceServerConfigurerAdapter起作用。
- WebSecurityConfigurerAdapter在springboot 2.7之后就开始过时了,需要使用SecurityFilterChain进行配置。
5.permitAll和webIgnore
permitAll配置
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/css/**", "/js/**","/fonts/**").permitAll()
.anyRequest().authenticated();
}
}
web.ignore配置
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/css/**");
web.ignoring().antMatchers("/js/**");
web.ignoring().antMatchers("/fonts/**");
}
}
5.1 区别
WebSecurity主要是配置跟web资源相关的,比如css、js、images等等,但是这个还不是本质的区别,关键的区别如下:
- ingore是完全绕过了spring security的所有filter,相当于不走spring security
- permitall没有绕过spring security,其中包含了登录的以及匿名的。
参考:
Spring Security中HttpSecurity常用方法及说明_wh柒八九的博客-CSDN博客_httpsecurity
本文探讨了JWT、Spring Security、Shiro和OAuth2.0的区别,重点介绍了Spring Security的HttpSecurity配置方法,包括常用的控制策略和不同框架的适用场景。还对比了requestMatchers()与authorizeRequests()的差异,以及ResourceServerConfigurerAdapter与WebSecurityConfigurerAdapter的集成问题。

1万+

被折叠的 条评论
为什么被折叠?



