本文整理汇总了Java中org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl类的典型用法代码示例。如果您正苦于以下问题:Java OAuthIssuerImpl类的具体用法?Java OAuthIssuerImpl怎么用?Java OAuthIssuerImpl使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OAuthIssuerImpl类属于org.apache.oltu.oauth2.as.issuer包,在下文中一共展示了OAuthIssuerImpl类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: authorize
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl; //导入依赖的package包/类
private String authorize(HttpServletRequest request, String redirectURI, Authentication authentication) throws OAuthSystemException {
User principal = (User) authentication.getPrincipal();
if (hasAuthorized(principal)) {
// 生成code
String code = new OAuthIssuerImpl(new MD5Generator()).authorizationCode();
// 把授权码存入
oAuth2Service.addCode(code, principal.getOpenId());
// 构建oauth2授权返回信息
OAuthResponse oauthResponse = OAuthASResponse
.authorizationResponse(request, HttpServletResponse.SC_FOUND)
.setCode(code)
.location(redirectURI)
.buildQueryMessage();
return "redirect:" + oauthResponse.getLocationUri();
} else {
return "oauth2/authorize";
}
}
示例2: authorize
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl; //导入依赖的package包/类
@POST
@Path("/authz")
@RequiresRoles("agate-user")
public Response authorize(@Context HttpServletRequest servletRequest, @FormParam("grant") boolean grant)
throws URISyntaxException, OAuthSystemException {
return tryBuildResponse(servletRequest, (data) -> {
try {
if(!grant) {
return buildErrorResponse(OAuthProblemException.error("access_denied", "Owner denied authorization."), data.getRequest(), data.getRedirectUri());
}
OAuthIssuer oAuthIssuer = new OAuthIssuerImpl(new MD5Generator());
User user = userService.getCurrentUser();
Authorization authorization = authorizationService.find(user.getName(), data.getClientId());
if(authorization == null) {
authorization = new Authorization(user.getName(), data.getClientId());
}
authorization.setCode(oAuthIssuer.authorizationCode());
authorization.setScopes(data.getRequest().getScopes());
authorization.setRedirectURI(data.getRedirectUri());
authorizationService.save(authorization);
long expiresIn = authorizationService.getExpirationDate(authorization).getMillis() - DateTime.now().getMillis();
OAuthASResponse.OAuthAuthorizationResponseBuilder builder = OAuthASResponse
.authorizationResponse(servletRequest, HttpServletResponse.SC_FOUND) //
.setCode(authorization.getCode()) //
.setExpiresIn(expiresIn / 1000) //
.location(data.getRedirectUri());
setState(builder, data.getRequest());
OAuthResponse response = builder.buildQueryMessage();
return Response.status(response.getResponseStatus()).location(new URI(response.getLocationUri())).build();
} catch(URISyntaxException | OAuthSystemException e) {
throw Throwables.propagate(e);
}
});
}
示例3: saveOauthCode
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl; //导入依赖的package包/类
@Test
public void saveOauthCode() throws Exception {
final OAuthIssuerImpl oAuthIssuer = new OAuthIssuerImpl(new MD5Generator());
OauthCode oauthCode = new OauthCode()
.username("test")
.code(oAuthIssuer.authorizationCode())
.clientId("client+id");
oauthJdbcRepository.saveOauthCode(oauthCode);
final OauthCode oauthCode1 = oauthJdbcRepository.findOauthCode(oauthCode.code(), oauthCode.clientId());
assertNotNull(oauthCode1);
assertNotNull(oauthCode1.code());
assertNotNull(oauthCode1.username());
System.out.println(oauthCode1.code());
final OauthCode oauthCode2 = oauthJdbcRepository.findOauthCodeByUsernameClientId(oauthCode.username(), oauthCode.clientId());
assertNotNull(oauthCode2);
final int i = oauthJdbcRepository.deleteOauthCode(oauthCode);
assertEquals(i, 1);
final OauthCode oauthCode3 = oauthJdbcRepository.findOauthCodeByUsernameClientId("add", "ddood");
assertNull(oauthCode3);
}
示例4: buildAuthenticatedUserResponse
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl; //导入依赖的package包/类
private Response buildAuthenticatedUserResponse(UserVO userVO) throws OAuthSystemException {
OAuthIssuer oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());
OAuthResponse response = OAuthASResponse
.tokenResponse(HttpServletResponse.SC_OK)
.setAccessToken(oauthIssuerImpl.accessToken())
.setExpiresIn("3600")
.buildJSONMessage();
return Response.status(response.getResponseStatus()).entity(response.getBody()).build();
}
示例5: OAuth2Tokens
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl; //导入依赖的package包/类
/**
* Generate Constructor
* @return new OAuth2Tokens Object with newly generated tokens
* @throws Exception
*/
public OAuth2Tokens () throws Exception {
OAuthIssuer oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator() );
this.authorizationCode = oauthIssuerImpl.authorizationCode();
this.accessToken = oauthIssuerImpl.accessToken();
this.refreshToken = oauthIssuerImpl.refreshToken();
}
示例6: DefaultTokenGenerator
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl; //导入依赖的package包/类
public DefaultTokenGenerator() {
__issuer = new OAuthIssuerImpl(new MD5Generator());
}
示例7: preHandle
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl; //导入依赖的package包/类
@Override
protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
I18N i18n = new I18N(request.getLocale());
try {
OAuthTokenRequest oAuthRequest = new OAuthTokenRequest(httpRequest);
String clientId = oAuthRequest.getClientId();
// Check client id
if (!oAuthService.checkClient(clientId))
return ResponseUtils.processResponse(httpResponse, null,
ResponseUtils.responseInvalidClient(i18n.getString("INVALID_CLIENT_ID")));
// Check client secret
if (!oAuthService.checkClient(clientId, oAuthRequest.getClientSecret()))
return ResponseUtils.processResponse(httpResponse, null,
ResponseUtils.responseUnauthClient(i18n.getString("INVALID_CLIENT_SECRET")));
// Check grant data according to grant type
String grantType = oAuthRequest.getGrantType();
String authCode = oAuthRequest.getCode();
String refreshToken = oAuthRequest.getRefreshToken();
if (GrantType.AUTHORIZATION_CODE.toString().equals(grantType)) {
if (!oAuthService.checkAuthCode(authCode, clientId))
return ResponseUtils.processResponse(httpResponse, null,
ResponseUtils.responseInvalidGrant(i18n.getString("INVALID_AUTH_CODE")));
} else if (oAuthService.refreshTokenSupported()
&& GrantType.REFRESH_TOKEN.toString().equals(grantType)) {
if (!oAuthService.checkRefreshToken(refreshToken, clientId))
return ResponseUtils.processResponse(httpResponse, null,
ResponseUtils.responseInvalidGrant(i18n.getString("INVALID_REFRESH_CODE")));
} else {
return ResponseUtils.processResponse(httpResponse, null,
ResponseUtils.responseUnsuppGrant(i18n.getString("UNSUPPORT_GRANT_TYPE")));
}
// generate access token
OAuthIssuerImpl oAuthIssuer = new OAuthIssuerImpl(new MD5Generator());
String accessToken = oAuthIssuer.accessToken();
if (GrantType.AUTHORIZATION_CODE.toString().equals(grantType))
oAuthService.addAcessToken(accessToken, authCode);
else
oAuthService.refreshAccessToken(accessToken, refreshToken);
// generate refresh token
refreshToken = null;
if (oAuthService.refreshTokenSupported()) {
refreshToken = oAuthIssuer.refreshToken();
oAuthService.addRefreshToken(refreshToken, accessToken);
}
// generate page content
String expireIn = String.valueOf(oAuthService.getExpireIn(accessToken));
return ResponseUtils.processResponse(httpResponse, null,
OAuthASResponse.tokenResponse(HttpServletResponse.SC_OK).setExpiresIn(expireIn)
.setAccessToken(accessToken).setRefreshToken(refreshToken));
} catch (OAuthProblemException ex) {
if (OAuthUtils.isEmpty(ex.getError()))
return ResponseUtils.processResponse(httpResponse, null,
ResponseUtils.responseInvalidRequest(ex.getDescription()));
return ResponseUtils.processResponse(httpResponse, null,
ResponseUtils.responseBadRequest(ex));
}
}
示例8: token
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl; //导入依赖的package包/类
@RequestMapping("/accessToken")
public HttpEntity<String> token(HttpServletRequest request) throws URISyntaxException, OAuthSystemException {
try {
// 构建OAuth请求
OAuthTokenRequest oauthRequest = new OAuthTokenRequest(request);
// 检查提交的客户端id是否正确
if (!oAuthService.checkClientId(oauthRequest.getClientId())) {
return buildInvalidClientIdResponse();
}
// 检查客户端安全KEY是否正确
if (!oAuthService.checkClientSecret(oauthRequest.getClientSecret())) {
return buildInvalidClientSecretResponse();
}
String authCode = oauthRequest.getParam(OAuth.OAUTH_CODE);
// 检查验证类型,此处只检查AUTHORIZATION_CODE类型,其他的还有PASSWORD或REFRESH_TOKEN
if (oauthRequest.getParam(OAuth.OAUTH_GRANT_TYPE).equals(GrantType.AUTHORIZATION_CODE.toString())) {
if (!oAuthService.checkAuthCode(authCode)) {
return buildBadAuthCodeResponse();
}
// TODO 后两种未测试
} else if (oauthRequest.getParam(OAuth.OAUTH_GRANT_TYPE).equals(GrantType.PASSWORD.toString())) {
if (!checkUserPassword(oauthRequest.getUsername(), oauthRequest.getPassword())) {
return buildInvalidUserPassResponse();
}
} else if (oauthRequest.getParam(OAuth.OAUTH_GRANT_TYPE).equals(GrantType.REFRESH_TOKEN.toString())) {
// https://github.com/zhouyongtao/homeinns-web
if (!oAuthService.checkAuthCode(authCode)) {
return buildInvalidRefreshTokenResponse();
}
}
// 生成Access Token
OAuthIssuer oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());
final String accessToken = oauthIssuerImpl.accessToken();
oAuthService.addAccessToken(accessToken, oAuthService.getUsernameByAuthCode(authCode));
final String refreshToken = oauthIssuerImpl.refreshToken();
oAuthService.addAccessToken(refreshToken, oAuthService.getUsernameByAuthCode(authCode));
// 生成OAuth响应
OAuthResponse response = OAuthASResponse.tokenResponse(HttpServletResponse.SC_OK)
.setAccessToken(accessToken).setExpiresIn(String.valueOf(oAuthService.getExpireIn()))
.setTokenType(TokenType.BEARER.toString()).setRefreshToken(refreshToken).buildJSONMessage();
// 根据OAuthResponse生成ResponseEntity
return new ResponseEntity<String>(response.getBody(), HttpStatus.valueOf(response.getResponseStatus()));
} catch (OAuthProblemException e) {
// 构建错误响应
OAuthResponse res = OAuthASResponse.errorResponse(HttpServletResponse.SC_BAD_REQUEST).error(e)
.buildJSONMessage();
return new ResponseEntity<String>(res.getBody(), HttpStatus.valueOf(res.getResponseStatus()));
}
}
示例9: generateAuthorizationCode
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl; //导入依赖的package包/类
/**
* All check passed, generate authorization code and redirect back
*
* @param request
* HTTP request
* @param response
* HTTP response
* @param savedRequest
* saved request
* @return always return false
* @throws IOException
* If an input or output exception occurs
* @throws OAuthSystemException
* If an OAuth system exception occurs
*/
protected boolean generateAuthorizationCode(HttpServletRequest request,
HttpServletResponse response, SavedOAuthRequest savedRequest)
throws IOException, OAuthSystemException {
String authCode = new OAuthIssuerImpl(new MD5Generator()).authorizationCode();
String scope = null;
if (savedRequest.scopes != null)
scope = OAuthUtils.encodeScopes(savedRequest.scopes);
oAuthService.addAuthCode(authCode, savedRequest.clientId, savedRequest.scopes);
clearSavedRequest(); // Clear saved request before redirect back
return ResponseUtils.processResponse(response, savedRequest.redirectURI,
ResponseUtils.responseAuthCode(request, authCode, scope, savedRequest.state));
}