com.flyfish.oauth.entry.CodeAuthenticationEntryPoint Maven / Gradle / Ivy
package com.flyfish.oauth.entry;
import com.flyfish.oauth.client.OAuth2SsoClient;
import com.flyfish.oauth.client.RestClient;
import com.flyfish.oauth.configuration.OAuth2SsoProperties;
import com.flyfish.oauth.domain.OAuthSSOToken;
import com.flyfish.oauth.utils.JacksonUtil;
import com.flyfish.oauth.utils.OAuth2Utils;
import com.flyfish.oauth.utils.OAuthRequestParser;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
/**
* 授权码模式接入点
*
* @author wangyu
*/
public class CodeAuthenticationEntryPoint extends AbstractAuthenticationEntryPoint {
private OAuth2SsoProperties properties;
public CodeAuthenticationEntryPoint(OAuth2SsoClient client) {
this.properties = client.getProperties();
}
@Override
public void configure(OAuth2SsoProperties properties) {
this.properties = properties;
}
@Override
public OAuth2SsoProperties getProperties() {
return properties;
}
/**
* 获取AccessToken
*
* @return 结果
*/
@Override
public OAuthSSOToken getClientToken() {
return null;
}
@Override
public OAuthSSOToken getAccessToken(String code, String grantType) {
try {
String result = RestClient.create()
.url(properties.getAccessTokenUri())
.addParam(OAuth2Utils.CLIENT_ID, properties.getClientId())
.addParam(OAuth2Utils.CLIENT_SECRET, properties.getClientSecret())
.addParam(OAuth2Utils.CODE, code)
.addParam(OAuth2Utils.GRANT_TYPE, grantType)
.build()
.executeForString();
return JacksonUtil.fromJson(result, OAuthSSOToken.class);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 刷新当前已经失效的token,使用refreshToken
*
* @param refreshToken 刷新token
* @return 结果
*/
@Override
public OAuthSSOToken refreshAccessToken(String refreshToken) {
return null;
}
@Override
public String redirectUrl(OAuthRequestParser context) {
try {
return properties.getLoginUri(context.getLocation(properties.getLocalUrl()));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return properties.getLocalUrl();
}
/**
* 登出。触发登出操作
*
* @param request 请求
* @return 结果
*/
@Override
public boolean logout(HttpServletRequest request) {
return false;
}
}