2016-11-04 6 views
0

요청으로부터 매개 변수를 전달해야하는 사용자 정의 인증 공급자를 만들었습니다.Spring Security - Oauth2 Pass 인증 요청자에게 매개 변수 요청

enpoint은 상기에서

/OAuth를/토큰? grant_type = 비밀번호 & 이름 = XXX & 암호 = XXX & CLIENT_ID = XXX & client_secret = XXX & 국가 = 내

입니다 요청 country 매개 변수를 캡처하여 사용자 지정 인증 공급자에게 전달해야합니다. 사용자 지정 인증 공급자는 사용자 이름, 암호 및 국가를 기반으로 사용자를 인증합니다.

위의 요청은 ClientCredentialsTokenEndpointFilter 필터를 통과하지만 국가 값을 요청에서 인증 개체로 설정할 수있는 방법이 없습니다.

<http pattern="/oauth/token" create-session="never" 
    authentication-manager-ref="clientAuthenticationManager" 
    xmlns="http://www.springframework.org/schema/security"> 
    <intercept-url pattern="/oauth/token" access="fullyAuthenticated" /> 
    <anonymous enabled="false" /> 
    <http-basic entry-point-ref="clientAuthenticationEntryPoint" />  
    <custom-filter ref="clientCredentialsTokenEndpointFilter" 
     after="BASIC_AUTH_FILTER" /> 
    <access-denied-handler ref="oauthAccessDeniedHandler" /> 
</http> 

보안 기능을 확장하여 구현할 수 있는지 알려주세요. 내가 Spring 4.1Spring Security 4.0.3RELEASE을 사용하고

감사합니다, 안와르

답변

0

당신은 UsernamePasswordCountryAuthenticationToken를 작성해야합니다.

public class UsernamePasswordCountryAuthenticationToken extends UsernamePasswordAuthenticationToken { 

    private String country; 

    public UsernamePasswordCountryAuthenticationToken(Object principal, Object credentials, String country, Collection<? extends GrantedAuthority> authorities) { 
     super(principal, credentials, country, authorities); 
    } 

    public UsernamePasswordCountryAuthenticationToken(Object principal, Object credentials, String country) { 
     super(principal, credentials, country); 
    } 

    public String getCountry() { 
     return country; 
    } 
} 

그리고 당신은 제대로 정의 AuthenticationProvider을 위해 AuthenticationManager를 구성한 경우 봄 보안 OAuth를 구성 파일 이제

<bean id="customResourceOwnerPasswordTokenGranter" class="CustomResourceOwnerPasswordTokenGranter"> 
    <constructor-arg index="0" ref="authenticationManager"/> 
    <constructor-arg index="1" ref="tokenServices"/> 
    <constructor-arg index="2" ref="clientDetailsService"/> 
</bean> 

<oauth:authorization-server ...> 
    <oauth:custom-grant token-granter-ref="customResourceOwnerPasswordTokenGranter" /> 
</oauth:authorization-server> 

에서 마지막으로 ResourceOwnerPasswordTokenGranter

public class CustomResourceOwnerPasswordTokenGranter extends AbstractTokenGranter { 

    private static final String GRANT_TYPE = "password"; 

    private final AuthenticationManager authenticationManager; 

    public CustomResourceOwnerPasswordTokenGranter(AuthenticationManager authenticationManager, 
     AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService) { 
     super(tokenServices, clientDetailsService, GRANT_TYPE); 
     this.authenticationManager = authenticationManager; 
    } 


    protected OAuth2Authentication getOAuth2Authentication(AuthorizationRequest clientToken) { 
     Map<String, String> parameters = clientToken.getAuthorizationParameters(); 
     String username = parameters.get("username"); 
     String password = parameters.get("password"); 
     String country = parameters.get("country"); 

     Authentication userAuth = new UsernamePasswordCountryAuthenticationToken(username, password, country); 
     try { 
      userAuth = authenticationManager.authenticate(userAuth); 
     } catch (AccountStatusException ase) { 
      //covers expired, locked, disabled cases (mentioned in section 5.2, draft 31) 
      throw new InvalidGrantException(ase.getMessage()); 
     } catch (BadCredentialsException e) { 
      // If the username/password are wrong the spec says we should send 400/bad grant 
      throw new InvalidGrantException(e.getMessage()); 
     } 

     if (userAuth == null || !userAuth.isAuthenticated()) { 
      throw new InvalidGrantException("Could not authenticate user: " + username); 
     } 

     return new OAuth2Authentication(clientToken, userAuth); 
    } 
} 

그리고를 오버라이드 (override), 당신이 될 것입니다을에서 AuthenticationProvider.authenticate method(Authentication auth)으로 전송하면됩니다.에서 UsernamePasswordCountryAuthenticationToken으로 변경하여 사용하십시오.

관련 문제