在Spring Security中,我知道可以将特定URL配置为只有已记录的用户或具有特定角色的用户可以通过以下语句访问:
http.authorizeRequests().antMatchers("/admin/**").access("hasRole('ADMIN')");
但有没有办法让它只能由匿名者访问?这意味着它限制了登录用户的访问权限。例如登录页面,如果用户已登录,我希望它不可访问。只有在没有用户登录时才能访问它。有没有办法实现这一点?
您可以像这样添加匿名
@Override
public void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.antMatchers("/api/**")
.and()
.authorizeRequests()
.antMatchers("/api/books/*").anonymous();
}