Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
S
sso
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
曲欣亮
sso
Commits
4f13a3f5
Commit
4f13a3f5
authored
Jul 24, 2019
by
Quxl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
x
parent
4f6c943d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
173 additions
and
0 deletions
+173
-0
AccessToken.java
src/main/java/com/egolm/sso/api/AccessToken.java
+51
-0
Auth2Api.java
src/main/java/com/egolm/sso/api/Auth2Api.java
+62
-0
OAuthConfig.java
src/main/java/com/egolm/sso/api/OAuthConfig.java
+60
-0
No files found.
src/main/java/com/egolm/sso/api/AccessToken.java
0 → 100644
View file @
4f13a3f5
package
com
.
egolm
.
sso
.
api
;
public
class
AccessToken
{
private
String
accessToken
;
private
String
refreshToken
;
private
String
idToken
;
private
String
tokenType
;
private
Long
expiresIn
;
public
String
getAccessToken
()
{
return
accessToken
;
}
public
void
setAccessToken
(
String
accessToken
)
{
this
.
accessToken
=
accessToken
;
}
public
String
getRefreshToken
()
{
return
refreshToken
;
}
public
void
setRefreshToken
(
String
refreshToken
)
{
this
.
refreshToken
=
refreshToken
;
}
public
String
getIdToken
()
{
return
idToken
;
}
public
void
setIdToken
(
String
idToken
)
{
this
.
idToken
=
idToken
;
}
public
String
getTokenType
()
{
return
tokenType
;
}
public
void
setTokenType
(
String
tokenType
)
{
this
.
tokenType
=
tokenType
;
}
public
Long
getExpiresIn
()
{
return
expiresIn
;
}
public
void
setExpiresIn
(
Long
expiresIn
)
{
this
.
expiresIn
=
expiresIn
;
}
}
src/main/java/com/egolm/sso/api/Auth2Api.java
0 → 100644
View file @
4f13a3f5
package
com
.
egolm
.
sso
.
api
;
import
java.io.IOException
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
org.apache.oltu.oauth2.client.OAuthClient
;
import
org.apache.oltu.oauth2.client.URLConnectionClient
;
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
;
import
org.apache.oltu.oauth2.common.OAuth
;
import
org.apache.oltu.oauth2.common.exception.OAuthProblemException
;
import
org.apache.oltu.oauth2.common.exception.OAuthSystemException
;
import
org.apache.oltu.oauth2.common.message.types.GrantType
;
public
interface
Auth2Api
{
OAuthConfig
getOAuthConfig
();
default
void
toLoginPage
(
HttpServletResponse
response
)
throws
IOException
,
OAuthSystemException
{
OAuthConfig
config
=
this
.
getOAuthConfig
();
AuthenticationRequestBuilder
builder
=
OAuthClientRequest
.
authorizationLocation
(
config
.
getAuthorizeUrl
());
builder
.
setResponseType
(
OAuth
.
OAUTH_CODE
);
builder
.
setClientId
(
config
.
getClientId
());
builder
.
setRedirectURI
(
config
.
getRediretUrl
());
builder
.
setScope
(
config
.
getClientScope
());
OAuthClientRequest
oauthResponse
=
builder
.
buildQueryMessage
();
response
.
sendRedirect
(
oauthResponse
.
getLocationUri
());
}
default
AccessToken
getToken
(
HttpServletRequest
request
)
throws
OAuthProblemException
,
OAuthSystemException
{
OAuthConfig
config
=
this
.
getOAuthConfig
();
OAuthAuthzResponse
oauthAuthzResponse
=
OAuthAuthzResponse
.
oauthCodeAuthzResponse
(
request
);
String
code
=
oauthAuthzResponse
.
getCode
();
TokenRequestBuilder
builder
=
OAuthClientRequest
.
tokenLocation
(
config
.
getAccessTokenUrl
());
builder
.
setGrantType
(
GrantType
.
AUTHORIZATION_CODE
);
builder
.
setClientId
(
config
.
getClientId
());
builder
.
setClientSecret
(
config
.
getClientSecret
());
builder
.
setRedirectURI
(
config
.
getRediretUrl
());
builder
.
setCode
(
code
);
OAuthClientRequest
oauthClientRequest
=
builder
.
buildQueryMessage
();
OAuthClient
oAuthClient
=
new
OAuthClient
(
new
URLConnectionClient
());
OAuthJSONAccessTokenResponse
oAuthResponse
=
oAuthClient
.
accessToken
(
oauthClientRequest
,
OAuth
.
HttpMethod
.
POST
);
String
accessToken
=
oAuthResponse
.
getAccessToken
();
String
refreshToken
=
oAuthResponse
.
getRefreshToken
();
Long
expiresIn
=
oAuthResponse
.
getExpiresIn
();
String
idToken
=
oAuthResponse
.
getParam
(
"id_token"
);
String
tokenType
=
oAuthResponse
.
getTokenType
();
AccessToken
tokenObj
=
new
AccessToken
();
tokenObj
.
setAccessToken
(
accessToken
);
tokenObj
.
setRefreshToken
(
refreshToken
);
tokenObj
.
setIdToken
(
idToken
);
tokenObj
.
setTokenType
(
tokenType
);
tokenObj
.
setExpiresIn
(
expiresIn
);
return
tokenObj
;
}
}
src/main/java/com/egolm/sso/api/OAuthConfig.java
0 → 100644
View file @
4f13a3f5
package
com
.
egolm
.
sso
.
api
;
public
class
OAuthConfig
{
private
String
clientId
;
private
String
clientSecret
;
private
String
clientScope
;
private
String
rediretUrl
;
private
String
authorizeUrl
;
private
String
accessTokenUrl
;
public
String
getClientId
()
{
return
clientId
;
}
public
void
setClientId
(
String
clientId
)
{
this
.
clientId
=
clientId
;
}
public
String
getClientSecret
()
{
return
clientSecret
;
}
public
void
setClientSecret
(
String
clientSecret
)
{
this
.
clientSecret
=
clientSecret
;
}
public
String
getClientScope
()
{
return
clientScope
;
}
public
void
setClientScope
(
String
clientScope
)
{
this
.
clientScope
=
clientScope
;
}
public
String
getAuthorizeUrl
()
{
return
authorizeUrl
;
}
public
void
setAuthorizeUrl
(
String
authorizeUrl
)
{
this
.
authorizeUrl
=
authorizeUrl
;
}
public
String
getAccessTokenUrl
()
{
return
accessTokenUrl
;
}
public
void
setAccessTokenUrl
(
String
accessTokenUrl
)
{
this
.
accessTokenUrl
=
accessTokenUrl
;
}
public
String
getRediretUrl
()
{
return
rediretUrl
;
}
public
void
setRediretUrl
(
String
rediretUrl
)
{
this
.
rediretUrl
=
rediretUrl
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment