2014-07-05 7 views
0
@Service 
public class MyVoter implements AccessDecisionVoter<Entity> { 

    @Override 
    public boolean supports(ConfigAttribute attribute) {   
     boolean myBool = false; 
     return myBool; 
    } 

    @Override 
    public boolean supports(Class<?> clazz) { 
     return clazz == Project.class; 
    } 

    @Override 
    public int vote(Authentication authentication, Entity someEntity, 
      Collection<ConfigAttribute> config) { 
     return ACCESS_GRANTED; 
    } 
} 

첫 번째 지원 방법이 어떻게 작동 할 것이라고 설명 할 수 있습니까? myBool을 어떻게 변경하든 관계없이 항상 vote 메소드가 호출됩니다. 그것은 단지 지원 (클래스 clazz)이 invokation에 영향을 미친 것처럼 보입니다.스프링 보안 : AccessDecisionVoter

아이디어가 있으십니까?

편집 :

@Configuration 
@EnableWebMvcSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

@Autowired 
ApplicationContext context; 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http.csrf().disable(); 
    http 
     .authorizeRequests() 
      .antMatchers("/").permitAll() 
      .anyRequest().authenticated(); 
    http 
     .formLogin() 
      .loginPage("/login") 
      .permitAll()    
      .and() 
     .logout() 
      .permitAll(); 
} 

@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth 
     .inMemoryAuthentication() 
      .withUser("user").password("password").roles("USER"); 
} 

@Bean 
public AffirmativeBased accessDecisionManager() { 
    Map<String, AccessDecisionVoter> beans = context 
      .getBeansOfType(AccessDecisionVoter.class); 

    List<AccessDecisionVoter> decisionVoters = new ArrayList<>(
      beans.values()); 

    AffirmativeBased affirmativeBased = new AffirmativeBased(decisionVoters); 
    return affirmativeBased; 
} 
} 

이것은 기본적으로 내 유일한 설정입니다.

이 내가 AccessDecisionManager가 사용하는 방법이다 : 당신의 봄 보안 응용 프로그램 컨텍스트 구성없이

/* AUTHORIZATION */ 
    Authentication authentication = SecurityContextHolder.getContext() 
      .getAuthentication(); 

    Collection<ConfigAttribute> config = new HashSet<ConfigAttribute>(); 
    config.add(new SecurityConfig("Something")); 

    try { 
     adm.decide(authentication, project, config); 
    } catch (Exception e) { 
     // .. Exception Handling 
    } 

답변

3

을, 올바른 대답을하기 어렵다하지만 귀하의 질문에 대한 방법에 대한 자바 독은 다음과 상태;

Indicates whether this AccessDecisionVoter is able to vote on the 
passed ConfigAttribute. 

이 방법은 실제는 WebExpressionVoter

<security:http auto-config="true" use-expressions="true"> 
     <security:intercept-url pattern="/login*" 
      access="isAnonymous()" /> 
</security:http> 

또는의 구현입니다 RoleVoter 뭔가 WebExpressionVoter

<security:http auto-config="true" use-expressions="true"> 
     <security:intercept-url pattern="/admin/**" 
      access="ROLE_ADMIN" /> 
</security:http> 

"ROLE_ADMIN" 추천하고 RoleVoter에 대해 다음 "isAnonymous()" 같은 ConfigAttribute에 대한 호출 10. 위에서 언급 한대로 ConfigAttribute을 평가하려고하지 않는 한. 메서드가 호출되지 않으므로 true 또는 false을 반환해도 효과가 나타나지 않습니다. 희망이 도움이됩니다.

편집

당신이 AffirmativeBased AccessDecisionManager가의 decide 방법을 보면.

public void More ...decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) 
46   throws AccessDeniedException { 
47  int deny = 0; 
48 
49  for (AccessDecisionVoter voter : getDecisionVoters()) { 
50   int result = voter.vote(authentication, object, configAttributes); 
51 
52   if (logger.isDebugEnabled()) { 
53    logger.debug("Voter: " + voter + ", returned: " + result); 
54   } 
55 
56   switch (result) { 
57   case AccessDecisionVoter.ACCESS_GRANTED: 
58    return; 
59 
60   case AccessDecisionVoter.ACCESS_DENIED: 
61    deny++; 
62 
63    break; 
64 
65   default: 
66    break; 
67   } 
68  } 
69 
70  if (deny > 0) { 
71   throw new AccessDeniedException(messages.getMessage("AbstractAccessDecisionManager.accessDenied", 
72     "Access is denied")); 
73  } 
74 
75  // To get this far, every AccessDecisionVoter abstained 
76  checkAllowIfAllAbstainDecisions(); 
77 } 

supports(ConfigAttribute con) 방법을 전혀 사용하지 않습니다. 따라서 코드를 수정하여 아래의 내용이 제대로 작동하는지 확인해야합니다.

@Service 
public class MyVoter implements AccessDecisionVoter<Entity> { 

    @Override 
    public boolean supports(ConfigAttribute attribute) {   
     boolean myBool = false; 
     return myBool; 
    } 

    @Override 
    public boolean supports(Class<?> clazz) { 
     return clazz == Project.class; 
    } 

    @Override 
    public int vote(Authentication authentication, Entity someEntity, 
      Collection<ConfigAttribute> config) { 
     if(supports(config)) { // Add this check 
      return ACCESS_GRANTED; 
     } else { 
      return ACCESS_DENIED; // Abstain Based on your requirement 
     } 
    } 
} 
+0

안녕하세요. Java 구성 만 사용하고 있으며 Spring에 대해 상당히 익숙하므로 답변을 이해하지 못합니다. 나는 스프링 부트를 사용하고 액세스 결정 관리자 만 구성했습니다. 액세스 결정 관리자가 내 컨트롤러에서 사용자가 개체에 액세스 할 권한이 있는지 여부를 결정하는 방법을 사용하고 있습니다. void 결정 (인증 인증, Object 객체, Collection configAttributes) configAttributes가 유권자에게 어떤 영향을 미쳤다 고 생각합니다 ....? –

+0

Java 구성보기 및 컨트롤러에서 AccessDecisionManager를 사용하는 방법에 대해서는 더 자세히 대답하기가 어렵습니다. – shazin

+0

내 질문을 편집했습니다. 희망이 당신이 나를 도울 수 있습니다 :) –