Spring Security仅允许匿名访问

问题描述 投票:-1回答:1

在Spring Security中,我知道可以将特定URL配置为只有已记录的用户或具有特定角色的用户可以通过以下语句访问:

http.authorizeRequests().antMatchers("/admin/**").access("hasRole('ADMIN')");

但有没有办法让它只能由匿名者访问?这意味着它限制了登录用户的访问权限。例如登录页面,如果用户已登录,我希望它不可访问。只有在没有用户登录时才能访问它。有没有办法实现这一点?

java spring spring-security
1个回答
1
投票

您可以像这样添加匿名

@Override
public void configure(HttpSecurity http) throws Exception {
    http
    .requestMatchers()
    .antMatchers("/api/**")
    .and()
    .authorizeRequests()
    .antMatchers("/api/books/*").anonymous();
}
© www.soinside.com 2019 - 2024. All rights reserved.