복붙노트

[SPRING] XML 파일없이 Spring ACL을 설정하는 방법

SPRING

XML 파일없이 Spring ACL을 설정하는 방법

내 서버에 ACL 기능을 추가하려고합니다. 나는 자바 파일을 사용하여 스프링 보안을 설정했으며 같은 방식으로 ACL을 추가하고 싶다. 어떻게해야합니까? 내가 찾은 모든 자습서는 XML 파일을 사용했습니다.

SecurityInit :

@Order(1)
public class SecurityInitializer extends AbstractSecurityWebApplicationInitializer {
}

SecurityConfig

@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
@Component
@ComponentScan(basePackages = {"test.package"})
public class SecurityConfig extends 

WebSecurityConfigurerAdapter {

...
    @Autowired
    protected void registerAuthentication(UserDetailsService userDetailsService, AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

//  http://stackoverflow.com/a/21100458/162345
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .headers().disable()
                .addFilterBefore(...)
                .addFilterBefore(...)

//                TODO: create a better way to differentiate login to signup
                .exceptionHandling()
                    .authenticationEntryPoint(noRedirectForAnonymous)
                    .and()

                .formLogin()
                    .successHandler(restAuthenticationSuccessHandler)
                    .failureHandler(restAuthenticationFailureHandler)
                    .and()

                .logout()
                    .logoutSuccessHandler(noRedirectLogoutSuccessHandler)
                    .and()

                .authorizeRequests()
                    .antMatchers("/api/keywords/**").permitAll()
                    .antMatchers("/api/**").authenticated();
    }
}

해결법

  1. ==============================

    1.xml 파일없이 spring acl을 구성 할 수있는 방법은 없습니다. 이것은 스프링 문서 자체에 언급되어 있습니다. 스프링 문서를 참조하십시오.

    xml 파일없이 spring acl을 구성 할 수있는 방법은 없습니다. 이것은 스프링 문서 자체에 언급되어 있습니다. 스프링 문서를 참조하십시오.

  2. ==============================

    2.다음과 같이 Java 구성 클래스를 사용하여 spring acl을 구성 할 수 있습니다.

    다음과 같이 Java 구성 클래스를 사용하여 spring acl을 구성 할 수 있습니다.

    @Configuration
    @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
    public class ACLConfig extends GlobalMethodSecurityConfiguration {
    
    @Autowired
    DataSource dataSource;
    
    EhCacheBasedAclCache aclCache() {
        EhCacheFactoryBean factoryBean = new EhCacheFactoryBean();
        EhCacheManagerFactoryBean cacheManager = new EhCacheManagerFactoryBean();
    
        factoryBean.setName("aclCache");
        factoryBean.setCacheManager(cacheManager.getObject());
        return new EhCacheBasedAclCache(factoryBean.getObject());
    }
    
    
    LookupStrategy lookupStrategy() {
        return new BasicLookupStrategy(dataSource, aclCache(), aclAuthorizationStrategy(), new ConsoleAuditLogger());
    }
    
    
    AclAuthorizationStrategy aclAuthorizationStrategy() {
        return new AclAuthorizationStrategyImpl(new SimpleGrantedAuthority("ROLE_ACL_ADMIN"),
            new SimpleGrantedAuthority("ROLE_ACL_ADMIN"),
            new SimpleGrantedAuthority("ROLE_ACL_ADMIN"));
    }
    
    @Bean
    JdbcMutableAclService aclService() {
        JdbcMutableAclService service = new JdbcMutableAclService(dataSource, lookupStrategy(), aclCache());
        service.setClassIdentityQuery("select currval(pg_get_serial_sequence('acl_class', 'id'))");
        service.setSidIdentityQuery("select currval(pg_get_serial_sequence('acl_sid', 'id'))");
        return service;
    }
    
    @Bean
    AclMasterService masterService() {
        return new AclMasterService();
    }
    
    @Override
    protected MethodSecurityExpressionHandler createExpressionHandler(){
        DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
        expressionHandler.setPermissionEvaluator(new AclPermissionEvaluator(aclService()));
        return expressionHandler;
    }
    }
    

    구성의 중요한 측면은 다음과 같습니다.

    메소드를 오버라이드하다

    클래스의 시작 부분에서 follow annotation을 사용하여 Pre 및 Post 주석을 활성화 할 수 있습니다.

    이제 다음과 같은 특수 효과를 사용할 수 있습니다.

    @Pre과 @Post anotations의 더 많은 사용법에 대해서는 Spring Security의 Contact 샘플이나 스프링 보안 참조 가이드를 참조하십시오. 이 구성 클래스는 Spring 4, Spring Security 4.0.1 및 Spring Security ACL 3.1.2에서 테스트되었습니다. 인증을 구성하려면 다른 Java 클래스를 사용하거나이 메소드에서 구성 메소드를 대체 할 수 있습니다. 이미 구성된 ehcache가있는 경우이 구성은 ehcache가 싱글 톤 클래스이므로 올바르게 작동하지 않을 수 있으며이 구성은 새 구성을 만들려고 시도합니다.

  3. from https://stackoverflow.com/questions/26292431/how-to-configure-spring-acl-without-xml-file by cc-by-sa and MIT license