【发布时间】:2015-06-03 10:14:59
【问题描述】:
有什么推荐的方式来创建自定义页面 OAuth Approval 页面:
我必须完全覆盖页面上的内容,需要添加样式、品牌等。实现这一目标的正确方法是什么?我在哪里可以看到默认页面的来源以将其用作起点?
我还需要覆盖 /login 页面,但我认为覆盖它的方法几乎相同。
【问题讨论】:
标签: spring-security spring-security-oauth2
有什么推荐的方式来创建自定义页面 OAuth Approval 页面:
我必须完全覆盖页面上的内容,需要添加样式、品牌等。实现这一目标的正确方法是什么?我在哪里可以看到默认页面的来源以将其用作起点?
我还需要覆盖 /login 页面,但我认为覆盖它的方法几乎相同。
【问题讨论】:
标签: spring-security spring-security-oauth2
推荐的方法是为“/oauth/confirm_access”提供一个普通的 Spring MVC @RequestMapping。您可以查看WhitelabelApprovalEndpoint 的默认实现。不要忘记在控制器中使用@SessionAttributes("authorizationRequest")。
【讨论】:
HandlerMapping 的顺序低于org.springframework.security.oauth2.provider.endpoint.FrameworkEndpointHandlerMapping,因此,自定义映射在默认映射之后被拾取并且不覆盖后者?
FrameworkEndpointHandlerMapping 的订单为Order.LOWEST_PRECEDENCE - 2,但我的自定义RequestMappingHandlerMapping 的订单为Order.LOWEST_PRECEDENCE 因此org.springframework.web.servlet.DispatcherServlet#getHandler 选择FrameworkEndpointHandlerMapping 的映射和请求永远不会到达我的客户控制器.不幸的是,此时更改订单不是我的选择。我最终做的是authorizationEndpoint.setUserApprovalPage("forward:/oauth/customer_path");在AuthorizationServerConfiguration 的@PostConstruct 方法中。
/oauth/customer_path 没有被FrameworkEndpointHandlerMapping 映射,调度程序到达我的控制器的映射。
除了@DaveSyer 的answer,它应该适用于大多数情况。有时基于配置和自定义,如果 Spring Security OAuth 包中的FrameworkEndpointHandlerMapping 的顺序高于您的应用程序的RequestMappingHandlerMapping,则上述方法可能不起作用。如果是这种情况,那么 servlet 调度程序将永远不会到达您的映射,并且将始终显示默认页面。
解决它的一种方法是更改映射器的顺序,假设FrameworkEndpointHandlerMapping 的顺序是Order.LOWEST_PRECEDENCE - 2。
另一种方法是将审批页面设置为自定义 URL,而不是由 FrameworkEndpointHandlerMapping 映射,因此 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 和
覆盖
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。
【讨论】: