【问题标题】:Spring OAuth2 - custom "OAuth Approval" page at oauth/authorizeSpring OAuth2 - oauth/authorize 的自定义“OAuth Approval”页面
【发布时间】:2015-06-03 10:14:59
【问题描述】:

有什么推荐的方式来创建自定义页面 OAuth Approval 页面:

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

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

【问题讨论】:

    标签: spring-security spring-security-oauth2


    【解决方案1】:

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

    【讨论】:

    • 是否有另一种方式来自定义页面。在我的设置应用程序中,HandlerMapping 的顺序低于org.springframework.security.oauth2.provider.endpoint.FrameworkEndpointHandlerMapping,因此,自定义映射在默认映射之后被拾取并且不覆盖后者?
    • 我不认为我遵循这一点。您只需在 /oauth/confirm_access (或您自定义端点路径的任何内容)处提供资源。不管你怎么做。
    • Framew‌​orkEndpointHandlerMa‌pping 的订单为Order.LOWEST_PRECEDENCE - 2,但我的自定义RequestMappingHandlerMapping 的订单为Order.LOWEST_PRECEDENCE 因此org.springframework.web.servlet.DispatcherServlet#getHandler 选择Framew‌​orkEndpointHandlerMa‌pping 的映射和请求永远不会到达我的客户控制器.不幸的是,此时更改订单不是我的选择。我最终做的是authorizationEndpoint.setUserApprovalPage("forward:/oauth/customer_path");在AuthorizationServerConfiguration@PostConstruct 方法中。
    • 由于/oauth/customer_path 没有被Framew‌​orkEndpointHandlerMa‌pping 映射,调度程序到达我的控制器的映射。
    • 另外,不要忘记将您的控制器更改为具有 WhitelabelApprovalEndpoint 以外的 bean 名称,否则 Spring 将获得优先权(正如我最终发现的那样).....
    【解决方案2】:

    除了@DaveSyer 的answer,它应该适用于大多数情况。有时基于配置和自定义,如果 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 这样的配置映射将分别用作确认页面和错误页面。

    【讨论】:

      【解决方案3】:

      使用WebMvcConfigurer 和 覆盖

      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

      【讨论】:

        猜你喜欢
        • 2015-03-20
        • 2016-04-29
        • 2011-08-09
        • 2017-08-31
        • 2016-08-25
        • 2018-06-14
        • 2011-07-27
        • 2017-03-10
        • 2016-05-17
        相关资源
        最近更新 更多