我有两个REST端点:
{
"query": {
"nested": {
"path": "tags",
"query": {
"bool": {
"must": [
{
"match": {
"tags.label": "A"
},
"range": {
"tags.confidence": {
"gte": 37
}
}
},
{
"match": {
"tags.label": "B"
},
"range": {
"tags.confidence": {
"gte": 80
}
}
}
]
}
}
}
}
}
/noAuth/rest/sayHi
我只想登录/rest/auth/getMsg
并直接访问/rest/auth/getMsg
。
当我在/noAuth/rest/sayHi
中使用以下模式时
WebSecurityConfigurerAdapter.configure(HttpSecurity http)
我只在@EnableWebSecurity
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("Java Techie")
.password("Password")
.roles("ADMIN");
auth
.inMemoryAuthentication()
.withUser("Basant")
.password("Password2")
.roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/rest/**").authenticated()
.anyRequest().permitAll()
.and()
.httpBasic();}
}
}
中得到登录提示,而在/rest/auth/getMsg
中却没有得到提示,这是预期的。
但是当我使用下面的模式时,我在/noAuth/rest/sayHi
和/rest/auth/getMsg
中都得到登录提示,这对我来说是意想不到的。
/noAuth/rest/sayHi
我第二次知道自己做错了事,所以我想了解为什么我不只登录http
.authorizeRequests()
.anyRequest().authenticated()
.antMatchers("/rest/**").permitAll()
.and()
.httpBasic();`
并直接访问/rest/auth/getMsg
。
更新
@nully这是有道理的,但在其他情况下会破坏我的理解。可以说如果我使用这种模式:
/noAuth/rest/sayHi
仅允许用户=“ Java Techie”登录,因为它是http
.authorizeRequests()
.anyRequest().authenticated()
.anyRequest().hasRole("ADMIN")
.and()
.httpBasic();`
,并且对用户=“ Basant”抛出ADMIN
。
但是当我使用
403 Forbidden
根据您的解释,它不应具有角色=“ USER”的权限,因此不允许用户=“ Basant”访问http
.authorizeRequests()
.anyRequest().authenticated()
.antMatchers("/rest/**").hasRole("ADMIN")
.and()
.httpBasic();
。但是实际上,它允许我在使用user =“ Basant”时访问/rest/auth/getMsg
。
答案 0 :(得分:0)
执行从左到右。在第一种情况下:.antMatchers(“ / rest / ”)。authenticated(),因此对所有与rest /
相匹配的内容进行身份验证在2种情况下:.anyRequest()。authenticated(),因此任何请求在进行下一步操作之前都将需要身份验证。
答案 1 :(得分:0)
如果没有其他规则,则默认要求进行身份验证。您应该添加类似antMatchers("/noAuth/**").permitAll()
的内容。