当前位置: 首页>>代码示例>>Java>>正文


Java AuthorizationRequest.setScope方法代码示例

本文整理汇总了Java中org.springframework.security.oauth2.provider.AuthorizationRequest.setScope方法的典型用法代码示例。如果您正苦于以下问题:Java AuthorizationRequest.setScope方法的具体用法?Java AuthorizationRequest.setScope怎么用?Java AuthorizationRequest.setScope使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.springframework.security.oauth2.provider.AuthorizationRequest的用法示例。


在下文中一共展示了AuthorizationRequest.setScope方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: updateAfterApproval

import org.springframework.security.oauth2.provider.AuthorizationRequest; //导入方法依赖的package包/类
/**
 * Requires the authorization request to be explicitly approved, including all individual scopes, and the user to be
 * authenticated. A scope that was requested in the authorization request can be approved by sending a request
 * parameter <code>scope.&lt;scopename&gt;</code> equal to "true" or "approved" (otherwise it will be assumed to
 * have been denied). The {@link ApprovalStore} will be updated to reflect the inputs.
 * 
 * @param authorizationRequest The authorization request.
 * @param userAuthentication the current user authentication
 * 
 * @return An approved request if all scopes have been approved by the current user.
 */
public AuthorizationRequest updateAfterApproval(AuthorizationRequest authorizationRequest,
		Authentication userAuthentication) {
	// Get the approved scopes
	Set<String> requestedScopes = authorizationRequest.getScope();
	Set<String> approvedScopes = new HashSet<String>();
	Set<Approval> approvals = new HashSet<Approval>();

	Date expiry = computeExpiry();

	// Store the scopes that have been approved / denied
	Map<String, String> approvalParameters = authorizationRequest.getApprovalParameters();
	for (String requestedScope : requestedScopes) {
		String approvalParameter = scopePrefix + requestedScope;
		String value = approvalParameters.get(approvalParameter);
		value = value == null ? "" : value.toLowerCase();
		if ("true".equals(value) || value.startsWith("approve")) {
			approvedScopes.add(requestedScope);
			approvals.add(new Approval(userAuthentication.getName(), authorizationRequest.getClientId(),
					requestedScope, expiry, ApprovalStatus.APPROVED));
		}
		else {
			approvals.add(new Approval(userAuthentication.getName(), authorizationRequest.getClientId(),
					requestedScope, expiry, ApprovalStatus.DENIED));
		}
	}
	approvalStore.addApprovals(approvals);

	boolean approved;
	authorizationRequest.setScope(approvedScopes);
	if (approvedScopes.isEmpty() && !requestedScopes.isEmpty()) {
		approved = false;
	}
	else {
		approved = true;
	}
	authorizationRequest.setApproved(approved);
	return authorizationRequest;
}
 
开发者ID:jungyang,项目名称:oauth-client-master,代码行数:50,代码来源:ApprovalStoreUserApprovalHandler.java

示例2: init

import org.springframework.security.oauth2.provider.AuthorizationRequest; //导入方法依赖的package包/类
@Before
public void init() {
	AuthorizationRequest authorizationRequest = new AuthorizationRequest();
	authorizationRequest.setClientId("client");
	authorizationRequest.setScope(Arrays.asList("read", "write"));
	authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(), userAuthentication);
	InMemoryClientDetailsService clientDetailsService = new InMemoryClientDetailsService();
	client = new BaseClientDetails("client", "source", "read,write", "authorization_code,client_credentials",
			"read");
	clientDetailsService.setClientDetailsStore(Collections.singletonMap("client", client));
	voter.setClientDetailsService(clientDetailsService);
}
 
开发者ID:jungyang,项目名称:oauth-client-master,代码行数:13,代码来源:ClientScopeVoterTests.java

示例3: checkForPreApproval

import org.springframework.security.oauth2.provider.AuthorizationRequest; //导入方法依赖的package包/类
public AuthorizationRequest checkForPreApproval(AuthorizationRequest authorizationRequest,
		Authentication userAuthentication) {

	String clientId = authorizationRequest.getClientId();
	Collection<String> requestedScopes = authorizationRequest.getScope();
	Set<String> approvedScopes = new HashSet<String>();
	Set<String> validUserApprovedScopes = new HashSet<String>();

	if (clientDetailsService != null) {
		try {
			ClientDetails client = clientDetailsService.loadClientByClientId(clientId);
			for (String scope : requestedScopes) {
				if (client.isAutoApprove(scope) || client.isAutoApprove("all")) {
					approvedScopes.add(scope);
				}
			}
			if (approvedScopes.containsAll(requestedScopes)) {
				authorizationRequest.setApproved(true);
				return authorizationRequest;
			}
		}
		catch (ClientRegistrationException e) {
			logger.warn("Client registration problem prevent autoapproval check for client=" + clientId);
		}
	}

	if (logger.isDebugEnabled()) {
		StringBuilder builder = new StringBuilder("Looking up user approved authorizations for ");
		builder.append("client_id=" + clientId);
		builder.append(" and username=" + userAuthentication.getName());
		logger.debug(builder.toString());
	}

	// Find the stored approvals for that user and client
	Collection<Approval> userApprovals = approvalStore.getApprovals(userAuthentication.getName(),
			clientId);

	// Look at the scopes and see if they have expired
	Date today = new Date();
	for (Approval approval : userApprovals) {
		if (approval.getExpiresAt().after(today)) {
			validUserApprovedScopes.add(approval.getScope());
			if (approval.getStatus() == ApprovalStatus.APPROVED) {
				approvedScopes.add(approval.getScope());
			}
		}
	}

	if (logger.isDebugEnabled()) {
		logger.debug("Valid user approved/denied scopes are " + validUserApprovedScopes);
	}

	// If the requested scopes have already been acted upon by the user,
	// this request is approved
	if (validUserApprovedScopes.containsAll(requestedScopes)) {
		approvedScopes.retainAll(requestedScopes);
		// Set only the scopes that have been approved by the user
		authorizationRequest.setScope(approvedScopes);
		authorizationRequest.setApproved(true);
	}

	return authorizationRequest;

}
 
开发者ID:jungyang,项目名称:oauth-client-master,代码行数:65,代码来源:ApprovalStoreUserApprovalHandler.java

示例4: doFilter

import org.springframework.security.oauth2.provider.AuthorizationRequest; //导入方法依赖的package包/类
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
		ServletException {

	final boolean debug = logger.isDebugEnabled();
	final HttpServletRequest request = (HttpServletRequest) req;
	final HttpServletResponse response = (HttpServletResponse) res;

	try {
		Authentication credentials = extractCredentials(request);

		if (credentials != null) {

			if (debug) {
				logger.debug("Authentication credentials found for '" + credentials.getName() + "'");
			}

			Authentication authResult = authenticationManager.authenticate(credentials);

			if (debug) {
				logger.debug("Authentication success: " + authResult.getName());
			}

			Authentication clientAuth = SecurityContextHolder.getContext().getAuthentication();
			if (clientAuth == null) {
				throw new BadCredentialsException(
						"No client authentication found. Remember to put a filter upstream of the TokenEndpointAuthenticationFilter.");
			}
			
			Map<String, String> map = getSingleValueMap(request);
			map.put(OAuth2Utils.CLIENT_ID, clientAuth.getName());
			AuthorizationRequest authorizationRequest = oAuth2RequestFactory.createAuthorizationRequest(map);

			authorizationRequest.setScope(getScope(request));
			if (clientAuth.isAuthenticated()) {
				// Ensure the OAuth2Authentication is authenticated
				authorizationRequest.setApproved(true);
			}

			OAuth2Request storedOAuth2Request = oAuth2RequestFactory.createOAuth2Request(authorizationRequest);
			
			SecurityContextHolder.getContext().setAuthentication(
					new OAuth2Authentication(storedOAuth2Request, authResult));

			onSuccessfulAuthentication(request, response, authResult);

		}

	}
	catch (AuthenticationException failed) {
		SecurityContextHolder.clearContext();

		if (debug) {
			logger.debug("Authentication request for failed: " + failed);
		}

		onUnsuccessfulAuthentication(request, response, failed);

		authenticationEntryPoint.commence(request, response, failed);

		return;
	}

	chain.doFilter(request, response);
}
 
开发者ID:jungyang,项目名称:oauth-client-master,代码行数:65,代码来源:TokenEndpointAuthenticationFilter.java


注:本文中的org.springframework.security.oauth2.provider.AuthorizationRequest.setScope方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。