spring-authorization-server 授权码许可类型授权页面自定义JSON响应,应用前后端分离场景

时间:2025-04-02 12:56:33
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import .Set;
  • import .factory.;
  • import ;
  • import 2.2ParameterNames;
  • import 2.2AuthorizationConsent;
  • import 2.2AuthorizationConsentService;
  • import 2.;
  • import 2.;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • /**
  • * 自定义用户授权页面
  • * 参照:/spring-projects-experimental/spring-authorization-server/blob/main/samples/boot/oauth2-integration/authorizationserver-custom-consent-page/src/main/java/sample/web/
  • *
  • * @author Daniel Garnier-Moiroux sunjing
  • * @since 2.4.0
  • */
  • @Controller
  • public class AuthorizationConsentController {
  • @Autowired(required = false)
  • private RegisteredClientRepository registeredClientRepository;
  • @Autowired(required = false)
  • private OAuth2AuthorizationConsentService authorizationConsentService;
  • @Autowired(required = false)
  • private ScopeService scopeService;
  • @GetMapping(value = "/oauth2/consent")
  • public ResponseEntity<Map<String, Object>> consent(Principal principal,
  • @RequestParam(OAuth2ParameterNames.CLIENT_ID) String clientId,
  • @RequestParam(OAuth2) String scope,
  • @RequestParam(OAuth2) String state) {
  • // Remove scopes that were already approved
  • Set<String> scopesToApprove = new HashSet<>();
  • Set<String> previouslyApprovedScopes = new HashSet<>();
  • Set<String> authorizedScopes = authorizedScopes(clientId, ());
  • for (String requestedScope : (scope, " ")) {
  • if (authorizedScopes.contains(requestedScope)) {
  • previouslyApprovedScopes.add(requestedScope);
  • } else {
  • scopesToApprove.add(requestedScope);
  • }
  • }
  • Map<String, Map<String, Object>> scopeCodeToScope = scopeCodeToScope(scopesToApprove, previouslyApprovedScopes);
  • RegisteredClient registeredClient = (clientId);
  • Map<String, Object> response = new HashMap<>();
  • ("clientId", clientId);
  • ("clientName", ());
  • ("state", state);
  • ("scopes", withDescription(scopesToApprove, scopeCodeToScope));
  • ("previouslyApprovedScopes", withDescription(previouslyApprovedScopes, scopeCodeToScope));
  • ("principalName", ());
  • return (response);
  • }
  • private Set<String> authorizedScopes(String clientId, String principalName) {
  • OAuth2AuthorizationConsent currentAuthorizationConsent =
  • (clientId, principalName);
  • return currentAuthorizationConsent != null ? () : ();
  • }
  • private Map<String, Map<String, Object>> scopeCodeToScope(Set<String> scopesToApprove,
  • Set<String> previouslyApprovedScopes) {
  • List<String> scopeCodes = new ArrayList<>();
  • (scopesToApprove);
  • (previouslyApprovedScopes);
  • List<Map<String, Object>> scopes = (scopeCodes);
  • Map<String, Map<String, Object>> scopeCodeToScope = new HashMap<>();
  • for (Map<String, Object> scope : scopes) {
  • (scope.get("scopeCode").toString(), scope);
  • }
  • return scopeCodeToScope;
  • }
  • private List<ScopeWithDescription> withDescription(Set<String> scopesToApprove,
  • Map<String, Map<String, Object>> scopeCodeToScope) {
  • List<ScopeWithDescription> scopeWithDescriptions = new LinkedList<>();
  • for (String scopeCode : scopesToApprove) {
  • Map<String, Object> scope = scopeCodeToScope.get(scopeCode);
  • if (scope == null) {
  • scopeWithDescriptions.add(new ScopeWithDescription(scopeCode, "", ""));
  • continue;
  • }
  • String scopeName = ("scopeName", "").toString();
  • String scopeProfileInfo = ("scopeProfileInfo", "").toString();
  • scopeWithDescriptions.add(new ScopeWithDescription(scopeCode, scopeName, scopeProfileInfo));
  • }
  • return scopeWithDescriptions;
  • }
  • @Getter
  • private static class ScopeWithDescription {
  • /**
  • * scope的编码
  • */
  • private final String scope;
  • /**
  • * scope的名称
  • */
  • private final String scopeName;
  • /**
  • * 概要信息
  • */
  • private final String scopeProfileInfo;
  • ScopeWithDescription(String scope, String scopeName, String scopeProfileInfo) {
  • = scope;
  • = scopeName;
  • = scopeProfileInfo;
  • }
  • }
  • }