यह समाधान पासवर्ड प्रवाह के लिए काम करेगा और दूसरों के लिए मुझे यकीन नहीं है। आप ओएथ-सर्वर कॉन्फ़िगरेशन, में 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 का पालन कर सकते हैं।
धन्यवाद। यह मेरे बाहर है क्यों वसंत oauth2 के लेखकों ने सरल सफलता/असफल हैंडलर के साथ सिद्ध डिजाइन तोड़ दिया है जो अतीत में इतना अच्छा काम किया है। –