2015-03-30 7 views
9

क्या वसंत-ओथ 2 का उपयोग करके लॉगिन सफलता हैंडलर जोड़ने का कोई तरीका है?spring-oauth2 लॉगिन सफलता हैंडलर

मैंने मूल प्रमाणीकरण फ़िल्टर का उपयोग करने का प्रयास किया लेकिन यह केवल क्लाइंट प्रमाण-पत्रों को फ़िल्टर करता है जो उपयोगकर्ता प्रमाण-पत्र नहीं हैं।

या मुझे कस्टम उपयोगकर्ता प्रमाणीकरण प्रबंधक बनाने की आवश्यकता है?

TIA

उत्तर

0

हम एक कस्टम प्रमाणीकरण प्रबंधक जो हम OAuth2AuthenticationProcessingFilter इस किया पाने के लिए में तार का निर्माण किया। प्रबंधक की प्रमाणीकरण विधि प्रमाणीकरण प्रिंसिपल से OAuth2 प्रमाणीकरण और OAuth2 प्रमाणीकरण विवरण को अनपैक करने में सक्षम है।

<bean id="oAuth2AuthenticationManager" class="org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager"> 
    <property name="resourceId" value="XXX-api"/> 
    <property name="tokenServices" ref="tokenServices"/> 
</bean> 

<bean id="resourceServerFilter" 
     class="org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter"> 
    <property name="authenticationManager" ref="oAuth2AuthenticationManager"/> 
    <property name="tokenExtractor"> 
     <bean class="com.xxx.oauth.BearerTokenExtractor"/> 
    </property> 
</bean> 
5

यह समाधान पासवर्ड प्रवाह के लिए काम करेगा और दूसरों के लिए मुझे यकीन नहीं है। आप ओएथ-सर्वर कॉन्फ़िगरेशन, में http टैग में "पहले = BASIC_AUTH_FILTER" की स्थिति में अपना कस्टम फ़िल्टर जोड़ सकते हैं और आप "ओथ/टोकन" की पार्स प्रतिक्रिया से प्राप्त कर सकते हैं, इसलिए प्रतिक्रिया प्राप्त करने के लिए बाइटएरे रेस्पॉन्सवापर बनाएं, यहां I "org.apache.commons कॉमन्स-कब" से TeeOutputStream वर्ग का उपयोग कर रहा,

private class ByteArrayResponseWrapper extends HttpServletResponseWrapper { 

    public ByteArrayResponseWrapper(ServletResponse response) { 
     super((HttpServletResponse) response); 
    } 

    private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

    @Override 
    public ServletOutputStream getOutputStream() throws IOException { 
     return new DelegatingServletOutputStream(new TeeOutputStream(
       super.getOutputStream(), byteArrayOutputStream)); 
    } 

    public byte[] getByteArray() { 
     return this.byteArrayOutputStream.toByteArray(); 
    } 
} 

और मैं टोकन निकालने बनाया है के बाद की तरह अपने फिल्टर ओवरराइड doFilter बनाने

public class OAuth2AccessTokenExtractor implements 
    OAuth2AccessTokenExtractor { 

private ObjectMapper mapper = new ObjectMapper(); 

public String getAccessTokenValue(byte[] response) { 
    try { 
     return mapper.readValue(response, OAuth2AccessToken.class) 
       .getValue(); 
    } catch (JsonParseException e) { 
     e.printStackTrace(); 
    } catch (JsonMappingException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

} 

ACCESS_TOKEN निकालने की कोड को अलग करने यह

private DefaultTokenServices tokenServices; 

private OAuth2AccessTokenExtractor tokenExtractor; 

@Override 
public void doFilter(ServletRequest request, ServletResponse response, 
     FilterChain chain) throws IOException, ServletException { 

    // create wrapper to read response body 
    ByteArrayResponseWrapper responseWraper = new ByteArrayResponseWrapper(
      response); 

    // led them go 
    chain.doFilter(request, responseWraper); 

    // get ClientAuthentication 
    Authentication clientAuthentication = SecurityContextHolder 
      .getContext().getAuthentication(); 

    // is authenticated or not to proceed 
    if (clientAuthentication != null 
      && clientAuthentication.isAuthenticated()) { 

     // callBack client authenticated successfully 
     onSuccessfulClientAuthentication(request, response, 
       clientAuthentication); 

     // check response status is success of failure 
     if (responseWraper.getStatus() == 200) { 

      // extract accessToken from response 
      String token = tokenExtractor 
        .getAccessTokenValue(responseWraper.getByteArray()); 

      if (token != null && !token.isEmpty()) { 

       // load authentication from token 
       OAuth2Authentication oAuth2Authentication = this.tokenServices 
         .loadAuthentication(token); 
       OAuth2AccessToken actualAccessToken = this.tokenServices 
         .getAccessToken(oAuth2Authentication); 

       // callBack user authenticated successfully 
       onSuccessfulUserAuthentication(request, response, 
         clientAuthentication, oAuth2Authentication, 
         actualAccessToken); 
      } else { 
       log.error("access token is empty from extractor"); 
      } 
     } else { 
      // callBack user authenticated failure 
      onFailureUserAuthentication(request, response, 
        clientAuthentication, request.getParameter("username")); 
     } 
    } else { 
     // callBack client authenticated failure 
     onFailClientAuthentication(request, response, 
       request.getParameter(OAuth2Utils.CLIENT_ID)); 
    } 
} 

protected void onSuccessfulClientAuthentication(ServletRequest request, 
     ServletResponse response, Authentication authentication) { 
} 

protected void onFailClientAuthentication(ServletRequest request, 
     ServletResponse response, String clientId) { 
} 

protected void onSuccessfulUserAuthentication(ServletRequest request, 
     ServletResponse response, Authentication clientAuthentication, 
     OAuth2Authentication userOAuth2Authentication, 
     OAuth2AccessToken token) { 
} 

protected void onFailureUserAuthentication(ServletRequest request, 
     ServletResponse response, Authentication clientAuthentication, 
     String username) { 
} 

फ़िल्टर उदाहरण इंजेक्शन टोकन सर्विसेज बनाते समय। अब onSuccessfulClientAuthentication, onFailClientAuthentication, onSuccessfulUserAuthentication और onFailureUserAuthentication अपने प्रमाणीकरण के अनुसार बुलाया जाएगा

अधिक आप पर github

संपादित इस कोड का उल्लेख कर सकते के लिए

:

ऊपर टुकड़ा ठीक काम करता है जब आप डिफ़ॉल्ट है टोकन प्रतिक्रिया और यह केवल ServletResponseWrapper और निकालने का उपयोग करता है। लेकिन फिर भी यह कमजोर की तरह लगता है तो आप द्वारा org.springframework.security.oauth2.provider.token.TokenEnhancer वर्ग के माध्यम से

उपयोगकर्ता प्रमाणीकरण सफलता पता करने के लिए जानकारी के लिए इस answer का पालन कर सकते हैं।

+0

धन्यवाद। यह मेरे बाहर है क्यों वसंत oauth2 के लेखकों ने सरल सफलता/असफल हैंडलर के साथ सिद्ध डिजाइन तोड़ दिया है जो अतीत में इतना अच्छा काम किया है। –

संबंधित मुद्दे