up::自定义决策
新建Spring项目
运行成功:
用户名: 默认user 密码: 自动生成,在图中
访问8080端口
继承重写配置文件
@Configurable
@EnableWebSecurity
public class springSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeHttpRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.logout().permitAll()
.and()
.formLogin();
http.csrf().disable();
}
}
@Override public void configure(WebSecurity web) throws Exception {
// 不拦截静态资源的访问
web.ignoring().antMatchers("/js/**", "/css/**", "/images/**");
}
http.authorizeRequests()
方法有很多子方法,每个子匹配器将会按照声明的顺序起作用
通过mvcMatchers(url)
或antMatchers(url)
匹配url,然后通过hasRole()
方法指定角色或通过hasAuthority()
方法指定权限,
permitAll()表示不需要认证,可以直接访问
参考学习: SpringSecurity基础配置 - 掘金 这里备份拓展:SpringSecurity基础配置