SpringBoot+SpringSecurity OAuth2 认证服务搭建实战 (七)OAuth2经典场景~授权码模式+自定义登录页面&授权页面

admin2024-07-05  14

关键代码

@Bean
	@Order(Ordered.HIGHEST_PRECEDENCE)
	public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http)
			throws Exception {
		
		//OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);
		
		OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =	new OAuth2AuthorizationServerConfigurer();
		authorizationServerConfigurer.authorizationEndpoint(authorizationEndpoint ->{
                                       authorizationEndpoint.consentPage("/oauth2/consent");
                     }).oidc(Customizer.withDefaults());	// Enable OpenID Connect 1.0
		RequestMatcher endpointsMatcher = authorizationServerConfigurer.getEndpointsMatcher();
		http
			.securityMatcher(endpointsMatcher)
			.authorizeHttpRequests(authorize ->
				authorize.anyRequest().authenticated()
			)
			.csrf(csrf -> csrf.ignoringRequestMatchers(endpointsMatcher))
			.apply(authorizationServerConfigurer);
        
        http.exceptionHandling((exceptions) -> exceptions
                        .defaultAuthenticationEntryPointFor(//如果没有被认证过登录,则跳转到登录页面
                                new LoginUrlAuthenticationEntryPoint("/login"),
                                new MediaTypeRequestMatcher(MediaType.TEXT_HTML)
                        )
       
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!