Spring Security之Config模块详解(TODO)

发博词

由于软件开发中,要解决的安全的问题非常多且零碎,导致了Spring Security在配置项也很多,对于接触不久的人来说,可能本身安全方面的东西平时“工作生活”中就接触比较少,导致在学习Spring Security的过程中,有种剪不断理还乱的感觉。下面我们就通过Spring Security的Config模块的架构,来理清这个关系。

Spring Security Configurer

Spring Security Config模块一共有3个builder,认证相关的AuthenticationManagerBuilder和web相关的WebSecurity、HttpSecurity。

AuthenticationManagerBuilder

AuthenticationManagerBuilder用来配置全局的认证相关的信息,其实就是AuthenticationProvider和UserDetailsService,前者是认证服务提供商,后者是用户详情查询服务。

WebSecurity

全局请求忽略规则配置(比如说静态文件,比如说注册页面)、全局HttpFirewall配置、是否debug配置、全局SecurityFilterChain配置、privilegeEvaluator、expressionHandler、securityInterceptor、

HttpSecurity

具体的权限控制规则配置。一个这个配置相当于xml配置中的一个标签。
各种具体的认证机制的相关配置,OpenIDLoginConfigurer、AnonymousConfigurer、FormLoginConfigurer、HttpBasicConfigurer
LogoutConfigurer
RequestMatcherConfigurer:spring mvc style、ant style、regex style
HeadersConfigurer:
CorsConfigurer、CsrfConfigurer
SessionManagementConfigurer:
PortMapperConfigurer:
JeeConfigurer:
X509Configurer:
RememberMeConfigurer:
ExpressionUrlAuthorizationConfigurer:
RequestCacheConfigurer:
ExceptionHandlingConfigurer:
SecurityContextConfigurer:
ServletApiConfigurer:
ChannelSecurityConfigurer:
此模块的authenticationProvider和userDetailsService;
SecurityFilterChain控制

WebSecurityConfigurerAdapter

spring security为web应用提供了一个WebSecurityConfigurerAdapter适配器,应用里spring security相关的配置可以通过继承这个类来编写;具体是提供了上边三个顶级配置项构建器的构建重载回调方法;

protected void configure(AuthenticationManagerBuilder auth) throws Exception {
}

public void configure(WebSecurity web) throws Exception {
}

protected void configure(HttpSecurity httpSecurity) throws Exception {
}

具体配置思路

  1. httpSecurity.authorizeRequests()返回一个ExpressionInterceptUrlRegistry对象,这个对象就一个作用,注册intercept url规则权限匹配信息,通过设置URL Matcher,antMatchers,mvcMatchers,regexMatchers或者直接设置一个一个或者多个RequestMatcher对象;

  2. 上边设置matchers的方法会返回一个AuthorizedUrl对象,用于接着设置符合其规则的URL的权限信息,AuthorizedUrl对象提供了access方法用于设置一个权限表达式,比如说字符串“hasRole(‘ADMIN’) and hasRole(‘DBA’)”,同时提供了多个方便的语义方法,比如说:

public ExpressionInterceptUrlRegistry hasRole(String role) 
public ExpressionInterceptUrlRegistry hasAuthority(String authority)

这些方法返回值是ExpressionInterceptUrlRegistry,用于接着设置下一条过滤规则:

protected void configure(HttpSecurity http) throws Exception {
	http
		.authorizeRequests()
			.antMatchers("/resources/**", "/signup", "/about").permitAll()
			.antMatchers("/admin/**").hasRole("ADMIN")
			.antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
			.anyRequest().authenticated()
			.and()
		// ...
		.formLogin();
}

上边1和2结合起来的功能相当于标签的功能;
UrlAuthorizationConfigurer能实现上边类似的功能;

protected void configure(HttpSecurity http) throws Exception {
 	http.apply(new UrlAuthorizationConfigurer<HttpSecurity>()).getRegistry()
 			.antMatchers("/users**", "/sessions/**").hasRole("USER")
 			.antMatchers("/signup").hasRole("ANONYMOUS").anyRequest().hasRole("USER");
 }
  1. formLogin和logout
  • FormLoginConfigurer
  • OpenIDLoginConfigurer
  • HttpBasicConfigurer
  • LogoutConfigurer
Spring Security中,configure方法是一个关键的配置方法,用于配置安全相关的设置。在这个方法中,我们可以使用不同的重载方法来实现不同的配置。 首先,我们可以使用configure(AuthenticationManagerBuilder auth)方法来配置认证管理器的详细信息。在这个方法中,我们可以定义用户的认证方式,比如内存中的用户、数据库中的用户或者使用自定义的用户认证逻辑。通过这个方法,我们可以配置用户的身份验证方式和角色授权规则。 其次,我们可以使用configure(WebSecurity web)方法来配置WebSecurity。这个方法用于配置Spring Security对静态资源的访问控制。我们可以指定哪些静态资源不需要经过Spring Security的认证和授权。 最后,我们可以使用configure(HttpSecurity httpSecurity)方法来配置HttpSecurity。这个方法用于配置Web应用的安全性。我们可以定义哪些URL需要经过认证,哪些URL可以匿名访问,以及具体的权限规则和拦截策略。 综上所述,通过使用configure方法和其重载方法,我们可以根据需要配置Spring Security的认证和授权规则,定义静态资源的访问控制,以及设定Web应用的安全策略。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Spring SecurityConfig模块详解TODO)](https://blog.csdn.net/chen517611641/article/details/73277337)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈振阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值