有人能解释什么时候要重写configure(HttpSecurity),configure(WebSecurity)和configure(AuthenticationManagerBuilder)吗?
发布于 2015-02-02 06:42:01
configure(AuthenticationManagerBuilder)通过允许轻松添加AuthenticationProviders来建立身份验证机制:例如,下面定义了内置的‘用户’和'admin‘登录的内存中身份验证。
public void configure(AuthenticationManagerBuilder auth) {
auth
.inMemoryAuthentication()
.withUser("user")
.password("password")
.roles("USER")
.and()
.withUser("admin")
.password("password")
.roles("ADMIN","USER");
}configure(HttpSecurity)允许根据选择匹配在资源级别上配置基于web的安全性--例如,下面的示例将以/ ADMIN /开头的URL限制为具有管理角色的用户,并声明任何其他URL都需要成功地进行身份验证。
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
}configure(WebSecurity)用于影响全局安全性的配置设置(忽略资源、设置调试模式、通过实现自定义防火墙定义拒绝请求)。例如,为了身份验证目的,下面的方法将导致任何以/resources/开头的请求被忽略。
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**");
}要获得更多信息,可以参考以下链接Security Config预览: Web安全性
发布于 2019-12-28 07:32:51
WebSecurity ignoring()方法的一般使用忽略了Security,而且Security的任何特性都不可用。WebSecurity是基于HttpSecurity之上的。
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**")
.antMatchers("/publics/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/publics/**").hasRole("USER") // no effect
.anyRequest().authenticated();
}上面示例中的WebSecurity让Spring忽略了/resources/**和/publics/**。因此,.antMatchers("/publics/**").hasRole("USER")在HttpSecurity中是不考虑的。
这将从安全过滤链中完全省略请求模式。请注意,任何匹配此路径的内容都将不应用身份验证或授权服务,并且可以自由访问。
configure(HttpSecurity)允许根据选择匹配在资源级别上配置基于web的安全性,例如,下面的示例将以/admin/开头的URL限制为具有管理角色的用户,并声明任何其他URL都需要成功地进行身份验证。
configure(WebSecurity)用于影响全局安全性的配置设置(忽略资源、设置调试模式、通过实现自定义防火墙定义拒绝请求)。例如,为了进行身份验证,下面的方法将导致任何以/resources/开头的请求被忽略。
AuthenticationManagerBuilder
extends AbstractConfiguredSecurityBuilder<AuthenticationManager,AuthenticationManagerBuilder>
implements ProviderManagerBuilder<AuthenticationManagerBuilder>SecurityBuilder用来创建一个AuthenticationManager。允许轻松构建内存身份验证、LDAP身份验证、基于JDBC的身份验证、添加UserDetailsService和添加身份验证提供程序。
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
auth.userDetailsService(customUserDetailService).passwordEncoder(new BCryptPasswordEncoder());
}https://stackoverflow.com/questions/22998731
复制相似问题