Spring OAuth2 - oauth/authorize 处的自定义 "OAuth Approval" 页面

Spring OAuth2 - custom "OAuth Approval" page at oauth/authorize

创建自定义页面 OAuth 批准页面的推荐方法:

我必须完全覆盖页面上的内容,需要添加样式、品牌等。实现该目标的正确方法是什么?我在哪里可以看到默认页面的来源以将其用作起点?

我还需要覆盖 /login 页面,但我认为覆盖它的方法几乎相同。

推荐的方法是为“/oauth/confirm_access”提供一个正常的Spring MVC @RequestMapping。您可以查看 WhitelabelApprovalEndpoint 以了解默认实现。不要忘记在控制器中使用 @SessionAttributes("authorizationRequest")

除了@DaveSyer 的 ,它应该适用于大多数情况。有时基于配置和自定义,如果来自 Spring Security OAuth 包的 Framew‌orkEndpointHandlerMa‌pping 的顺序高于应用程序的 RequestMappingHandlerMapping,则上述方法可能不起作用。如果是这种情况,那么 servlet 调度程序将永远不会到达您的映射并且将始终显示默认页面。

修复它的一种方法是更改​​映射器的顺序,假设 Framew‌orkEndpointHandlerMa‌pping 的顺序是 Order.LOWEST_PRECEDENCE - 2

另一种方法是将批准页面设置为自定义 URL,而不是 Framew‌orkEndpointHandlerMa‌pping 映射,这样 servlet 调度程序将到达您应用程序的映射

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private AuthorizationEndpoint authorizationEndpoint;

    @PostConstruct
    public void init() {
        authorizationEndpoint.setUserApprovalPage("forward:/oauth/custom_confirm_access");
        authorizationEndpoint.setErrorPage("forward:/oauth/custom_error");
    }
}

有了这样的配置,/oauth/custom_confirm_access/oauth/custom_error的映射将分别用作确认页面和错误页面。

WebMvcConfigurer 实现你的 class 和 覆盖

void addViewControllers(ViewControllerRegistry registry)方法

@SpringBootApplication
@EnableAuthorizationServer
public class AuthServerApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(AuthServerApplication.class, args);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/oauth/confirm_access").setViewName("AuthorizationPage");
    }
}

此处 AuthorizationPage 是您创建的 html page