首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpSecurity、WebSecurity和AuthenticationManagerBuilder

HttpSecurity、WebSecurity和AuthenticationManagerBuilder
EN

Stack Overflow用户
提问于 2014-04-10 20:49:19
回答 2查看 29K关注 0票数 107

有人能解释什么时候要重写configure(HttpSecurity)configure(WebSecurity)configure(AuthenticationManagerBuilder)吗?

EN

回答 2

Stack Overflow用户

发布于 2015-02-02 06:42:01

configure(AuthenticationManagerBuilder)通过允许轻松添加AuthenticationProviders来建立身份验证机制:例如,下面定义了内置的‘用户’和'admin‘登录的内存中身份验证。

代码语言:javascript
复制
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都需要成功地进行身份验证。

代码语言:javascript
复制
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
        .antMatchers("/admin/**").hasRole("ADMIN")
        .anyRequest().authenticated()
}

configure(WebSecurity)用于影响全局安全性的配置设置(忽略资源、设置调试模式、通过实现自定义防火墙定义拒绝请求)。例如,为了身份验证目的,下面的方法将导致任何以/resources/开头的请求被忽略。

代码语言:javascript
复制
public void configure(WebSecurity web) throws Exception {
    web
        .ignoring()
        .antMatchers("/resources/**");
}

要获得更多信息,可以参考以下链接Security Config预览: Web安全性

票数 138
EN

Stack Overflow用户

发布于 2019-12-28 07:32:51

WebSecurity ignoring()方法的一般使用忽略了Security,而且Security的任何特性都不可用。WebSecurity是基于HttpSecurity之上的。

代码语言:javascript
复制
@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/开头的请求被忽略。

代码语言:javascript
复制
AuthenticationManagerBuilder
extends AbstractConfiguredSecurityBuilder<AuthenticationManager,AuthenticationManagerBuilder>
implements ProviderManagerBuilder<AuthenticationManagerBuilder>

SecurityBuilder用来创建一个AuthenticationManager。允许轻松构建内存身份验证、LDAP身份验证、基于JDBC的身份验证、添加UserDetailsService和添加身份验证提供程序。

代码语言:javascript
复制
@Override
     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER"); 
        auth.userDetailsService(customUserDetailService).passwordEncoder(new BCryptPasswordEncoder());
     }
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22998731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档