Commit ba192c5a authored by Quxl's avatar Quxl

x

parent 4842e714
package com.egolm.sso.oauth;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
......@@ -12,7 +10,6 @@ import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest.AuthenticationRequestBuilder;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder;
import org.apache.oltu.oauth2.client.response.OAuthAuthzResponse;
import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse;
......@@ -33,10 +30,6 @@ public interface OAuthApi {
final String OAUTH_TOKEN_SESSION = "OAUTH2_TOKEN_SESSION_KEY";
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println(URLDecoder.decode("https://identity-stg.schneider-electric.com/accessmanager/oauth2/authorize?scope=openid%2Bprofile&response_type=code&realm=%2Fse&redirect_uri=http%3A%2F%2F25d733423o.qicp.vip%2FdoLogin&client_id=clientId", "utf-8"));
}
default boolean isLogin() {
HttpServletRequest request = this.getHttpServletRequest();
HttpSession session = request.getSession();
......@@ -48,15 +41,9 @@ public interface OAuthApi {
try {
HttpServletResponse response = this.getHttpServletResponse();
OAuthConfig config = this.getOAuthConfig();
AuthenticationRequestBuilder builder = OAuthClientRequest.authorizationLocation(config.getAuthorizeUrl());
builder.setResponseType(OAuth.OAUTH_CODE);
builder.setClientId(config.getClientId());
builder.setRedirectURI(config.getRediretUrl());
System.out.println(config.getScope());
builder.setScope(config.getScope());
builder.setParameter("realm", config.getRealm());
OAuthClientRequest oauthResponse = builder.buildQueryMessage();
response.sendRedirect(oauthResponse.getLocationUri());
UrlBuilder split = new UrlBuilder(config.getAuthorizeUrl()).setParameter("scope", config.getScope()).setParameter("response_type", OAuth.OAUTH_CODE).setParameter("realm", config.getRealm()).setParameter("client_id", config.getClientId()).setParameter("redirect_uri", config.getRediretUrl());
String urlString = split.toUrlString();
response.sendRedirect(urlString);
} catch (Exception e) {
throw new OAuthApiException(e.getMessage(), e);
}
......
package com.egolm.sso.oauth;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.TreeMap;
public class UrlBuilder {
private String url;
private Map<String, String> parameters = new TreeMap<String, String>();
private Map<String, String> headers = new TreeMap<String, String>();
public UrlBuilder(String url) {
this.url = url;
}
public UrlBuilder setParameter(String key, String value) {
parameters.put(key, value);
return this;
}
public UrlBuilder setHeader(String key, String value) {
headers.put(key, value);
return this;
}
public Map<String, String> getHeaders() {
return headers;
}
public String toUrlString() throws UnsupportedEncodingException {
StringBuffer sb = new StringBuffer(url);
if (!url.contains("?")) {
sb.append("?");
} else {
sb.append("&");
}
for (String key : parameters.keySet()) {
String value = parameters.get(key);
if (value == null) {
value = "";
}
value = value.trim();
sb.append(key).append("=").append(URLEncoder.encode(value, "utf-8")).append("&");
}
String urlString = sb.toString();
if (urlString.endsWith("&")) {
urlString = urlString.substring(0, urlString.length() - 1);
}
return urlString;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment