谁能解释一下何时覆盖configure(HttpSecurity)
, configure(WebSecurity)
and configure(AuthenticationManagerBuilder)
?
配置(AuthenticationManagerBuilder)用于通过允许轻松添加 AuthenticationProvider 来建立身份验证机制:例如以下定义了使用内置“用户”和“管理员”登录名的内存中身份验证。
public void configure(AuthenticationManagerBuilder auth) {
auth
.inMemoryAuthentication()
.withUser("user")
.password("password")
.roles("USER")
.and()
.withUser("admin")
.password("password")
.roles("ADMIN","USER");
}
配置(HttpSecurity)允许根据选择匹配在资源级别配置基于 Web 的安全性 - 例如下面的示例将以 /admin/ 开头的 URL 限制为具有 ADMIN 角色的用户,并声明任何其他 URL 都需要成功进行身份验证。
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
}
配置(网络安全)用于影响全局安全的配置设置(忽略资源、设置调试模式、通过实施自定义防火墙定义拒绝请求)。例如,出于身份验证目的,以下方法将导致以 /resources/ 开头的任何请求被忽略。
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**");
}
您可以参考以下链接了解更多信息Spring Security Java 配置预览:Web 安全 http://spring.io/blog/2013/07/03/spring-security-java-config-preview-web-security/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)