【问题标题】:Spring boot with security annotation is not took in consideration不考虑带有安全注释的 Spring Boot
【发布时间】:2018-03-24 11:10:24
【问题描述】:

我使用带有 spring security 的 spring boot 2。

我将安全性拆分为 rest 和 mvc。

@EnableWebSecurity
public class MultiHttpSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

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

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Configuration
    @Order(1)
    public class RestWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .antMatcher("/rest/**")
                    .authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                    .httpBasic().and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().csrf().disable();
        }
    }

    @Configuration
    @Order(2)
    public class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .authorizeRequests()
                    .antMatchers("/css/**",  "/js/**", "/img/**", "/").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin().loginPage("/login").permitAll().successHandler(new CustomAuthenticationSuccessHandler())
                    .and()
                    .logout()
                    .logoutUrl("/logout")
                    .logoutSuccessHandler(new CustomLogoutHandler())
                    .and().csrf().disable();
        }
    }
}

我在 db 中的角色是

超级用户、管理员、集成商。

在我的一个休息控制器中,我放了

@Secured("hasRole('user')")

我的应用程序中不存在此角色。

我尝试使用具有以下角色的用户:超级用户和集成商,并且成功了...

同样的事情

@PreAuthorize("hasAuthority('user')")

还有其他配置吗?

【问题讨论】:

标签: spring-boot spring-security


【解决方案1】:

为了保护您的方法,您必须使用@EnableGlobalMethodSecurity 注释启用方法安全

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class HelloMethodSecurityConfig {

    @Bean
    public MethodSecurityService methodSecurityService() {
        return new MethodSecurityServiceImpl(); //Class managed by Spring
    }

    @Autowired
    public void registerGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
          .inMemoryAuthentication()
          .withUser("user").password("password").roles("USER").and()
          .withUser("admin").password("password").roles("USER", "ADMIN");
    }

}

通过上述最小配置,MethodSecurityService 类的方法现在可以通过方法安全性得到保护。

如需更多自定义方法安全性,您需要扩展GlobalMethodSecurityConfiguration

查看官方docs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-05
    • 2021-02-09
    • 2017-12-02
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 2017-02-20
    • 2014-03-09
    相关资源
    最近更新 更多