如何实现Spring Method级安全性

时间:2018-06-22 04:04:17

标签: java spring spring-security spring-aop

我想实现Spring Method级的安全性。为此,我要做的是

@Configuration
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig  extends WebSecurityConfigurerAdapter{
@Autowired
private AccessDeniedHandler accessDeniedHandler;

@Autowired
private AdminPortalAuthProvider authProvider;

@Autowired
private AdminWebAuthDetailsSource authenticationDetailsSource;

@Autowired
AdminAuthSuccessHandler successHandler;

@Override
protected void configure(HttpSecurity http) throws Exception {

    http
    .csrf().csrfTokenRepository(csrfTokenRepository()).and()
        .antMatcher("/admin/**")
        .authorizeRequests().antMatchers("/admin/registration","/admin/setPassword").anonymous()
        .antMatchers("/admin/setPass").permitAll()
        .anyRequest().authenticated()
        .and()
        .formLogin()
            .authenticationDetailsSource(authenticationDetailsSource)
            .usernameParameter("username")
            .passwordParameter("password")
            .loginPage("/admin/login")
            .defaultSuccessUrl("/admin/home")
            .successHandler(successHandler)
            .permitAll()
         .and()
            .logout()
            .logoutUrl("/admin/logout")
            .invalidateHttpSession(true)
            .permitAll()

         .and()
            .exceptionHandling().accessDeniedHandler(accessDeniedHandler)
        .and()
            .authenticationProvider(authProvider)
         .addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class)
         .addFilterAfter(new CustomSecurityFilter(),BasicAuthenticationFilter.class)
         .headers().xssProtection().and().frameOptions().sameOrigin()
        .cacheControl().and().contentTypeOptions();   

}


private CsrfTokenRepository csrfTokenRepository() {
    HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
    repository.setHeaderName("X-XSRF-TOKEN");
    return repository;
}

@Override
public void configure(WebSecurity web) throws Exception {
  web.ignoring().antMatchers("/js/**","/css/**","/img/**","/customjs/**","/fonts/**","/lib/**","/icons-reference/**","/admin/registration/**","/admin/customer/payment-invoice-pg-post");
}

}

我正在使用@PreAuthorize("hasRole('ROLE_ADMIN')"))

关于方法,但仍然无法正常工作...  有人可以帮我吗.....

  

因为我被要求将我的securityConfig代码放在所有与url安全有关的地方,所以我在这里编辑了问题...

2 个答案:

答案 0 :(得分:0)

我建议使用 GlobalMethodSecurityConfiguration 类尝试一次。

 @Configuration
 @EnableGlobalMethodSecurity(
  prePostEnabled = true)
 public class MethodSecurityConfig 
  extends GlobalMethodSecurityConfiguration {
 }

答案 1 :(得分:0)

在对代码和概念进行了深入研究之后,在我的前辈的帮助下,我得到了我的问题的解决方案...

伴随春天的方法级别的安全性 ...

第一 ,我发现我需要使用 AOP 的概念 因此,我在pom.xml中添加了必需的依赖项

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
   <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-aop</artifactId>
       <version>5.0.1.RELEASE</version>
   </dependency>

并将注释 @EnableAspectJAutoProxy 放在定义 @EnableGlobalMethodSecurity(prePostEnabled = true)的同一文件中。

仍然发现 @PreAuthorize 对代码没有影响。

然后 第二 ,我还有另一个根本原因是...

在我的项目中,tomcat的lib文件夹中提供了一些依赖项,这也是由于该项目在运行时得到了两个很少有依赖项的实例,所以存在冲突(,但这些冲突未反映在eclipse 中)。

所以我排除了pom.xml中的那些依赖项

结果是..... @PreAuthorize正常工作。