use*_*241 95 java spring spring-mvc spring-security
任何人都可以解释何时覆盖configure(HttpSecurity),configure(WebSecurity)和configure(AuthenticationManagerBuilder)?
小智 118
configure(AuthenticationManagerBuilder)用于通过允许轻松添加AuthenticationProvide来建立身份验证机制:例如,以下内容使用内置的"user"和"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限制为具有ADMIN角色的用户,并声明需要任何其他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/**");
}
有关Spring Security Java Config Preview的更多信息,请参阅以下链接:Web Security
WebSecurityignoring()方法的一般使用会忽略 Spring Security,并且 Spring 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 的安全性- 例如,下面的示例将以 开头的 URL 限制为/admin/具有ADMIN 角色的用户,并声明任何其他 URL 需要成功验证。
configure(WebSecurity)用于影响全局安全性的配置设置(忽略资源、设置调试模式、通过实现自定义防火墙定义拒绝请求)。例如,以下方法将导致任何以 开头的请求/resources/都被忽略以进行身份验证。
AuthenticationManagerBuilder
extends AbstractConfiguredSecurityBuilder<AuthenticationManager,AuthenticationManagerBuilder>
implements ProviderManagerBuilder<AuthenticationManagerBuilder>
SecurityBuilder 用于创建一个AuthenticationManager. 允许轻松构建内存身份验证、LDAP 身份验证、基于 JDBC 的身份验证、添加 UserDetailsService 和添加 AuthenticationProvider 的.
@Override
     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER"); 
        auth.userDetailsService(customUserDetailService).passwordEncoder(new BCryptPasswordEncoder());
     }
| 归档时间: | 
 | 
| 查看次数: | 20190 次 | 
| 最近记录: |