英文:
Spring Security protected void configure(HttpSecurity http) Please explain the proper use of " and() ". What does it mean?
问题
I have lots and lots of examples from many search results in this platform and others, but I can't find an explanation of the "and()" . Obviously some kind of delimiter. Possibly doing the logical AND (&&) but maybe not.
我有许多来自这个平台和其他地方的搜索结果示例,但我找不到关于 "and()" 的解释。显然是某种分隔符。可能执行逻辑 AND(&&),但也可能不是。
I want to understand proper usage and what it does...what it means.
我想了解它的正确用法以及它是做什么的...它的含义是什么。
I hope this question is salient and the answers useful for others
我希望这个问题对其他人有用。
Then inside of that document:
然后在该文档的内部:
protected void configure(HttpSecurity http)
throws java.lang.Exception
Override this method to configure the HttpSecurity. Typically subclasses should not invoke this method by calling super as it may override their configuration. The default configuration is:
覆盖此方法以配置 HttpSecurity。通常子类不应通过调用 super 来调用此方法,因为它可能会覆盖它们的配置。默认配置是:
http.authorizeRequests().anyRequest().authenticated().and().formLogin().and().httpBasic();
英文:
I have lots and lots of examples from many search results in this platform and others, but I can't find an explanation of the " and() ". Obviously some kind of delimiter. Possibly doing the logical AND (&&) but maybe not.
I want to understand proper usage and what it does...what it means.
I hope this question is salient and the answers useful for others
Then inside of that document:
protected void configure(HttpSecurity http)
throws java.lang.Exception
Override this method to configure the HttpSecurity. Typically subclasses should not invoke this method by calling super as it may override their configuration. The default configuration is:
http.authorizeRequests().anyRequest().authenticated().and().formLogin().and().httpBasic();
答案1
得分: 2
Here is the translated content:
- 让我们看下面的示例。两者是等价的。
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated();
http.formLogin()
.loginPage("/login")
.permitAll();
http.logout()
.permitAll();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
-
实际上,在这个示例中,我们正在配置不同的配置器,如
ExpressionUrlAuthorizationConfigurer
、FormLoginConfigurer
和LogoutConfigurer
。尽管在第一个示例中它们被分别配置,但它们会一起应用。因此,在这里,AND
起到了逻辑上的“与”的作用。 -
现在请注意,例如,
.anyRequest().authenticated()
的返回类型是ExpressionInterceptUrlRegistry
,但formLogin()
方法只存在于HttpSecurity
类型的对象中,所以在构建器模式中,and()
起到了切换返回类型的第二个作用,也就是一旦你调用了anyRequest().authenticated().and()
,返回类型就变成了HttpSecurity
,所以现在可以开始调用formLogin()
。 -
看到
IntelliJ
在各个点显示的返回类型。
英文:
- Lets take the following example. Both are equivalent.
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated();
http.formLogin()
.loginPage("/login")
.permitAll();
http.logout()
.permitAll();
}
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
-
Actually we are configuring different configurers here like
ExpressionUrlAuthorizationConfigurer
,FormLoginConfigurer
andLogoutConfigurer
in this example. Even though, they are configured separately in the first one, they are all applied together. So hereAND
plays the logical AND role. -
Now notice, for example, return type of
.anyRequest().authenticated()
isExpressionInterceptUrlRegistry
, but the methodformLogin()
is only present in object of typeHttpSecurity
, so in builder patternand()
plays the second role of switching return type, i.e as soon as you call,anyRequest().authenticated().and()
, the return type isHttpSecurity
so now it allows you startformLogin()
-
See the
Intellij
showing the return types at various points.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论