首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >configure(HttpSecurity http)和configure(AuthenticationManagerBuilder)会被WebSecurityConfigurerAdapter中的某些注释忽略

configure(HttpSecurity http)和configure(AuthenticationManagerBuilder)会被WebSecurityConfigurerAdapter中的某些注释忽略
EN

Stack Overflow用户
提问于 2021-03-02 08:02:20
回答 1查看 62关注 0票数 0

我正在尝试保护我的spring应用程序,它具有不同的用户角色。虽然设置了身份验证部分,并且工作正常,但在实现授权部分的过程中,我意识到SecurityConfiguration extends WebSecurityConfigurerAdapter class中的两个覆盖方法之一的某些注释被忽略了。

代码语言:javascript
复制
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

  @Autowired
  private WebApplicationContext applicationContext;
  private CredentialsService userDetailsService;

  @Autowired
  private DataSource dataSource;

  @PostConstruct
  public void completeSetup() {
    userDetailsService =   applicationContext.getBean(CredentialsService.class);
  }

  @Override
  protected void configure(final HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/login")
            .permitAll()
            .and()
            .formLogin()
            .permitAll()
            .and()

            .httpBasic()
            .disable()

            .authorizeRequests()
            .antMatchers("/admin", "/admin/**")
            .hasRole("ADMIN")
            .and()

            .authorizeRequests()
            .antMatchers("/employee", "/employee/**")
            .hasRole("EMPLOYEE")
            .and()

            .authorizeRequests()
            .antMatchers("/customer", "/customer/**")
            .hasRole("CUSTOMER");
  }

  @Override
  protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService)
            .passwordEncoder(encoder())
            .and()
            .authenticationProvider(authenticationProvider())
            .jdbcAuthentication()
            .dataSource(dataSource);
  }


  @Bean
  public DaoAuthenticationProvider authenticationProvider() {
    final DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
    authProvider.setUserDetailsService(userDetailsService);
    authProvider.setPasswordEncoder(encoder());
    return authProvider;
  }

  @Bean
  public PasswordEncoder encoder() {
    return new BCryptPasswordEncoder(12);
  }
}

现在的问题如下所示,这个类对我的用户进行身份验证,但有一个主要缺点:

代码语言:javascript
复制
configure(final HttpSecurity http) throws Exception {

完全被忽略了。

另一方面,如果我在类的顶部添加@Configuration注释,

代码语言:javascript
复制
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {

被完全忽略,因此将破坏授权,因为它将无法在我的自定义UserDetailsService实现上调用getUsername()和getPassword。

如您所见,我使用了一个DaoAuthenticationProvider实例作为authenticationProvider,因为我的应用程序从外部数据库检索用户/密码。

我现在采用的快速修复方法是在我的主类上添加以下方法

代码语言:javascript
复制
@EnableGlobalMethodSecurity(
        securedEnabled = true,
        jsr250Enabled = true,
        prePostEnabled = true
)

以及在我的受限控制器上使用@Secured注释。这是可行的,但我想了解为什么Spring有如此奇怪的行为,以及我可以采取什么步骤来解决这些问题。

EN

回答 1

Stack Overflow用户

发布于 2021-03-02 19:34:15

由于要将角色分配给用户,因此请使用以下语法

代码语言:javascript
复制
.antMatchers("/admin", "/admin/**")
.hasRole("ADMIN")

代码语言:javascript
复制
.antMatchers("/admin", "/admin/**")
.hasAuthority("ROLE_ADMIN")

角色只是存储为带有"ROLE_“前缀的权限。

因此,角色"ADMIN“相当于权限"ROLE_ADMIN”。

编辑1

您还可以简化您的配置,以清楚地了解所有内容的来源。

由于您的UserDetailsService (CredentialsService)已经是一个bean,Spring Security将自动拾取它。

代码语言:javascript
复制
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

  // The password encoder should be a bean
  @Bean
  public PasswordEncoder encoder() {
    return new BCryptPasswordEncoder(12);
  }

  @Override
  protected void configure(final HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/login")
            .permitAll()
            .and()
            .formLogin()
            .permitAll()
            .and()

            .authorizeRequests()
            .antMatchers("/admin", "/admin/**")
            .hasRole("ADMIN")
            .and()

            .authorizeRequests()
            .antMatchers("/manager", "/manager/**")
            .hasRole("MANAGER")
            .and()

            .authorizeRequests()
            .antMatchers("/customer", "/customer/**")
            .hasRole("CUSTOMER");
  }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66431422

复制
相关文章

相似问题

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