聊聊spring security的permitAll以及webIgnore
    		       		warning:
    		            这篇文章距离上次修改已过422天,其中的内容可能已经有所变动。
    		        
        		                
                在Spring Security中,permitAll()方法用于配置安全路由,允许所有用户访问它们,无需认证或授权。而ignoringAntMatchers()方法用于配置Spring Security忽略特定的路由,不对这些路由应用任何安全配置。
以下是使用permitAll()和ignoringAntMatchers()的示例代码:
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            // 忽略所有以"/api"开头的URL,不应用任何安全配置
            .authorizeRequests()
            .antMatchers("/api/**").permitAll()
            // 其他所有请求需要认证
            .anyRequest().authenticated()
            .and()
            // 其他的配置...
            ;
    }
}在这个例子中,任何以/api/开头的URL都将被忽略,不会被Spring Security进行安全控制,即允许所有用户无条件访问。其他所有路由都需要用户通过认证后才能访问。
评论已关闭